Modularization
Some checks failed
Test before pr merge / test-lint (pull_request) Failing after 7s
Build on push / prepare (push) Successful in 10s
Build on push / core (push) Successful in 18s
Build on push / query (push) Successful in 17s
Build on push / dependency (push) Successful in 17s
Build on push / application (push) Successful in 16s
Build on push / mail (push) Successful in 15s
Build on push / database (push) Successful in 15s
Build on push / translation (push) Successful in 18s
Build on push / auth (push) Successful in 23s
Build on push / api (push) Successful in 16s
Some checks failed
Test before pr merge / test-lint (pull_request) Failing after 7s
Build on push / prepare (push) Successful in 10s
Build on push / core (push) Successful in 18s
Build on push / query (push) Successful in 17s
Build on push / dependency (push) Successful in 17s
Build on push / application (push) Successful in 16s
Build on push / mail (push) Successful in 15s
Build on push / database (push) Successful in 15s
Build on push / translation (push) Successful in 18s
Build on push / auth (push) Successful in 23s
Build on push / api (push) Successful in 16s
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
import os
|
||||
from typing import Type
|
||||
|
||||
from cpl.application.abc import ApplicationABC as _ApplicationABC
|
||||
from cpl.dependency import ServiceCollection as _ServiceCollection
|
||||
from . import mysql as _mysql
|
||||
from . import postgres as _postgres
|
||||
from .table_manager import TableManager
|
||||
@@ -32,43 +30,5 @@ def _with_seeders(self: _ApplicationABC) -> _ApplicationABC:
|
||||
return self
|
||||
|
||||
|
||||
def _add(collection: _ServiceCollection, db_context: Type, default_port: int, server_type: str):
|
||||
from cpl.core.console import Console
|
||||
from cpl.core.configuration import Configuration
|
||||
from cpl.database.abc.db_context_abc import DBContextABC
|
||||
from cpl.database.model.server_type import ServerTypes, ServerType
|
||||
from cpl.database.model.database_settings import DatabaseSettings
|
||||
from cpl.database.service.migration_service import MigrationService
|
||||
from cpl.database.service.seeder_service import SeederService
|
||||
from cpl.database.schema.executed_migration_dao import ExecutedMigrationDao
|
||||
|
||||
try:
|
||||
ServerType.set_server_type(ServerTypes(server_type))
|
||||
Configuration.set("DB_DEFAULT_PORT", default_port)
|
||||
|
||||
collection.add_singleton(DBContextABC, db_context)
|
||||
collection.add_singleton(ExecutedMigrationDao)
|
||||
collection.add_singleton(MigrationService)
|
||||
collection.add_singleton(SeederService)
|
||||
except ImportError as e:
|
||||
Console.error("cpl-database is not installed", str(e))
|
||||
|
||||
|
||||
def add_mysql(collection: _ServiceCollection):
|
||||
from cpl.database.mysql.db_context import DBContext
|
||||
from cpl.database.model import ServerTypes
|
||||
|
||||
_add(collection, DBContext, 3306, ServerTypes.MYSQL.value)
|
||||
|
||||
|
||||
def add_postgres(collection: _ServiceCollection):
|
||||
from cpl.database.mysql.db_context import DBContext
|
||||
from cpl.database.model import ServerTypes
|
||||
|
||||
_add(collection, DBContext, 5432, ServerTypes.POSTGRES.value)
|
||||
|
||||
|
||||
_ServiceCollection.with_module(add_mysql, _mysql.__name__)
|
||||
_ServiceCollection.with_module(add_postgres, _postgres.__name__)
|
||||
_ApplicationABC.extend(_ApplicationABC.with_migrations, _with_migrations)
|
||||
_ApplicationABC.extend(_ApplicationABC.with_seeders, _with_seeders)
|
||||
|
||||
22
src/cpl-database/cpl/database/database_module.py
Normal file
22
src/cpl-database/cpl/database/database_module.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from cpl.core.errors import module_dependency_error
|
||||
from cpl.database.model.server_type import ServerType
|
||||
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 import Module, TModule
|
||||
from cpl.dependency.service_collection import ServiceCollection
|
||||
|
||||
|
||||
class DatabaseModule(Module):
|
||||
@staticmethod
|
||||
def dependencies() -> list[TModule]:
|
||||
if not ServerType.has_server_type:
|
||||
module_dependency_error(__name__, "MySQLModule or PostgresModule")
|
||||
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
def register(collection: ServiceCollection):
|
||||
collection.add_singleton(ExecutedMigrationDao)
|
||||
collection.add_singleton(MigrationService)
|
||||
collection.add_singleton(SeederService)
|
||||
@@ -15,6 +15,11 @@ class ServerType:
|
||||
assert isinstance(server_type, ServerTypes), f"Expected ServerType but got {type(server_type)}"
|
||||
cls._server_type = server_type
|
||||
|
||||
@classmethod
|
||||
@property
|
||||
def has_server_type(cls) -> bool:
|
||||
return cls._server_type is not None
|
||||
|
||||
@classmethod
|
||||
@property
|
||||
def server_type(cls) -> ServerTypes:
|
||||
|
||||
19
src/cpl-database/cpl/database/mysql/mysql_module.py
Normal file
19
src/cpl-database/cpl/database/mysql/mysql_module.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from cpl.core.configuration.configuration import Configuration
|
||||
from cpl.database.abc.db_context_abc import DBContextABC
|
||||
from cpl.database.model.server_type import ServerTypes, ServerType
|
||||
from cpl.database.mysql.db_context import DBContext
|
||||
from cpl.dependency.module import Module, TModule
|
||||
from cpl.dependency.service_collection import ServiceCollection
|
||||
|
||||
|
||||
class MySQLModule(Module):
|
||||
@staticmethod
|
||||
def dependencies() -> list[TModule]:
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
def register(collection: ServiceCollection):
|
||||
ServerType.set_server_type(ServerTypes(ServerTypes.MYSQL.value))
|
||||
Configuration.set("DB_DEFAULT_PORT", 3306)
|
||||
|
||||
collection.add_singleton(DBContextABC, DBContext)
|
||||
20
src/cpl-database/cpl/database/postgres/postgres_module.py
Normal file
20
src/cpl-database/cpl/database/postgres/postgres_module.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from cpl.core.configuration.configuration import Configuration
|
||||
from cpl.database.abc.db_context_abc import DBContextABC
|
||||
from cpl.database.database_module import DatabaseModule
|
||||
from cpl.database.model.server_type import ServerTypes, ServerType
|
||||
from cpl.database.postgres.db_context import DBContext
|
||||
from cpl.dependency.module import Module, TModule
|
||||
from cpl.dependency.service_collection import ServiceCollection
|
||||
|
||||
|
||||
class PostgresModule(Module):
|
||||
@staticmethod
|
||||
def dependencies() -> list[TModule]:
|
||||
return [DatabaseModule]
|
||||
|
||||
@staticmethod
|
||||
def register(collection: ServiceCollection):
|
||||
ServerType.set_server_type(ServerTypes(ServerTypes.POSTGRES.value))
|
||||
Configuration.set("DB_DEFAULT_PORT", 5432)
|
||||
|
||||
collection.add_singleton(DBContextABC, DBContext)
|
||||
Reference in New Issue
Block a user