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:
parent
20f219793d
commit
b375760aab
|
@ -0,0 +1,3 @@
|
|||
## Setup
|
||||
|
||||
Create the database schema `docker-compose exec webserver python src/makedb.py`
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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…
Reference in New Issue