From 4628f319932bd299050f5455282664912677cfba Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 10 Nov 2023 13:19:23 +0100 Subject: [PATCH] Added logic to handle down and upgrades #428 --- bot/tools/migration_to_sql/mock/migration_service.py | 9 ++++++++- bot/tools/migration_to_sql/mock/mock_db_context.py | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/bot/tools/migration_to_sql/mock/migration_service.py b/bot/tools/migration_to_sql/mock/migration_service.py index 5137e4b7..9843f15e 100644 --- a/bot/tools/migration_to_sql/mock/migration_service.py +++ b/bot/tools/migration_to_sql/mock/migration_service.py @@ -27,10 +27,17 @@ class MigrationService: migration_id = migration.__name__ try: migration_as_service: MigrationABC = self._services.get_service(migration) - self._db.set_migration(migration_as_service.name) + # save upgrade scripts + self._db.set_migration(migration_as_service.name, True) migration_as_service.upgrade() self._cursor.execute(MigrationHistory(migration_id).insert_string) self._db.save_changes() + # save downgrade scripts + self._db.set_migration(migration_as_service.name) + migration_as_service.downgrade() + self._cursor.execute(MigrationHistory(migration_id).insert_string) + self._db.save_changes() + except Exception as e: Console.error(e) diff --git a/bot/tools/migration_to_sql/mock/mock_db_context.py b/bot/tools/migration_to_sql/mock/mock_db_context.py index 25f38c15..aa898700 100644 --- a/bot/tools/migration_to_sql/mock/mock_db_context.py +++ b/bot/tools/migration_to_sql/mock/mock_db_context.py @@ -21,8 +21,8 @@ class MockDBContext(DatabaseContextABC): cursor.execute = self.execute return cursor - def set_migration(self, name: str): - self._migration_name = name.split("_")[1].replace("Migration", "") + def set_migration(self, name: str, upgrade: bool = False): + self._migration_name = f'{name.split("_")[1].replace("Migration", "")}_{"up" if upgrade else "down"}' self._migration_version = name.split("_")[0] self._migration_script = ""