#428 #437
@ -21,7 +21,8 @@
|
||||
"checks": "tools/checks/checks.json",
|
||||
"get-version": "tools/get_version/get-version.json",
|
||||
"post-build": "tools/post_build/post-build.json",
|
||||
"set-version": "tools/set_version/set-version.json"
|
||||
"set-version": "tools/set_version/set-version.json",
|
||||
"migration-to-sql": "tools/migration_to_sql/tools/migration-to-sql.json"
|
||||
},
|
||||
"Scripts": {
|
||||
"format": "black ./",
|
||||
|
@ -3,6 +3,7 @@ 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
|
||||
|
||||
|
1
bot/tools/migration_to_sql/__init__.py
Normal file
1
bot/tools/migration_to_sql/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
# imports
|
15
bot/tools/migration_to_sql/application.py
Normal file
15
bot/tools/migration_to_sql/application.py
Normal file
@ -0,0 +1,15 @@
|
||||
from cpl_core.application import ApplicationABC
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.console import Console
|
||||
from cpl_core.dependency_injection import ServiceProviderABC
|
||||
|
||||
|
||||
class Application(ApplicationABC):
|
||||
def __init__(self, config: ConfigurationABC, services: ServiceProviderABC):
|
||||
ApplicationABC.__init__(self, config, services)
|
||||
|
||||
async def configure(self):
|
||||
pass
|
||||
|
||||
async def main(self):
|
||||
Console.write_line("Hello World")
|
22
bot/tools/migration_to_sql/appsettings.json
Normal file
22
bot/tools/migration_to_sql/appsettings.json
Normal file
@ -0,0 +1,22 @@
|
||||
{
|
||||
"TimeFormatSettings": {
|
||||
"DateFormat": "%Y-%m-%d",
|
||||
"TimeFormat": "%H:%M:%S",
|
||||
"DateTimeFormat": "%Y-%m-%d %H:%M:%S.%f",
|
||||
"DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S"
|
||||
},
|
||||
"LoggingSettings": {
|
||||
"Path": "logs/",
|
||||
"Filename": "log_$start_time.log",
|
||||
"ConsoleLogLevel": "ERROR",
|
||||
"FileLogLevel": "WARN"
|
||||
},
|
||||
"BotLoggingSettings": {
|
||||
"Database": {
|
||||
"Path": "logs/$date_now/",
|
||||
"Filename": "database.log",
|
||||
"ConsoleLogLevel": "ERROR",
|
||||
"FileLogLevel": "INFO"
|
||||
}
|
||||
}
|
||||
}
|
26
bot/tools/migration_to_sql/main.py
Normal file
26
bot/tools/migration_to_sql/main.py
Normal file
@ -0,0 +1,26 @@
|
||||
import asyncio
|
||||
|
||||
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.startup import Startup
|
||||
from migration_to_sql.startup_mock_extension import StartupMockExtension
|
||||
from modules.database.database_extension import DatabaseExtension
|
||||
|
||||
|
||||
async def main():
|
||||
app_builder = (
|
||||
ApplicationBuilder(Application)
|
||||
.use_extension(StartupMockExtension)
|
||||
.use_extension(StartupMigrationExtension)
|
||||
.use_extension(DatabaseExtension)
|
||||
.use_startup(Startup)
|
||||
)
|
||||
|
||||
app: Application = await app_builder.build_async()
|
||||
await app.run_async()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
46
bot/tools/migration_to_sql/migration-to-sql.json
Normal file
46
bot/tools/migration_to_sql/migration-to-sql.json
Normal file
@ -0,0 +1,46 @@
|
||||
{
|
||||
"ProjectSettings": {
|
||||
"Name": "migration-to-sql",
|
||||
"Version": {
|
||||
"Major": "0",
|
||||
"Minor": "0",
|
||||
"Micro": "0"
|
||||
},
|
||||
"Author": "",
|
||||
"AuthorEmail": "",
|
||||
"Description": "",
|
||||
"LongDescription": "",
|
||||
"URL": "",
|
||||
"CopyrightDate": "",
|
||||
"CopyrightName": "",
|
||||
"LicenseName": "",
|
||||
"LicenseDescription": "",
|
||||
"Dependencies": [
|
||||
"cpl-core>=2023.10.0"
|
||||
],
|
||||
"DevDependencies": [
|
||||
"cpl-cli>=2023.4.0.post3"
|
||||
],
|
||||
"PythonVersion": ">=3.10.4",
|
||||
"PythonPath": {
|
||||
"linux": ""
|
||||
},
|
||||
"Classifiers": []
|
||||
},
|
||||
"BuildSettings": {
|
||||
"ProjectType": "console",
|
||||
"SourcePath": "",
|
||||
"OutputPath": "../../dist",
|
||||
"Main": "migration_to_sql.main",
|
||||
"EntryPoint": "migration-to-sql",
|
||||
"IncludePackageData": false,
|
||||
"Included": [],
|
||||
"Excluded": [
|
||||
"*/__pycache__",
|
||||
"*/logs",
|
||||
"*/tests"
|
||||
],
|
||||
"PackageData": {},
|
||||
"ProjectReferences": []
|
||||
}
|
||||
}
|
0
bot/tools/migration_to_sql/mock/__init__.py
Normal file
0
bot/tools/migration_to_sql/mock/__init__.py
Normal file
15
bot/tools/migration_to_sql/mock/mock_cursor.py
Normal file
15
bot/tools/migration_to_sql/mock/mock_cursor.py
Normal file
@ -0,0 +1,15 @@
|
||||
from mysql.connector.cursor import MySQLCursorBuffered
|
||||
|
||||
|
||||
class MockCursor(MySQLCursorBuffered):
|
||||
def __init__(self):
|
||||
MySQLCursorBuffered.__init__(self)
|
||||
|
||||
def fetchone(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def execute(self, query: str, *args, **kwargs):
|
||||
if "MigrationHistory" in query:
|
||||
return None
|
||||
else:
|
||||
pass
|
23
bot/tools/migration_to_sql/mock/mock_db_context.py
Normal file
23
bot/tools/migration_to_sql/mock/mock_db_context.py
Normal file
@ -0,0 +1,23 @@
|
||||
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)
|
||||
|
||||
@property
|
||||
def cursor(self) -> MockCursor:
|
||||
return MockCursor()
|
||||
|
||||
def connect(self, database_settings: DatabaseSettings):
|
||||
pass
|
||||
|
||||
def save_changes(self):
|
||||
pass
|
||||
|
||||
def select(self, statement: str) -> list[tuple]:
|
||||
pass
|
45
bot/tools/migration_to_sql/startup.py
Normal file
45
bot/tools/migration_to_sql/startup.py
Normal file
@ -0,0 +1,45 @@
|
||||
from typing import Optional, Type, Callable
|
||||
|
||||
from cpl_core.application import StartupABC
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.dependency_injection import ServiceCollectionABC
|
||||
from cpl_core.dependency_injection import ServiceProviderABC
|
||||
from cpl_core.environment import ApplicationEnvironment
|
||||
|
||||
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
|
||||
|
||||
|
||||
class Startup(StartupABC):
|
||||
def __init__(self):
|
||||
StartupABC.__init__(self)
|
||||
|
||||
def configure_configuration(
|
||||
self, configuration: ConfigurationABC, environment: ApplicationEnvironment
|
||||
) -> ConfigurationABC:
|
||||
configuration.add_json_file("appsettings.json")
|
||||
configuration.add_configuration(FeatureFlagsSettings, FeatureFlagsSettings())
|
||||
self._configure_settings_with_sub_settings(
|
||||
configuration, BotLoggingSettings, lambda x: x.files, lambda x: x.key
|
||||
)
|
||||
return configuration
|
||||
|
||||
def configure_services(
|
||||
self, services: ServiceCollectionABC, environment: ApplicationEnvironment
|
||||
) -> ServiceProviderABC:
|
||||
services.add_logging()
|
||||
services.add_singleton(CustomFileLoggerABC, DatabaseLogger)
|
||||
return services.build_service_provider()
|
||||
|
||||
@staticmethod
|
||||
def _configure_settings_with_sub_settings(
|
||||
config: ConfigurationABC, settings_type: Type, list_atr: Callable, atr: Callable
|
||||
):
|
||||
settings: Optional[settings_type] = config.get_configuration(settings_type)
|
||||
if settings is None:
|
||||
return
|
||||
|
||||
for sub_settings in list_atr(settings):
|
||||
config.add_configuration(f"{type(sub_settings).__name__}_{atr(sub_settings)}", sub_settings)
|
20
bot/tools/migration_to_sql/startup_mock_extension.py
Normal file
20
bot/tools/migration_to_sql/startup_mock_extension.py
Normal file
@ -0,0 +1,20 @@
|
||||
from cpl_core.application import StartupExtensionABC
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.database import DatabaseSettings
|
||||
from cpl_core.database.context import DatabaseContextABC
|
||||
from cpl_core.dependency_injection import ServiceCollectionABC
|
||||
from cpl_core.environment import ApplicationEnvironmentABC
|
||||
|
||||
from migration_to_sql.mock.mock_db_context import MockDBContext
|
||||
|
||||
|
||||
class StartupMockExtension(StartupExtensionABC):
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC):
|
||||
pass
|
||||
|
||||
def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC):
|
||||
services.add_db_context(MockDBContext, DatabaseSettings())
|
||||
services.add_transient(DatabaseContextABC, MockDBContext)
|
Loading…
Reference in New Issue
Block a user