Create basic database schema, and creation script

makedb should only have idempotent migrations, so it can be run on an
existing database.
This commit is contained in:
Leonora Tindall 2019-09-09 17:43:55 -05:00
parent 20f219793d
commit b375760aab
Signed by: nora
GPG Key ID: 99041B68DBC02DAC
3 changed files with 35 additions and 8 deletions

View File

@ -0,0 +1,3 @@
## Setup
Create the database schema `docker-compose exec webserver python src/makedb.py`

View File

@ -4,12 +4,16 @@ Database handling code for the CodePage
import psycopg2
import config
def getdb():
return psycopg2.connect(
dbname=config.DB_NAME,
user=config.DB_USER,
password=config.DB_PASS,
host=config.DB_HOST,
port=config.DB_PORT
)
DATABASE = None
def getdb():
global DATABASE
if DATABASE is None:
DATABASE = psycopg2.connect(
dbname=config.DB_NAME,
user=config.DB_USER,
password=config.DB_PASS,
host=config.DB_HOST,
port=config.DB_PORT)
return DATABASE.cursor()

20
webserver/src/makedb.py Normal file
View File

@ -0,0 +1,20 @@
from database import getdb
with getdb() as db:
# Initial database schema
db.execute("""
CREATE TABLE IF NOT EXISTS users (
id INT PRIMARY KEY NOT NULL,
dispname TEXT NOT NULL,
pwhash TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS codes (
id INT PRIMARY KEY NOT NULL,
username INT NOT NULL,
language TEXT NOT NULL,
title TEXT NOT NULL,
contents TEXT NOT NULL,
FOREIGN KEY (username) REFERENCES users(id)
);
""")