Updated and added script order #428
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
import os
|
||||
import shutil
|
||||
|
||||
from cpl_core.console import Console
|
||||
from cpl_core.dependency_injection import ServiceProviderABC
|
||||
from cpl_query.extension import List
|
||||
@@ -18,24 +21,31 @@ class MigrationService:
|
||||
self._db = db
|
||||
self._cursor = db.cursor
|
||||
|
||||
self._migrations: List[MigrationABC] = (
|
||||
List(type, MigrationABC.__subclasses__()).order_by(lambda x: x.name.split("_")[0]).then_by(lambda x: x.prio)
|
||||
)
|
||||
# self._migrations: List[MigrationABC] = (
|
||||
# List(type, MigrationABC.__subclasses__()).order_by(lambda x: x.name.split("_")[0]).then_by(lambda x: x.prio)
|
||||
# )
|
||||
|
||||
def migrate(self):
|
||||
for migration in self._migrations:
|
||||
migration_id = migration.__name__
|
||||
path = f"../../src/bot_data/scripts"
|
||||
if not os.path.exists(path):
|
||||
os.makedirs(path)
|
||||
else:
|
||||
shutil.rmtree(path)
|
||||
os.makedirs(path)
|
||||
|
||||
for migration in self._services.get_services(MigrationABC):
|
||||
migration_id = type(migration).__name__
|
||||
try:
|
||||
migration_as_service: MigrationABC = self._services.get_service(migration)
|
||||
# migration_as_service: MigrationABC = self._services.get_service(migration)
|
||||
# save upgrade scripts
|
||||
self._db.set_migration(migration_as_service.name, True)
|
||||
migration_as_service.upgrade()
|
||||
self._db.set_migration(migration.name, True)
|
||||
migration.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._db.set_migration(migration.name)
|
||||
migration.downgrade()
|
||||
self._cursor.execute(MigrationHistory(migration_id).insert_string)
|
||||
self._db.save_changes()
|
||||
|
||||
|
@@ -15,6 +15,11 @@ class MockDBContext(DatabaseContextABC):
|
||||
self._migration_name: Optional[str] = None
|
||||
self._migration_script: Optional[str] = None
|
||||
|
||||
self._old_version: Optional[str] = None
|
||||
self._old_name: Optional[str] = None
|
||||
|
||||
self._index: int = 0
|
||||
|
||||
@property
|
||||
def cursor(self) -> MockCursor:
|
||||
cursor = MockCursor()
|
||||
@@ -43,11 +48,25 @@ class MockDBContext(DatabaseContextABC):
|
||||
if not os.path.exists(path):
|
||||
os.makedirs(path)
|
||||
|
||||
if (
|
||||
self._old_name is not None
|
||||
and self._migration_name is not None
|
||||
and self._old_name.split("_")[0] == self._migration_name.split("_")[0]
|
||||
):
|
||||
pass
|
||||
elif self._old_version == self._migration_version:
|
||||
self._index += 1
|
||||
else:
|
||||
self._index = 1
|
||||
|
||||
script = textwrap.dedent(self._migration_script)
|
||||
with open(f"{path}/{self._migration_name}.sql", "w+") as f:
|
||||
with open(f"{path}/{self._index}_{self._migration_name}.sql", "w+") as f:
|
||||
f.write(script)
|
||||
f.close()
|
||||
|
||||
self._old_version = self._migration_version
|
||||
self._old_name = self._migration_name
|
||||
|
||||
self._migration_name = None
|
||||
self._migration_version = None
|
||||
self._migration_script = None
|
||||
|
Reference in New Issue
Block a user