Added logic to handle down and upgrades #428

This commit is contained in:
Sven Heidemann 2023-11-10 13:19:23 +01:00
parent 1f2fbc362f
commit 4628f31993
2 changed files with 10 additions and 3 deletions

View File

@ -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)

View File

@ -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 = ""