diff --git a/matemat/__init__.py b/matemat/__init__.py index 489c7e1..4cedeb6 100644 --- a/matemat/__init__.py +++ b/matemat/__init__.py @@ -1,2 +1,2 @@ -__version__ = '0.4.1' +__version__ = '0.4.2' diff --git a/matemat/db/migrations.py b/matemat/db/migrations.py index d9f8c64..286f805 100644 --- a/matemat/db/migrations.py +++ b/matemat/db/migrations.py @@ -309,3 +309,31 @@ def migrate_schema_8_to_9(c: sqlite3.Cursor): ON DELETE CASCADE ON UPDATE CASCADE ) ''') + + +def migrate_schema_9_to_10(c: sqlite3.Cursor): + c.execute(''' + ALTER TABLE users RENAME TO users_old + ''') + c.execute(''' + CREATE TABLE users ( + user_id INTEGER PRIMARY KEY, + username TEXT UNIQUE NOT NULL COLLATE NOCASE, + 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, + receipt_pref INTEGER(1) NOT NULL DEFAULT 0, + created INTEGER(8) NOT NULL DEFAULT 0, + logout_after_purchase INTEGER(1) DEFAULT 0 + ) + ''') + c.execute(''' + INSERT INTO users SELECT * FROM users_old + ''') + c.execute(''' + DROP TABLE users_old + ''') diff --git a/matemat/db/schemas.py b/matemat/db/schemas.py index 54d0980..5481fc8 100644 --- a/matemat/db/schemas.py +++ b/matemat/db/schemas.py @@ -669,3 +669,96 @@ SCHEMAS[9] = [ ON DELETE CASCADE ON UPDATE CASCADE ); '''] + + +SCHEMAS[10] = [ + ''' + CREATE TABLE users ( + user_id INTEGER PRIMARY KEY, + username TEXT UNIQUE NOT NULL COLLATE NOCASE, + 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, + receipt_pref INTEGER(1) NOT NULL DEFAULT 0, + created INTEGER(8) NOT NULL DEFAULT 0, + logout_after_purchase INTEGER(1) DEFAULT 0 + ); + ''', + ''' + CREATE TABLE products ( + product_id INTEGER PRIMARY KEY, + name TEXT UNIQUE NOT NULL, + stock INTEGER(8) DEFAULT 0, + stockable INTEGER(1) DEFAULT 1, + price_member INTEGER(8) NOT NULL, + price_non_member INTEGER(8) NOT NULL, + custom_price INTEGER(1) DEFAULT 0, + ean TEXT UNIQUE DEFAULT NULL + ); + ''', + ''' + CREATE TABLE transactions ( -- "superclass" of the following 3 tables + ta_id INTEGER PRIMARY KEY, + user_id INTEGER DEFAULT 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 SET NULL ON UPDATE CASCADE + ); + ''', + ''' + CREATE TABLE consumptions ( -- transactions involving buying a product + ta_id INTEGER PRIMARY KEY, + product TEXT NOT NULL, + FOREIGN KEY (ta_id) REFERENCES transactions(ta_id) + ON DELETE CASCADE 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 TEXT 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 receipts ( -- receipts sent to the users + receipt_id INTEGER PRIMARY KEY, + user_id INTEGER NOT NULL, + first_ta_id INTEGER DEFAULT NULL, + last_ta_id INTEGER DEFAULT NULL, + date INTEGER(8) DEFAULT (STRFTIME('%s', 'now')), + FOREIGN KEY (user_id) REFERENCES users(user_id) + ON DELETE CASCADE ON UPDATE CASCADE, + FOREIGN KEY (first_ta_id) REFERENCES transactions(ta_id) + ON DELETE SET NULL ON UPDATE CASCADE, + FOREIGN KEY (last_ta_id) REFERENCES transactions(ta_id) + ON DELETE SET NULL ON UPDATE CASCADE + ); + ''', + ''' + CREATE TABLE tokens ( -- authentication tokens such as barcodes + token_id INTEGER PRIMARY KEY, + user_id INTEGER NOT NULL, + token TEXT UNIQUE NOT NULL, + name TEXT UNIQUE NOT NULL, + date INTEGER(8) DEFAULT (STRFTIME('%s', 'now')), + FOREIGN KEY (user_id) REFERENCES users(user_id) + ON DELETE CASCADE ON UPDATE CASCADE + ); + '''] diff --git a/matemat/db/wrapper.py b/matemat/db/wrapper.py index 2493b86..30fd6e5 100644 --- a/matemat/db/wrapper.py +++ b/matemat/db/wrapper.py @@ -40,7 +40,7 @@ class DatabaseTransaction(object): class DatabaseWrapper(object): - SCHEMA_VERSION = 9 + SCHEMA_VERSION = 10 def __init__(self, filename: str) -> None: self._filename: str = filename @@ -78,8 +78,7 @@ class DatabaseWrapper(object): def _upgrade(self, from_version: int, to_version: int) -> None: with self.transaction() as c: - # Note to future s3lph: If there are further migrations, also consider upgrades like 1 -> 3 - if from_version == 1 and to_version >= 2: + if from_version <= 1 and to_version >= 2: migrate_schema_1_to_2(c) if from_version <= 2 and to_version >= 3: migrate_schema_2_to_3(c) @@ -97,6 +96,8 @@ class DatabaseWrapper(object): migrate_schema_7_to_8(c) if from_version <= 8 and to_version >= 9: migrate_schema_8_to_9(c) + if from_version <= 9 and to_version >= 10: + migrate_schema_9_to_10(c) def connect(self) -> None: if self.is_connected():