Fixed code style in db schema definitions.

This commit is contained in:
s3lph 2018-07-21 21:46:46 +02:00
parent ce0a01cb7c
commit 9afd86d5ab

View file

@ -1,105 +1,103 @@
from typing import Dict, List from typing import Dict, List
SCHEMAS: Dict[int, List[str]] = dict() SCHEMAS: Dict[int, List[str]] = dict()
SCHEMAS[1] = [
'''
CREATE TABLE users (
user_id INTEGER PRIMARY KEY,
username TEXT UNIQUE NOT NULL,
email TEXT DEFAULT NULL,
password TEXT NOT NULL,
touchkey TEXT DEFAULT NULL,
is_admin INTEGER(1) NOT NULL DEFAULT 0,
is_member INTEGER(1) NOT NULL DEFAULT 1,
balance INTEGER(8) NOT NULL DEFAULT 0,
lastchange INTEGER(8) NOT NULL DEFAULT 0
);
''',
'''
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
name TEXT UNIQUE NOT NULL,
stock INTEGER(8) NOT NULL DEFAULT 0,
price_member INTEGER(8) NOT NULL,
price_non_member INTEGER(8) NOT NULL
);
''',
'''
CREATE TABLE consumption (
user_id INTEGER NOT NULL,
product_id INTEGER NOT NULL,
count INTEGER(8) NOT NULL DEFAULT 0,
PRIMARY KEY (user_id, product_id),
FOREIGN KEY (user_id) REFERENCES users(user_id)
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (product_id) REFERENCES products(product_id)
ON DELETE CASCADE ON UPDATE CASCADE
);
''']
SCHEMAS[1] = [''' SCHEMAS[2] = [
CREATE TABLE users ( '''
user_id INTEGER PRIMARY KEY, CREATE TABLE users (
username TEXT UNIQUE NOT NULL, user_id INTEGER PRIMARY KEY,
email TEXT DEFAULT NULL, username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL, email TEXT DEFAULT NULL,
touchkey TEXT DEFAULT NULL, password TEXT NOT NULL,
is_admin INTEGER(1) NOT NULL DEFAULT 0, touchkey TEXT DEFAULT NULL,
is_member INTEGER(1) NOT NULL DEFAULT 1, is_admin INTEGER(1) NOT NULL DEFAULT 0,
balance INTEGER(8) NOT NULL DEFAULT 0, is_member INTEGER(1) NOT NULL DEFAULT 1,
lastchange INTEGER(8) NOT NULL DEFAULT 0 balance INTEGER(8) NOT NULL DEFAULT 0,
); lastchange INTEGER(8) NOT NULL DEFAULT 0
''', );
''' ''',
CREATE TABLE products ( '''
product_id INTEGER PRIMARY KEY, CREATE TABLE products (
name TEXT UNIQUE NOT NULL, product_id INTEGER PRIMARY KEY,
stock INTEGER(8) NOT NULL DEFAULT 0, name TEXT UNIQUE NOT NULL,
price_member INTEGER(8) NOT NULL, stock INTEGER(8) NOT NULL DEFAULT 0,
price_non_member INTEGER(8) NOT NULL price_member INTEGER(8) NOT NULL,
); price_non_member INTEGER(8) NOT NULL
''', );
''' ''',
CREATE TABLE consumption ( '''
user_id INTEGER NOT NULL, CREATE TABLE transactions ( -- "superclass" of the following 3 tables
product_id INTEGER NOT NULL, ta_id INTEGER PRIMARY KEY,
count INTEGER(8) NOT NULL DEFAULT 0, user_id INTEGER NOT NULL,
PRIMARY KEY (user_id, product_id), value INTEGER(8) NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(user_id) old_balance INTEGER(8) NOT NULL,
ON DELETE CASCADE ON UPDATE CASCADE, date INTEGER(8) DEFAULT (STRFTIME('%s', 'now')),
FOREIGN KEY (product_id) REFERENCES products(product_id) FOREIGN KEY (user_id) REFERENCES users(user_id)
ON DELETE CASCADE ON UPDATE CASCADE ON DELETE CASCADE ON UPDATE CASCADE
); );
'''] ''',
'''
CREATE TABLE consumptions ( -- transactions involving buying a product
SCHEMAS[2] = [''' ta_id INTEGER PRIMARY KEY,
CREATE TABLE users ( product_id INTEGER DEFAULT NULL,
user_id INTEGER PRIMARY KEY, FOREIGN KEY (ta_id) REFERENCES transactions(ta_id)
username TEXT UNIQUE NOT NULL, ON DELETE CASCADE ON UPDATE CASCADE,
email TEXT DEFAULT NULL, FOREIGN KEY (product_id) REFERENCES products(product_id)
password TEXT NOT NULL, ON DELETE SET NULL ON UPDATE CASCADE
touchkey TEXT DEFAULT NULL, );
is_admin INTEGER(1) NOT NULL DEFAULT 0, ''',
is_member INTEGER(1) NOT NULL DEFAULT 1, '''
balance INTEGER(8) NOT NULL DEFAULT 0, CREATE TABLE deposits ( -- transactions involving depositing cash
lastchange INTEGER(8) NOT NULL DEFAULT 0 ta_id INTEGER PRIMARY KEY,
); FOREIGN KEY (ta_id) REFERENCES transactions(ta_id)
''', ON DELETE CASCADE ON UPDATE CASCADE
''' );
CREATE TABLE products ( ''',
product_id INTEGER PRIMARY KEY, '''
name TEXT UNIQUE NOT NULL, CREATE TABLE modifications ( -- transactions involving balance modification by an admin
stock INTEGER(8) NOT NULL DEFAULT 0, ta_id INTEGER NOT NULL,
price_member INTEGER(8) NOT NULL, agent_id INTEGER NOT NULL,
price_non_member INTEGER(8) NOT NULL reason TEXT DEFAULT NULL,
); PRIMARY KEY (ta_id),
''', FOREIGN KEY (ta_id) REFERENCES transactions(ta_id)
''' ON DELETE CASCADE ON UPDATE CASCADE,
CREATE TABLE transactions ( -- "superclass" of the following 3 tables FOREIGN KEY (agent_id) REFERENCES users(user_id)
ta_id INTEGER PRIMARY KEY, ON DELETE CASCADE ON UPDATE CASCADE
user_id INTEGER NOT NULL, );
value INTEGER(8) NOT NULL, ''']
old_balance INTEGER(8) NOT NULL,
date INTEGER(8) DEFAULT (STRFTIME('%s', 'now')),
FOREIGN KEY (user_id) REFERENCES users(user_id)
ON DELETE CASCADE ON UPDATE CASCADE
);
''',
'''
CREATE TABLE consumptions ( -- transactions involving buying a product
ta_id INTEGER PRIMARY KEY,
product_id INTEGER DEFAULT NULL,
FOREIGN KEY (ta_id) REFERENCES transactions(ta_id)
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (product_id) REFERENCES products(product_id)
ON DELETE SET NULL ON UPDATE CASCADE
);
''',
'''
CREATE TABLE deposits ( -- transactions involving depositing cash
ta_id INTEGER PRIMARY KEY,
FOREIGN KEY (ta_id) REFERENCES transactions(ta_id)
ON DELETE CASCADE ON UPDATE CASCADE
);
''',
'''
CREATE TABLE modifications ( -- transactions involving balance modification by an admin
ta_id INTEGER NOT NULL,
agent_id INTEGER NOT NULL,
reason TEXT DEFAULT NULL,
PRIMARY KEY (ta_id),
FOREIGN KEY (ta_id) REFERENCES transactions(ta_id)
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (agent_id) REFERENCES users(user_id)
ON DELETE CASCADE ON UPDATE CASCADE
);
''']