Compare commits

..

No commits in common. "fe5b0207c0ed9827ebdcd9448262e7a312e0b26b" and "d2d59bdad70d7c22a89a909482dedf1e2f1b9aff" have entirely different histories.

7 changed files with 56 additions and 84 deletions

View File

@ -1,4 +1,4 @@
DROP TABLE `AutoRole`;
DROP TABLE `AutoRoleRules`; DROP TABLE `AutoRoleRules`;
DROP TABLE `AutoRoles`;

View File

@ -1,4 +1,4 @@
DROP TABLE `AuthUsers`;
DROP TABLE `AuthUserUsersRelations`; DROP TABLE `AuthUserUsersRelations`;
DROP TABLE `AuthUsers`;

View File

@ -1,8 +1,6 @@
DROP TABLE `UserJoinedGameServer`; DROP TABLE `GameServers`;
DROP TABLE `UserJoinedGameServer`;
DROP TABLE `UserGameIdents`; DROP TABLE `UserGameIdents`;
DROP TABLE `GameServers`;

View File

@ -1,10 +1,6 @@
DROP TABLE `UserGotAchievements`;
DROP TABLE `Achievements`; DROP TABLE `Achievements`;
ALTER TABLE Users DROP COLUMN MessageCount; ALTER TABLE Users DROP COLUMN MessageCount;
ALTER TABLE Users DROP COLUMN ReactionCount; ALTER TABLE Users DROP COLUMN ReactionCount;
DROP TABLE `AchievementsHistory`;

View File

@ -1,24 +1,8 @@
DROP TABLE `CFG_ServerTeamRoleIds`;
DROP TABLE `CFG_ServerTeamRoleIdsHistory`;
DROP TABLE `CFG_ServerAFKChannelIds`;
DROP TABLE `CFG_ServerAFKChannelIdsHistory`;
DROP TABLE `CFG_Server`; DROP TABLE `CFG_Server`;
DROP TABLE `CFG_ServerHistory`;
DROP TABLE `CFG_TechnicianPingUrls`;
DROP TABLE `CFG_TechnicianPingUrlsHistory`;
DROP TABLE `CFG_TechnicianIds`;
DROP TABLE `CFG_TechnicianIdsHistory`;
DROP TABLE `CFG_Technician`; DROP TABLE `CFG_Technician`;
DROP TABLE `CFG_TechnicianHistory`; DROP TABLE `CFG_TechnicianPingUrls`;
DROP TABLE `CFG_TechnicianIds`;

View File

@ -2,10 +2,11 @@ DROP TABLE `SteamSpecialOffers`;
ALTER TABLE CFG_Server ALTER TABLE CFG_Server
DROP COLUMN GameOfferNotificationChatId; DROP COLUMN ShortRoleNameSetOnlyHighest;
ALTER TABLE CFG_ServerHistory ALTER TABLE CFG_ServerHistory
DROP COLUMN GameOfferNotificationChatId; DROP COLUMN ShortRoleNameSetOnlyHighest;

View File

