From c496049b4cb6598133e6a7e2b6c8eed053bc4f06 Mon Sep 17 00:00:00 2001 From: Jonathan Wu Date: Mon, 20 Dec 2021 23:20:19 -0500 Subject: [PATCH] add migration script --- src/migrate.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/migrate.py b/src/migrate.py index 01c97bd..7def769 100644 --- a/src/migrate.py +++ b/src/migrate.py @@ -3,7 +3,7 @@ import sys msg = """Before migrating, please confirm the following: - - You are on v2.4.0 to v2.4.3 (older version please update to one of these first, new version no migrate necessary) + - You are on v3.0.0 to v3.0.1 (older version please update to one of these first, new version no migrate necessary) - You have made a backup of the database - You have write permissions in the current directory Please note that migration is a one-way operation, and you will not be able to revert to the previous version without a backup. @@ -16,7 +16,19 @@ db = cs50.SQL("sqlite:///database.db") -## TODO: add missing submissions from contest problem exports -db.execute("ALTER TABLE contest_users ADD COLUMN 'hidden' integer NOT NULL DEFAULT(0)") +db.execute('ALTER TABLE contest_users ADD COLUMN "hidden" integer NOT NULL DEFAULT(0)') + +exportable = db.execute('SELECT * FROM submissions WHERE contest_id IS NOT NULL') +problem_ids = set([e['id'] for e in db.execute('SELECT id FROM problems')]) +i = 1 +for sub in exportable: + print(f'Duplicating submission {i} of {len(exportable)}') + i += 1 + exported_id = sub['contest_id'] + '-' + sub['problem_id'] + if exported_id not in problem_ids: + continue + db.execute(('INSERT INTO submissions(date, user_id, problem_id, correct, submitted) ' + 'VALUES(?, ?, ?, ?, ?)'), sub['date'], sub['user_id'], exported_id, + sub['correct'], sub['submitted']) print('Migration completed.')