Better modules
All checks were successful
Test before pr merge / test-lint (pull_request) Successful in 6s
Build on push / prepare (push) Successful in 9s
Build on push / core (push) Successful in 18s
Build on push / query (push) Successful in 19s
Build on push / dependency (push) Successful in 17s
Build on push / mail (push) Successful in 16s
Build on push / translation (push) Successful in 16s
Build on push / application (push) Successful in 18s
Build on push / database (push) Successful in 19s
Build on push / auth (push) Successful in 17s
Build on push / api (push) Successful in 14s
All checks were successful
Test before pr merge / test-lint (pull_request) Successful in 6s
Build on push / prepare (push) Successful in 9s
Build on push / core (push) Successful in 18s
Build on push / query (push) Successful in 19s
Build on push / dependency (push) Successful in 17s
Build on push / mail (push) Successful in 16s
Build on push / translation (push) Successful in 16s
Build on push / application (push) Successful in 18s
Build on push / database (push) Successful in 19s
Build on push / auth (push) Successful in 17s
Build on push / api (push) Successful in 14s
This commit is contained in:
@@ -1,36 +1,5 @@
|
||||
import os
|
||||
|
||||
from cpl.application.abc import ApplicationABC as _ApplicationABC
|
||||
from . import mysql as _mysql
|
||||
from . import postgres as _postgres
|
||||
from .database_module import DatabaseModule
|
||||
from .table_manager import TableManager
|
||||
from .logger import DBLogger
|
||||
|
||||
|
||||
def _with_migrations(self: _ApplicationABC, *paths: str | list[str]) -> _ApplicationABC:
|
||||
from cpl.database.service.migration_service import MigrationService
|
||||
|
||||
migration_service = self._services.get_service(MigrationService)
|
||||
migration_service.with_directory(os.path.join(os.path.dirname(os.path.abspath(__file__)), "scripts"))
|
||||
|
||||
if isinstance(paths, str):
|
||||
paths = [paths]
|
||||
|
||||
for path in paths:
|
||||
migration_service.with_directory(path)
|
||||
|
||||
return self
|
||||
|
||||
|
||||
def _with_seeders(self: _ApplicationABC) -> _ApplicationABC:
|
||||
from cpl.database.service.seeder_service import SeederService
|
||||
from cpl.application.host import Host
|
||||
|
||||
seeder_service: SeederService = self._services.get_service(SeederService)
|
||||
Host.run(seeder_service.seed)
|
||||
return self
|
||||
|
||||
|
||||
_ApplicationABC.extend(_ApplicationABC.with_migrations, _with_migrations)
|
||||
_ApplicationABC.extend(_ApplicationABC.with_seeders, _with_seeders)
|
||||
from .table_manager import TableManager
|
||||
|
||||
@@ -1,18 +1,33 @@
|
||||
from cpl.database.model.database_settings import DatabaseSettings
|
||||
from cpl.database.mysql.mysql_module import MySQLModule
|
||||
from cpl.database.postgres.postgres_module import PostgresModule
|
||||
from cpl.dependency.module import Module
|
||||
from cpl.dependency.service_collection import ServiceCollection
|
||||
from cpl.database.schema.executed_migration_dao import ExecutedMigrationDao
|
||||
from cpl.database.service.migration_service import MigrationService
|
||||
from cpl.database.service.seeder_service import SeederService
|
||||
from cpl.dependency.module.module import Module
|
||||
from cpl.dependency.service_provider import ServiceProvider
|
||||
|
||||
|
||||
class DatabaseModule(Module):
|
||||
dependencies = [(MySQLModule, PostgresModule)]
|
||||
config = [DatabaseSettings]
|
||||
singleton = [
|
||||
ExecutedMigrationDao,
|
||||
MigrationService,
|
||||
SeederService,
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def configure(cls, provider: ServiceProvider): ...
|
||||
|
||||
@staticmethod
|
||||
def register(collection: ServiceCollection):
|
||||
from cpl.database.schema import ExecutedMigrationDao
|
||||
def with_migrations(services: ServiceProvider, *paths: str | list[str]):
|
||||
from cpl.database.service.migration_service import MigrationService
|
||||
from cpl.database.service.seeder_service import SeederService
|
||||
|
||||
collection.add_singleton(ExecutedMigrationDao)
|
||||
collection.add_singleton(MigrationService)
|
||||
collection.add_singleton(SeederService)
|
||||
migration_service = services.get_service(MigrationService)
|
||||
|
||||
if isinstance(paths, str):
|
||||
paths = [paths]
|
||||
|
||||
for path in paths:
|
||||
migration_service.with_directory(path)
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
from cpl.core.configuration.configuration import Configuration
|
||||
from cpl.database.abc.db_context_abc import DBContextABC
|
||||
from cpl.database.model.database_settings import DatabaseSettings
|
||||
from cpl.database.model.server_type import ServerTypes, ServerType
|
||||
from cpl.dependency.module import Module
|
||||
from cpl.database.mysql.db_context import DBContext
|
||||
from cpl.dependency.module.module import Module
|
||||
from cpl.dependency.service_collection import ServiceCollection
|
||||
|
||||
|
||||
class MySQLModule(Module):
|
||||
dependencies = []
|
||||
config = [DatabaseSettings]
|
||||
singleton = [(DBContextABC, DBContext)]
|
||||
|
||||
@staticmethod
|
||||
def register(collection: ServiceCollection):
|
||||
from cpl.database.abc.db_context_abc import DBContextABC
|
||||
from cpl.database.mysql.db_context import DBContext
|
||||
|
||||
ServerType.set_server_type(ServerTypes(ServerTypes.MYSQL.value))
|
||||
Configuration.set("DB_DEFAULT_PORT", 3306)
|
||||
|
||||
collection.add_singleton(DBContextABC, DBContext)
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
from cpl.core.configuration.configuration import Configuration
|
||||
from cpl.database.abc.db_context_abc import DBContextABC
|
||||
from cpl.database.model.database_settings import DatabaseSettings
|
||||
from cpl.database.model.server_type import ServerTypes, ServerType
|
||||
from cpl.dependency.module import Module
|
||||
from cpl.database.postgres.db_context import DBContext
|
||||
from cpl.dependency.module.module import Module
|
||||
from cpl.dependency.service_collection import ServiceCollection
|
||||
|
||||
|
||||
class PostgresModule(Module):
|
||||
dependencies = []
|
||||
config = [DatabaseSettings]
|
||||
singleton = [(DBContextABC, DBContext)]
|
||||
|
||||
@staticmethod
|
||||
def register(collection: ServiceCollection):
|
||||
from cpl.database.abc.db_context_abc import DBContextABC
|
||||
from cpl.database.postgres.db_context import DBContext
|
||||
|
||||
ServerType.set_server_type(ServerTypes(ServerTypes.POSTGRES.value))
|
||||
Configuration.set("DB_DEFAULT_PORT", 5432)
|
||||
|
||||
collection.add_singleton(DBContextABC, DBContext)
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
from .seeder_service import SeederService
|
||||
from .migration_service import MigrationService
|
||||
|
||||
@@ -7,13 +7,12 @@ from cpl.database.model.migration import Migration
|
||||
from cpl.database.model.server_type import ServerType, ServerTypes
|
||||
from cpl.database.schema.executed_migration import ExecutedMigration
|
||||
from cpl.database.schema.executed_migration_dao import ExecutedMigrationDao
|
||||
from cpl.dependency.hosted.startup_task import StartupTask
|
||||
from cpl.dependency.hosted import StartupTask
|
||||
|
||||
|
||||
class MigrationService(StartupTask):
|
||||
|
||||
def __init__(self, logger: DBLogger, db: DBContextABC, executed_migration_dao: ExecutedMigrationDao):
|
||||
StartupTask.__init__(self)
|
||||
self._logger = logger
|
||||
self._db = db
|
||||
self._executed_migration_dao = executed_migration_dao
|
||||
@@ -24,12 +23,23 @@ class MigrationService(StartupTask):
|
||||
self.with_directory(os.path.join(os.path.dirname(os.path.realpath(__file__)), "../scripts/postgres"))
|
||||
elif ServerType.server_type == ServerTypes.MYSQL:
|
||||
self.with_directory(os.path.join(os.path.dirname(os.path.realpath(__file__)), "../scripts/mysql"))
|
||||
else:
|
||||
raise Exception("Unsupported database type")
|
||||
|
||||
async def run(self):
|
||||
await self._execute(self._load_scripts())
|
||||
|
||||
def with_directory(self, directory: str) -> "MigrationService":
|
||||
self._script_directories.append(directory)
|
||||
cpl_rel_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../../..")
|
||||
cpl_abs_path = os.path.abspath(cpl_rel_path)
|
||||
|
||||
if directory.startswith(cpl_abs_path) or os.path.abspath(directory).startswith(cpl_abs_path):
|
||||
if len(self._script_directories) > 0:
|
||||
self._script_directories.insert(1, directory)
|
||||
else:
|
||||
self._script_directories.append(directory)
|
||||
else:
|
||||
self._script_directories.append(directory)
|
||||
return self
|
||||
|
||||
async def _get_migration_history(self) -> list[ExecutedMigration]:
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
from cpl.database.abc.data_seeder_abc import DataSeederABC
|
||||
from cpl.database.logger import DBLogger
|
||||
from cpl.dependency import ServiceProvider
|
||||
from cpl.dependency.hosted import StartupTask
|
||||
|
||||
|
||||
class SeederService:
|
||||
class SeederService(StartupTask):
|
||||
|
||||
def __init__(self, provider: ServiceProvider):
|
||||
StartupTask.__init__(self)
|
||||
self._provider = provider
|
||||
self._logger = provider.get_service(DBLogger)
|
||||
|
||||
async def seed(self):
|
||||
async def run(self):
|
||||
seeders = self._provider.get_services(DataSeederABC)
|
||||
self._logger.debug(f"Found {len(seeders)} seeders")
|
||||
for seeder in seeders:
|
||||
|
||||
Reference in New Issue
Block a user