From ff0c13d3676677264d8c6a803d97e551905c3ea6 Mon Sep 17 00:00:00 2001 From: s3lph Date: Sat, 1 Sep 2018 02:54:55 +0200 Subject: [PATCH] Yet another schema change: consumptions now contains the product name rather than the product id. --- matemat/db/facade.py | 6 +++--- matemat/db/migrations.py | 19 +++++++++++++++++++ matemat/db/schemas.py | 6 ++---- matemat/db/test/test_migrations.py | 21 +++++++++++++++++---- 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/matemat/db/facade.py b/matemat/db/facade.py index 34211c6..8e9fe08 100644 --- a/matemat/db/facade.py +++ b/matemat/db/facade.py @@ -472,10 +472,10 @@ class MatematDatabase(object): 'old_balance': user.balance }) c.execute(''' - INSERT INTO consumptions (ta_id, product_id) - VALUES (last_insert_rowid(), :product_id) + INSERT INTO consumptions (ta_id, product) + VALUES (last_insert_rowid(), :product) ''', { - 'product_id': product.id + 'product': product.name }) # Subtract the price from the user's account balance. c.execute(''' diff --git a/matemat/db/migrations.py b/matemat/db/migrations.py index 34416d3..cad2461 100644 --- a/matemat/db/migrations.py +++ b/matemat/db/migrations.py @@ -135,6 +135,25 @@ def migrate_schema_2_to_3(c: sqlite3.Cursor): c.execute('DROP TABLE transactions') c.execute('ALTER TABLE transactions_new RENAME TO transactions') + # Change consumptions table + c.execute(''' + CREATE TABLE consumptions_new ( + ta_id INTEGER PRIMARY KEY, + product TEXT NOT NULL, + FOREIGN KEY (ta_id) REFERENCES transactions(ta_id) + ON DELETE CASCADE ON UPDATE CASCADE + ) + ''') + c.execute(''' + INSERT INTO consumptions_new (ta_id, product) + SELECT c.ta_id, COALESCE(p.name, '') + FROM consumptions as c + LEFT JOIN products as p + ON c.product_id = p.product_id + ''') + c.execute('DROP TABLE consumptions') + c.execute('ALTER TABLE consumptions_new RENAME TO consumptions') + # Change modifications table c.execute(''' CREATE TABLE modifications_new ( diff --git a/matemat/db/schemas.py b/matemat/db/schemas.py index 999e4ff..4907f39 100644 --- a/matemat/db/schemas.py +++ b/matemat/db/schemas.py @@ -140,11 +140,9 @@ SCHEMAS[3] = [ ''' CREATE TABLE consumptions ( -- transactions involving buying a product ta_id INTEGER PRIMARY KEY, - product_id INTEGER DEFAULT NULL, + product TEXT NOT 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 + ON DELETE CASCADE ON UPDATE CASCADE ); ''', ''' diff --git a/matemat/db/test/test_migrations.py b/matemat/db/test/test_migrations.py index bbc6fb2..19929c9 100644 --- a/matemat/db/test/test_migrations.py +++ b/matemat/db/test/test_migrations.py @@ -52,7 +52,10 @@ class TestMigrations(unittest.TestCase): cursor.execute('PRAGMA user_version = 1') # Kick off the migration + schema_version = self.db.SCHEMA_VERSION + self.db.SCHEMA_VERSION = 2 self.db._setup() + self.db.SCHEMA_VERSION = schema_version # Test whether the new tables were created cursor.execute('PRAGMA table_info(transactions)') @@ -136,25 +139,29 @@ class TestMigrations(unittest.TestCase): cursor.execute(''' INSERT INTO products VALUES (1, 'Club Mate', 42, 200, 250), - (2, 'Flora Power Mate (1/4l)', 10, 100, 150) + (2, 'Flora Power Mate', 10, 100, 150) ''') cursor.execute(''' INSERT INTO transactions VALUES (1, 1, 4200, 0, 1000), -- deposit (2, 2, 1337, 0, 1001), -- modification - (3, 3, 1337, 0, 1001), -- modification with deleted agent - (4, 2, 200, 1337, 1002) -- consumption + (3, 3, 1337, 0, 1002), -- modification with deleted agent + (4, 2, -200, 1337, 1003), -- consumption + (5, 3, -200, 1337, 1004) -- consumption with deleted product ''') cursor.execute('''INSERT INTO deposits VALUES (1)''') cursor.execute(''' INSERT INTO modifications VALUES (2, 1, 'Account migration'), (3, 42, 'You can''t find out who i am... MUAHAHAHA!!!')''') - cursor.execute('''INSERT INTO consumptions VALUES (4, 2)''') + cursor.execute('''INSERT INTO consumptions VALUES (4, 2), (5, 42)''') cursor.execute('''PRAGMA user_version = 2''') # Kick off the migration + schema_version = self.db.SCHEMA_VERSION + self.db.SCHEMA_VERSION = 3 self.db._setup() + self.db.SCHEMA_VERSION = schema_version # Make sure the receipts table was created cursor.execute('''SELECT COUNT(receipt_id) FROM receipts''') @@ -165,3 +172,9 @@ class TestMigrations(unittest.TestCase): self.assertEqual('testadmin', cursor.fetchone()[0]) cursor.execute('''SELECT agent FROM modifications WHERE ta_id = 3''') self.assertEqual('', cursor.fetchone()[0]) + + # Make sure the consumptions table was changed to contain the product name, or a fallback + cursor.execute('''SELECT product FROM consumptions WHERE ta_id = 4''') + self.assertEqual('Flora Power Mate', cursor.fetchone()[0]) + cursor.execute('''SELECT product FROM consumptions WHERE ta_id = 5''') + self.assertEqual('', cursor.fetchone()[0])