#428 #437

Merged
edraft merged 20 commits from #428 into master 2023-11-15 17:39:44 +01:00
8 changed files with 92 additions and 14 deletions
Showing only changes of commit 1f2fbc362f - Show all commits

View File

@ -3,7 +3,6 @@ from datetime import datetime
from cpl_core.application.application_extension_abc import ApplicationExtensionABC
from cpl_core.configuration import ConfigurationABC
from cpl_core.database.context import DatabaseContextABC
from cpl_core.dependency_injection import ServiceProviderABC
from cpl_core.logging import LoggerABC

View File

@ -12,4 +12,4 @@ class Application(ApplicationABC):
pass
async def main(self):
Console.write_line("Hello World")
Console.write_line("Finished")

View File

@ -4,9 +4,9 @@ from cpl_core.application import ApplicationBuilder
from bot_data.startup_migration_extension import StartupMigrationExtension
from migration_to_sql.application import Application
from migration_to_sql.mock.database_extension import DatabaseExtension
from migration_to_sql.startup import Startup
from migration_to_sql.startup_mock_extension import StartupMockExtension
from modules.database.database_extension import DatabaseExtension
async def main():

View File

@ -0,0 +1,17 @@
from datetime import datetime
from cpl_core.application.application_extension_abc import ApplicationExtensionABC
from cpl_core.configuration import ConfigurationABC
from cpl_core.dependency_injection import ServiceProviderABC
from migration_to_sql.mock.migration_service import MigrationService
class DatabaseExtension(ApplicationExtensionABC):
def __init__(self):
pass
async def run(self, config: ConfigurationABC, services: ServiceProviderABC):
config.add_configuration("Database_StartTime", str(datetime.now()))
migrations: MigrationService = services.get_service(MigrationService)
migrations.migrate()

View File

@ -0,0 +1,36 @@
from cpl_core.console import Console
from cpl_core.dependency_injection import ServiceProviderABC
from cpl_query.extension import List
from bot_data.abc.migration_abc import MigrationABC
from bot_data.model.migration_history import MigrationHistory
from migration_to_sql.mock.mock_db_context import MockDBContext
class MigrationService:
def __init__(
self,
services: ServiceProviderABC,
db: MockDBContext,
):
self._services = services
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)
)
def migrate(self):
for migration in self._migrations:
migration_id = migration.__name__
try:
migration_as_service: MigrationABC = self._services.get_service(migration)
self._db.set_migration(migration_as_service.name)
migration_as_service.upgrade()
self._cursor.execute(MigrationHistory(migration_id).insert_string)
self._db.save_changes()
except Exception as e:
Console.error(e)

View File

@ -7,9 +7,3 @@ class MockCursor(MySQLCursorBuffered):
def fetchone(self, *args, **kwargs):
pass
def execute(self, query: str, *args, **kwargs):
if "MigrationHistory" in query:
return None
else:
pass

View File

@ -1,23 +1,53 @@
import os
import textwrap
from typing import Optional
from cpl_core.database import DatabaseSettings
from cpl_core.database.context import DatabaseContextABC
from bot_core.logging.database_logger import DatabaseLogger
from migration_to_sql.mock.mock_cursor import MockCursor
class MockDBContext(DatabaseContextABC):
def __init__(self):
DatabaseContextABC.__init__(self)
self._migration_version: Optional[str] = None
self._migration_name: Optional[str] = None
self._migration_script: Optional[str] = None
@property
def cursor(self) -> MockCursor:
return MockCursor()
cursor = MockCursor()
cursor.execute = self.execute
return cursor
def set_migration(self, name: str):
self._migration_name = name.split("_")[1].replace("Migration", "")
self._migration_version = name.split("_")[0]
self._migration_script = ""
def connect(self, database_settings: DatabaseSettings):
pass
def save_changes(self):
pass
def select(self, statement: str) -> list[tuple]:
pass
def execute(self, query: str, *args, **kwargs):
if "MigrationHistory" in query:
return None
else:
self._migration_script += f"{textwrap.dedent(query)}\n\n"
def save_changes(self):
path = f"../../src/bot_data/scripts/{self._migration_version}"
if not os.path.exists(path):
os.makedirs(path)
script = textwrap.dedent(self._migration_script)
with open(f"{path}/{self._migration_name}.sql", "w+") as f:
f.write(script)
f.close()
self._migration_name = None
self._migration_version = None
self._migration_script = None

View File

@ -10,6 +10,7 @@ from bot_core.abc.custom_file_logger_abc import CustomFileLoggerABC
from bot_core.configuration.bot_logging_settings import BotLoggingSettings
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_core.logging.database_logger import DatabaseLogger
from migration_to_sql.mock.migration_service import MigrationService
class Startup(StartupABC):
@ -31,6 +32,7 @@ class Startup(StartupABC):
) -> ServiceProviderABC:
services.add_logging()
services.add_singleton(CustomFileLoggerABC, DatabaseLogger)
services.add_transient(MigrationService)
return services.build_service_provider()
@staticmethod