@ -4,9 +4,7 @@ import os
from cpl_core.database.context import DatabaseContextABC from cpl_core.database.context import DatabaseContextABC
from cpl_core.dependency_injection import ServiceProviderABC from cpl_core.dependency_injection import ServiceProviderABC
from cpl_query.extension import List from cpl_query.extension import List
from packaging import version
import bot
from bot_core.logging.database_logger import DatabaseLogger from bot_core.logging.database_logger import DatabaseLogger
from bot_data.model.migration import Migration from bot_data.model.migration import Migration
from bot_data.model.migration_history import MigrationHistory from bot_data.model.migration_history import MigrationHistory
@ -25,7 +23,11 @@ class MigrationService:
self._db = db self._db = db
self._cursor = db.cursor self._cursor = db.cursor
def _get_migration_history(self) -> List[MigrationHistory]: # self._migrations: List[MigrationABC] = (
# List(type, MigrationABC.__subclasses__()).order_by(lambda x: x.name.split("_")[0]).then_by(lambda x: x.prio)
# )
def _migrate_from_old_to_new(self):
results = self._db.select( results = self._db.select(
""" """
SELECT * FROM `MigrationHistory` SELECT * FROM `MigrationHistory`
@ -35,15 +37,7 @@ class MigrationService:
for result in results: for result in results:
applied_migrations.add(MigrationHistory(result[0], result[1])) applied_migrations.add(MigrationHistory(result[0], result[1]))
return applied_migrations for migration in applied_migrations:
def _migrate_from_old_to_new(self):
self._cursor.execute("SHOW TABLES LIKE 'MigrationHistory'")
result = self._cursor.fetchone()
if not result:
return
for migration in self._get_migration_history():
if not migration.migration_id.endswith("Migration"): if not migration.migration_id.endswith("Migration"):
continue continue
@ -51,37 +45,20 @@ class MigrationService:
self._cursor.execute(migration.change_id_string(migration.migration_id.replace("Migration", ""))) self._cursor.execute(migration.change_id_string(migration.migration_id.replace("Migration", "")))
self._db.save_changes() self._db.save_changes()
def _load_scripts(self, upgrade: bool = True) -> List[Migration]: def _load_up_scripts(self) -> List[Migration]:
migrations = List(Migration) migrations = List(Migration)
path = "../../src/bot_data/scripts" path = "../../src/bot_data/scripts"
if not os.path.exists(path): if not os.path.exists(path):
raise Exception("Migration path not found") raise Exception("Migration path not found")
folders = List(str, glob.glob(f"{path}/*")) folders = List(str, glob.glob(f"{path}/*")).order_by()
if upgrade:
folders = folders.order_by()
else:
folders = folders.order_by_descending()
for folder in folders: for folder in folders:
splitted = folder.split("/") splitted = folder.split("/")
version_str = splitted[len(splitted) - 1] version = splitted[len(splitted) - 1]
# upgrade do not run migrations from higher versions for file in List(str, os.listdir(folder)).where(lambda x: x.endswith("_up.sql")).order_by().to_list():
if upgrade and version.Version(version_str) > version.Version(bot.__version__):
break
# downgrade run migrations from higher versions
if not upgrade and version.Version(version_str) <= version.Version(bot.__version__):
continue
files = List(str, os.listdir(folder)).where(lambda x: x.endswith(f"_{'up' if upgrade else 'down'}.sql"))
if upgrade:
files = files.order_by()
else:
files = files.order_by_descending()
for file in files.to_list():
if not file.endswith(".sql"): if not file.endswith(".sql"):
continue continue
@ -97,11 +74,11 @@ class MigrationService:
script = f.read() script = f.read()
f.close() f.close()
migrations.add(Migration(name, version_str, script)) migrations.add(Migration(name, version, script))
return migrations return migrations
def _execute(self, migrations: List[Migration], upgrade: bool = True): def _execute(self, migrations: List[Migration]):
for migration in migrations: for migration in migrations:
active_statement = "" active_statement = ""
try: try:
@ -115,14 +92,10 @@ class MigrationService:
f"Running SQL Command: {MigrationHistory.get_select_by_id_string(migration.name)}", f"Running SQL Command: {MigrationHistory.get_select_by_id_string(migration.name)}",
) )
migration_from_db = self._db.select(MigrationHistory.get_select_by_id_string(migration.name)) migration_from_db = self._db.select(MigrationHistory.get_select_by_id_string(migration.name))
if upgrade and migration_from_db is not None and len(migration_from_db) > 0: if migration_from_db is not None and len(migration_from_db) > 0:
continue
elif not upgrade and (migration_from_db is None or len(migration_from_db) == 0):
continue continue
self._logger.debug( self._logger.debug(__name__, f"Running migration: {migration.name}")
__name__, f"Running {'upgrade' if upgrade else 'downgrade'} migration: {migration.name}"
)
for statement in migration.script.split("\n\n"): for statement in migration.script.split("\n\n"):
if statement in ["", "\n"]: if statement in ["", "\n"]:
@ -130,18 +103,38 @@ class MigrationService:
active_statement = statement active_statement = statement
self._cursor.execute(statement + ";") self._cursor.execute(statement + ";")
self._cursor.execute( self._cursor.execute(MigrationHistory(migration.name).insert_string)
MigrationHistory(migration.name).insert_string
if upgrade
else MigrationHistory(migration.name).delete_string
)
self._db.save_changes() self._db.save_changes()
except Exception as e: except Exception as e:
self._logger.fatal( self._logger.fatal(__name__, f"Migration failed: {migration.name}\n{active_statement}", e)
__name__, f"Migration failed: {migration.version}-{migration.name}\n{active_statement}", e
)
def migrate(self): def migrate(self):
self._migrate_from_old_to_new() self._migrate_from_old_to_new()
self._execute(self._load_scripts()) self._execute(self._load_up_scripts())
self._execute(self._load_scripts(False), False)
# def migrate(self):
# self._logger.info(__name__, f"Running Migrations")
# for migration in self._migrations:
# migration_id = migration.__name__
# try:
# # check if table exists
# self._cursor.execute("SHOW TABLES LIKE 'MigrationHistory'")
# result = self._cursor.fetchone()
# if result:
# # there is a table named "tableName"
# self._logger.trace(
# __name__,
# f"Running SQL Command: {MigrationHistory.get_select_by_id_string(migration_id)}",
# )
# migration_from_db = self._db.select(MigrationHistory.get_select_by_id_string(migration_id))
# if migration_from_db is not None and len(migration_from_db) > 0:
# continue
#
# self._logger.debug(__name__, f"Running Migration {migration_id}")
# migration_as_service: MigrationABC = self._services.get_service(migration)
# migration_as_service.upgrade()
# self._cursor.execute(MigrationHistory(migration_id).insert_string)
# self._db.save_changes()
#
# except Exception as e:
# self._logger.error(__name__, f"Cannot get migration with id {migration}", e)