Browse Source

Create basic database schema, and creation script

makedb should only have idempotent migrations, so it can be run on an
existing database.
master
Leonora Tindall 2 years ago
parent
commit
b375760aab
Signed by: nora GPG Key ID: 99041B68DBC02DAC
  1. 3
      README.md
  2. 18
      webserver/src/database.py
  3. 20
      webserver/src/makedb.py

3
README.md

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

18
webserver/src/database.py

@ -4,12 +4,16 @@ Database handling code for the CodePage
import psycopg2
import config
DATABASE = None
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
)
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

@ -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)
);
""")
Loading…
Cancel
Save