Added short role name migration #378

This commit is contained in:
Sven Heidemann 2023-09-28 08:56:59 +02:00
parent 378d2c3dc9
commit d1c79c95b2
5 changed files with 91 additions and 12 deletions

View File

@ -16,6 +16,7 @@ from bot_data.migration.default_role_migration import DefaultRoleMigration
from bot_data.migration.initial_migration import InitialMigration
from bot_data.migration.level_migration import LevelMigration
from bot_data.migration.remove_stats_migration import RemoveStatsMigration
from bot_data.migration.short_role_name_migration import ShortRoleNameMigration
from bot_data.migration.stats_migration import StatsMigration
from bot_data.migration.user_joined_game_server_migration import UserJoinedGameServerMigration
from bot_data.migration.user_message_count_per_hour_migration import (
@ -50,3 +51,4 @@ class StartupMigrationExtension(StartupExtensionABC):
services.add_transient(MigrationABC, ConfigMigration) # 19.07.2023 #127 - 1.1.0
services.add_transient(MigrationABC, ConfigFeatureFlagsMigration) # 15.08.2023 #334 - 1.1.0
services.add_transient(MigrationABC, DefaultRoleMigration) # 24.09.2023 #360 - 1.1.3
services.add_transient(MigrationABC, ShortRoleNameMigration) # 28.09.2023 #378 - 1.1.7

View File

@ -1,13 +1,20 @@
import os
from abc import ABC, abstractmethod
from cpl_core.dependency_injection import ServiceProviderABC
from mysql.connector.cursor import MySQLCursorBuffered
from bot_data.db_context import DBContext
class MigrationABC(ABC):
name = None
prio = 0
@abstractmethod
def __init__(self):
pass
@ServiceProviderABC.inject
def __init__(self, db: DBContext):
self._cursor: MySQLCursorBuffered = db.cursor
@abstractmethod
def upgrade(self):
@ -16,3 +23,12 @@ class MigrationABC(ABC):
@abstractmethod
def downgrade(self):
pass
def _exec(self, file: str):
path = f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts"
sql = open(f"{path}/{file}").read()
for statement in sql.split("\n\n"):
self._cursor.execute(statement + ";")

View File

@ -1,5 +1,3 @@
import os
from bot_core.logging.database_logger import DatabaseLogger
from bot_data.abc.migration_abc import MigrationABC
from bot_data.db_context import DBContext
@ -12,14 +10,6 @@ class ConfigMigration(MigrationABC):
MigrationABC.__init__(self)
self._logger = logger
self._db = db
self._cursor = db.cursor
def _exec(self, file: str):
path = f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts"
sql = open(f"{path}/{file}").read()
for statement in sql.split("\n\n"):
self._cursor.execute(statement + ";")
def upgrade(self):
self._logger.debug(__name__, "Running upgrade")

View File

@ -0,0 +1,33 @@
CREATE TABLE IF NOT EXISTS `ShortRoleNamesHistory`
(
`Id` BIGINT(20) NOT NULL,
`Name` VARCHAR(64) DEFAULT NULL,
`DiscordRoleId` BIGINT(20) NOT NULL,
`Deleted` BOOL DEFAULT FALSE,
`DateFrom` DATETIME(6) NOT NULL,
`DateTo` DATETIME(6) NOT NULL
);
DROP TRIGGER IF EXISTS `TR_ShortRoleNamesUpdate`;
CREATE TRIGGER `TR_ShortRoleNamesUpdate`
AFTER UPDATE
ON `ShortRoleNames`
FOR EACH ROW
BEGIN
INSERT INTO `ShortRoleNamesHistory` (`Id`, `Name`, `DiscordRoleId`, `DateFrom`, `DateTo`)
VALUES (OLD.Id, OLD.Name, OLD.DiscordRoleId, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6));
END;
DROP TRIGGER IF EXISTS `TR_ShortRoleNamesDelete`;
CREATE TRIGGER `TR_ShortRoleNamesDelete`
AFTER DELETE
ON `ShortRoleNames`
FOR EACH ROW
BEGIN
INSERT INTO `ShortRoleNamesHistory` (`Id`, `Name`, `DiscordRoleId`, `Deleted`, `DateFrom`,
`DateTo`)
VALUES (OLD.Id, OLD.Name, OLD.DiscordRoleId, TRUE, OLD.LastModifiedAt,
CURRENT_TIMESTAMP(6));
END;

View File

@ -0,0 +1,38 @@
from bot_core.logging.database_logger import DatabaseLogger
from bot_data.abc.migration_abc import MigrationABC
from bot_data.db_context import DBContext
class ShortRoleNameMigration(MigrationABC):
name = "1.1.7_ShortRoleNameMigration"
def __init__(self, logger: DatabaseLogger, db: DBContext):
MigrationABC.__init__(self)
self._logger = logger
self._db = db
self._cursor = db.cursor
def upgrade(self):
self._logger.debug(__name__, "Running upgrade")
self._cursor.execute(
str(
f"""
CREATE TABLE IF NOT EXISTS `ShortRoleNames` (
`Id` BIGINT NOT NULL AUTO_INCREMENT,
`ShortName` VARCHAR(255) NOT NULL,
`DiscordRoleId` BIGINT NOT NULL,
`ServerId` BIGINT,
`CreatedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6),
`LastModifiedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY(`Id`),
FOREIGN KEY (`ServerId`) REFERENCES `Servers`(`ServerId`)
);
"""
)
)
self._exec("config/short_rule_names.sql")
def downgrade(self):
self._cursor.execute("DROP TABLE `ShortRoleNames`;")