From 3bdc9417fd8ed09877f0a897e4979974043273fe Mon Sep 17 00:00:00 2001 From: s3lph Date: Sat, 1 Sep 2018 23:23:06 +0200 Subject: [PATCH] Database schema: Added user.created column. --- matemat/db/facade.py | 6 +++--- matemat/db/migrations.py | 12 +++++++++++- matemat/db/schemas.py | 3 ++- matemat/db/test/test_migrations.py | 7 ++++++- matemat/db/test/test_wrapper.py | 6 +++--- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/matemat/db/facade.py b/matemat/db/facade.py index 8e9fe08..7b798ed 100644 --- a/matemat/db/facade.py +++ b/matemat/db/facade.py @@ -114,7 +114,7 @@ class MatematDatabase(object): user_id, username, email, is_admin, is_member, balance, receipt_p = row try: receipt_pref: ReceiptPreference = ReceiptPreference(receipt_p) - except ValueError as e: + except ValueError: raise DatabaseConsistencyError(f'{receipt_p} is not a valid ReceiptPreference') return User(user_id, username, balance, email, is_admin, is_member, receipt_pref) @@ -144,8 +144,8 @@ class MatematDatabase(object): raise ValueError(f'A user with the name \'{username}\' already exists.') # Insert the user into the database. c.execute(''' - INSERT INTO users (username, email, password, balance, is_admin, is_member, lastchange) - VALUES (:username, :email, :pwhash, 0, :admin, :member, STRFTIME('%s', 'now')) + INSERT INTO users (username, email, password, balance, is_admin, is_member, lastchange, created) + VALUES (:username, :email, :pwhash, 0, :admin, :member, STRFTIME('%s', 'now'), STRFTIME('%s', 'now')) ''', { 'username': username, 'email': email, diff --git a/matemat/db/migrations.py b/matemat/db/migrations.py index cad2461..8983180 100644 --- a/matemat/db/migrations.py +++ b/matemat/db/migrations.py @@ -116,8 +116,18 @@ def migrate_schema_1_to_2(c: sqlite3.Cursor): def migrate_schema_2_to_3(c: sqlite3.Cursor): - # Add missing column to users table + # Add missing columns to users table c.execute('ALTER TABLE users ADD COLUMN receipt_pref INTEGER(1) NOT NULL DEFAULT 0') + c.execute('''ALTER TABLE users ADD COLUMN created INTEGER(8) NOT NULL DEFAULT 0''') + # Guess creation date based on the oldest entry in the database related to the user ( -1 minute for good measure) + c.execute(''' + UPDATE users + SET created = COALESCE( + (SELECT MIN(t.date) + FROM transactions AS t + WHERE t.user_id = users.user_id), + lastchange) - 60 + ''') # Fix ON DELETE in transactions table c.execute(''' diff --git a/matemat/db/schemas.py b/matemat/db/schemas.py index 4907f39..3bbb5b9 100644 --- a/matemat/db/schemas.py +++ b/matemat/db/schemas.py @@ -114,7 +114,8 @@ SCHEMAS[3] = [ 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 + receipt_pref INTEGER(1) NOT NULL DEFAULT 0, + created INTEGER(8) NOT NULL DEFAULT 0 ); ''', ''' diff --git a/matemat/db/test/test_migrations.py b/matemat/db/test/test_migrations.py index 19929c9..707709e 100644 --- a/matemat/db/test/test_migrations.py +++ b/matemat/db/test/test_migrations.py @@ -134,7 +134,8 @@ class TestMigrations(unittest.TestCase): INSERT INTO users VALUES (1, 'testadmin', 'a@b.c', '$2a$10$herebehashes', NULL, 1, 1, 1337, 0), (2, 'testuser', NULL, '$2a$10$herebehashes', '$2a$10$herebehashes', 0, 1, 4242, 0), - (3, 'alien', NULL, '$2a$10$herebehashes', '$2a$10$herebehashes', 0, 0, 1234, 0) + (3, 'alien', NULL, '$2a$10$herebehashes', '$2a$10$herebehashes', 0, 0, 1234, 0), + (4, 'neverused', NULL, '$2a$10$herebehashes', '$2a$10$herebehashes', 0, 0, 1234, 1234) ''') cursor.execute(''' INSERT INTO products VALUES @@ -167,6 +168,10 @@ class TestMigrations(unittest.TestCase): cursor.execute('''SELECT COUNT(receipt_id) FROM receipts''') self.assertEqual(0, cursor.fetchone()[0]) + # Make sure users.created was populated with the expected values + cursor.execute('''SELECT u.created FROM users AS u ORDER BY u.user_id ASC''') + self.assertEqual([(940,), (941,), (942,), (1174,)], cursor.fetchall()) + # Make sure the modifications table was changed to contain the username, or a fallback cursor.execute('''SELECT agent FROM modifications WHERE ta_id = 2''') self.assertEqual('testadmin', cursor.fetchone()[0]) diff --git a/matemat/db/test/test_wrapper.py b/matemat/db/test/test_wrapper.py index 1a72388..d661c4f 100644 --- a/matemat/db/test/test_wrapper.py +++ b/matemat/db/test/test_wrapper.py @@ -53,12 +53,12 @@ class DatabaseTest(unittest.TestCase): with self.db as db: with db.transaction() as c: c.execute(''' - INSERT INTO users VALUES (1, 'testuser', NULL, 'supersecurepassword', NULL, 1, 1, 0, 42, 0) + INSERT INTO users VALUES (1, 'testuser', NULL, 'supersecurepassword', NULL, 1, 1, 0, 42, 0, 0) ''') c = db._sqlite_db.cursor() c.execute("SELECT * FROM users") user = c.fetchone() - self.assertEqual((1, 'testuser', None, 'supersecurepassword', None, 1, 1, 0, 42, 0), user) + self.assertEqual((1, 'testuser', None, 'supersecurepassword', None, 1, 1, 0, 42, 0, 0), user) def test_transaction_rollback(self) -> None: """ @@ -68,7 +68,7 @@ class DatabaseTest(unittest.TestCase): try: with db.transaction() as c: c.execute(''' - INSERT INTO users VALUES (1, 'testuser', NULL, 'supersecurepassword', NULL, 1, 1, 0, 42, 0) + INSERT INTO users VALUES (1, 'testuser', NULL, 'supersecurepassword', NULL, 1, 1, 0, 42, 0, 0) ''') raise ValueError('This should trigger a rollback') except ValueError as e: