Added config migration #127

This commit is contained in:
Sven Heidemann 2023-07-19 13:03:38 +02:00
parent 8e08910f2f
commit 2b69a906a6
7 changed files with 540 additions and 13 deletions

View File

@ -1,3 +1,5 @@
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,22 +14,140 @@ class ConfigMigration(MigrationABC):
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")
self._server_upgrade()
self._technician_upgrade()
# self._cursor.execute(
# str(
# f"""
# CREATE TABLE IF NOT EXISTS `Config` (
# `Id` BIGINT NOT NULL AUTO_INCREMENT,
# `CreatedAt` DATETIME(6),
# `LastModifiedAt` DATETIME(6),
# PRIMARY KEY(`Id`)
# );
# """
# )
# )
self._exec("config/server.sql")
self._exec("config/server_afk_channels.sql")
self._exec("config/server_team_roles.sql")
self._exec("config/technician.sql")
self._exec("config/technician_ids.sql")
self._exec("config/technician_ping_urls.sql")
def _server_upgrade(self):
self._cursor.execute(
str(
f"""
CREATE TABLE IF NOT EXISTS `CFG_Server` (
`Id` BIGINT NOT NULL AUTO_INCREMENT,
`MessageDeleteTimer` BIGINT NOT NULL DEFAULT 6,
`NotificationChatId` BIGINT NOT NULL,
`MaxVoiceStateHours` BIGINT NOT NULL DEFAULT 6,
`XpPerMessage` BIGINT NOT NULL DEFAULT 1,
`XpPerReaction` BIGINT NOT NULL DEFAULT 1,
`MaxMessageXpPerHour` BIGINT NOT NULL DEFAULT 20,
`XpPerOntimeHour` BIGINT NOT NULL DEFAULT 10,
`XpPerEventParticipation` BIGINT NOT NULL DEFAULT 10,
`XpPerAchievement` BIGINT NOT NULL DEFAULT 10,
`AFKCommandChannelId` BIGINT NOT NULL,
`HelpVoiceChannelId` BIGINT NOT NULL,
`TeamChannelId` BIGINT NOT NULL,
`LoginMessageChannelId` BIGINT NOT NULL,
`ServerId` BIGINT NOT NULL,
`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._cursor.execute(
str(
f"""
CREATE TABLE IF NOT EXISTS `CFG_ServerAFKChannelIds` (
`Id` BIGINT NOT NULL AUTO_INCREMENT,
`ChannelId` BIGINT NOT NULL,
`ServerId` BIGINT NOT NULL,
`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._cursor.execute(
str(
f"""
CREATE TABLE IF NOT EXISTS `CFG_ServerTeamRoleIds` (
`Id` BIGINT NOT NULL AUTO_INCREMENT,
`RoleId` BIGINT NOT NULL,
`TeamMemberType` ENUM('Moderator', 'Admin') NOT NULL,
`ServerId` BIGINT NOT NULL,
`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`)
);
"""
)
)
def _technician_upgrade(self):
self._cursor.execute(
str(
f"""
CREATE TABLE IF NOT EXISTS `CFG_Technician` (
`Id` BIGINT NOT NULL AUTO_INCREMENT,
`HelpCommandReferenceUrl` VARCHAR(255) NOT NULL,
`WaitForRestart` BIGINT NOT NULL DEFAULT 8,
`WaitForShutdown` BIGINT NOT NULL DEFAULT 8,
`CacheMaxMessages` BIGINT NOT NULL DEFAULT 1000000,
`CreatedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6),
`LastModifiedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY(`Id`)
);
"""
)
)
self._cursor.execute(
str(
f"""
CREATE TABLE IF NOT EXISTS `CFG_TechnicianPingUrls` (
`Id` BIGINT NOT NULL AUTO_INCREMENT,
`URL` VARCHAR(255) NOT NULL,
`CreatedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6),
`LastModifiedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY(`Id`)
);
"""
)
)
self._cursor.execute(
str(
f"""
CREATE TABLE IF NOT EXISTS `CFG_TechnicianIds` (
`Id` BIGINT NOT NULL AUTO_INCREMENT,
`TechnicianId` BIGINT NOT NULL,
`CreatedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6),
`LastModifiedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY(`Id`)
);
"""
)
)
def downgrade(self):
self._logger.debug(__name__, "Running downgrade")
# self._cursor.execute("DROP TABLE `Config`;")
def _server_downgrade(self):
self._cursor.execute("DROP TABLE `CFG_Server`;")
def _technician_downgrade(self):
self._cursor.execute("DROP TABLE `CFG_Technician`;")
self._cursor.execute("DROP TABLE `CFG_TechnicianPingUrls`;")
self._cursor.execute("DROP TABLE `CFG_TechnicianIds`;")

View File

@ -0,0 +1,117 @@
CREATE TABLE IF NOT EXISTS `CFG_ServerHistory`
(
`Id` BIGINT(20) NOT NULL,
`MessageDeleteTimer` BIGINT NOT NULL DEFAULT 6,
`NotificationChatId` BIGINT NOT NULL,
`MaxVoiceStateHours` BIGINT NOT NULL DEFAULT 6,
`XpPerMessage` BIGINT NOT NULL DEFAULT 1,
`XpPerReaction` BIGINT NOT NULL DEFAULT 1,
`MaxMessageXpPerHour` BIGINT NOT NULL DEFAULT 20,
`XpPerOntimeHour` BIGINT NOT NULL DEFAULT 10,
`XpPerEventParticipation` BIGINT NOT NULL DEFAULT 10,
`XpPerAchievement` BIGINT NOT NULL DEFAULT 10,
`AFKCommandChannelId` BIGINT NOT NULL,
`HelpVoiceChannelId` BIGINT NOT NULL,
`TeamChannelId` BIGINT NOT NULL,
`LoginMessageChannelId` BIGINT NOT NULL,
`ServerId` BIGINT NOT NULL,
`Deleted` BOOL DEFAULT FALSE,
`DateFrom` DATETIME(6) NOT NULL,
`DateTo` DATETIME(6) NOT NULL
);
DROP TRIGGER IF EXISTS `TR_CFG_ServerUpdate`;
CREATE TRIGGER `TR_CFG_ServerUpdate`
AFTER UPDATE
ON `CFG_Server`
FOR EACH ROW
BEGIN
INSERT INTO `CFG_ServerHistory` (
`Id`,
`MessageDeleteTimer`,
`NotificationChatId`,
`MaxVoiceStateHours`,
`XpPerMessage`,
`XpPerReaction`,
`MaxMessageXpPerHour`,
`XpPerOntimeHour`,
`XpPerEventParticipation`,
`XpPerAchievement`,
`AFKCommandChannelId`,
`HelpVoiceChannelId`,
`TeamChannelId`,
`LoginMessageChannelId`,
`ServerId`,
`DateFrom`,
`DateTo`
)
VALUES (
OLD.UserId,
OLD.MessageDeleteTimer,
OLD.NotificationChatId,
OLD.MaxVoiceStateHours,
OLD.XpPerMessage,
OLD.XpPerReaction,
OLD.MaxMessageXpPerHour,
OLD.XpPerOntimeHour,
OLD.XpPerEventParticipation,
OLD.XpPerAchievement,
OLD.AFKCommandChannelId,
OLD.HelpVoiceChannelId,
OLD.TeamChannelId,
OLD.LoginMessageChannelId,
OLD.ServerId,
OLD.LastModifiedAt,
CURRENT_TIMESTAMP(6)
);
END;
DROP TRIGGER IF EXISTS `TR_CFG_ServerDelete`;
CREATE TRIGGER `TR_CFG_ServerDelete`
AFTER DELETE
ON `CFG_Server`
FOR EACH ROW
BEGIN
INSERT INTO `CFG_ServerHistory` (
`Id`,
`MessageDeleteTimer`,
`NotificationChatId`,
`MaxVoiceStateHours`,
`XpPerMessage`,
`XpPerReaction`,
`MaxMessageXpPerHour`,
`XpPerOntimeHour`,
`XpPerEventParticipation`,
`XpPerAchievement`,
`AFKCommandChannelId`,
`HelpVoiceChannelId`,
`TeamChannelId`,
`LoginMessageChannelId`,
`ServerId`,
`Deleted`,
`DateFrom`,
`DateTo`
)
VALUES (
OLD.UserId,
OLD.MessageDeleteTimer,
OLD.NotificationChatId,
OLD.MaxVoiceStateHours,
OLD.XpPerMessage,
OLD.XpPerReaction,
OLD.MaxMessageXpPerHour,
OLD.XpPerOntimeHour,
OLD.XpPerEventParticipation,
OLD.XpPerAchievement,
OLD.AFKCommandChannelId,
OLD.HelpVoiceChannelId,
OLD.TeamChannelId,
OLD.LoginMessageChannelId,
OLD.ServerId,
TRUE,
OLD.LastModifiedAt,
CURRENT_TIMESTAMP(6)
);
END;

View File

@ -0,0 +1,57 @@
CREATE TABLE IF NOT EXISTS `CFG_ServerAFKChannelIdsHistory`
(
`Id` BIGINT(20) NOT NULL,
`ChannelId` BIGINT NOT NULL,
`ServerId` BIGINT NOT NULL,
`Deleted` BOOL DEFAULT FALSE,
`DateFrom` DATETIME(6) NOT NULL,
`DateTo` DATETIME(6) NOT NULL
);
DROP TRIGGER IF EXISTS `TR_CFG_ServerAFKChannelIdsUpdate`;
CREATE TRIGGER `TR_CFG_ServerAFKChannelIdsUpdate`
AFTER UPDATE
ON `CFG_ServerAFKChannelIds`
FOR EACH ROW
BEGIN
INSERT INTO `CFG_ServerAFKChannelIdsHistory` (
`Id`,
`ChannelId`,
`ServerId`,
`DateFrom`,
`DateTo`
)
VALUES (
OLD.UserId,
OLD.ChannelId,
OLD.ServerId,
OLD.LastModifiedAt,
CURRENT_TIMESTAMP(6)
);
END;
DROP TRIGGER IF EXISTS `TR_CFG_ServerAFKChannelIdsDelete`;
CREATE TRIGGER `TR_CFG_ServerAFKChannelIdsDelete`
AFTER DELETE
ON `CFG_ServerAFKChannelIds`
FOR EACH ROW
BEGIN
INSERT INTO `CFG_ServerAFKChannelIdsHistory` (
`Id`,
`ChannelId`,
`ServerId`,
`Deleted`,
`DateFrom`,
`DateTo`
)
VALUES (
OLD.UserId,
OLD.ChannelId,
OLD.ServerId,
TRUE,
OLD.LastModifiedAt,
CURRENT_TIMESTAMP(6)
);
END;

View File

@ -0,0 +1,62 @@
CREATE TABLE IF NOT EXISTS `CFG_ServerTeamRoleIdsHistory`
(
`Id` BIGINT(20) NOT NULL,
`RoleId` BIGINT NOT NULL,
`TeamMemberType` ENUM('Moderator', 'Admin') NOT NULL,
`ServerId` BIGINT NOT NULL,
`Deleted` BOOL DEFAULT FALSE,
`DateFrom` DATETIME(6) NOT NULL,
`DateTo` DATETIME(6) NOT NULL
);
DROP TRIGGER IF EXISTS `TR_CFG_ServerTeamRoleIdsUpdate`;
CREATE TRIGGER `TR_CFG_ServerTeamRoleIdsUpdate`
AFTER UPDATE
ON `CFG_ServerTeamRoleIds`
FOR EACH ROW
BEGIN
INSERT INTO `CFG_ServerTeamRoleIdsHistory` (
`Id`,
`RoleId`,
`TeamMemberType`,
`ServerId`,
`DateFrom`,
`DateTo`
)
VALUES (
OLD.UserId,
OLD.RoleId,
OLD.TeamMemberType,
OLD.ServerId,
OLD.LastModifiedAt,
CURRENT_TIMESTAMP(6)
);
END;
DROP TRIGGER IF EXISTS `TR_CFG_ServerTeamRoleIdsDelete`;
CREATE TRIGGER `TR_CFG_ServerTeamRoleIdsDelete`
AFTER DELETE
ON `CFG_ServerTeamRoleIds`
FOR EACH ROW
BEGIN
INSERT INTO `CFG_ServerTeamRoleIdsHistory` (
`Id`,
`RoleId`,
`TeamMemberType`,
`ServerId`,
`Deleted`,
`DateFrom`,
`DateTo`
)
VALUES (
OLD.UserId,
OLD.RoleId,
OLD.TeamMemberType,
OLD.ServerId,
TRUE,
OLD.LastModifiedAt,
CURRENT_TIMESTAMP(6)
);
END;

View File

@ -0,0 +1,67 @@
CREATE TABLE IF NOT EXISTS `CFG_TechnicianHistory`
(
`Id` BIGINT(20) NOT NULL,
`HelpCommandReferenceUrl` VARCHAR(255) NOT NULL,
`WaitForRestart` BIGINT NOT NULL DEFAULT 8,
`WaitForShutdown` BIGINT NOT NULL DEFAULT 8,
`CacheMaxMessages` BIGINT NOT NULL DEFAULT 1000000,
`Deleted` BOOL DEFAULT FALSE,
`DateFrom` DATETIME(6) NOT NULL,
`DateTo` DATETIME(6) NOT NULL
);
DROP TRIGGER IF EXISTS `TR_CFG_TechnicianUpdate`;
CREATE TRIGGER `TR_CFG_TechnicianUpdate`
AFTER UPDATE
ON `CFG_Technician`
FOR EACH ROW
BEGIN
INSERT INTO `CFG_TechnicianHistory` (
`Id`,
`HelpCommandReferenceUrl`,
`WaitForRestart`,
`WaitForShutdown`,
`CacheMaxMessages`,
`DateFrom`,
`DateTo`
)
VALUES (
OLD.UserId,
OLD.HelpCommandReferenceUrl,
OLD.WaitForRestart,
OLD.WaitForShutdown,
OLD.CacheMaxMessages,
OLD.LastModifiedAt,
CURRENT_TIMESTAMP(6)
);
END;
DROP TRIGGER IF EXISTS `TR_CFG_TechnicianDelete`;
CREATE TRIGGER `TR_CFG_TechnicianDelete`
AFTER DELETE
ON `CFG_Technician`
FOR EACH ROW
BEGIN
INSERT INTO `CFG_TechnicianHistory` (
`Id`,
`HelpCommandReferenceUrl`,
`WaitForRestart`,
`WaitForShutdown`,
`CacheMaxMessages`,
`Deleted`,
`DateFrom`,
`DateTo`
)
VALUES (
OLD.UserId,
OLD.HelpCommandReferenceUrl,
OLD.WaitForRestart,
OLD.WaitForShutdown,
OLD.CacheMaxMessages,
TRUE,
OLD.LastModifiedAt,
CURRENT_TIMESTAMP(6)
);
END;

View File

@ -0,0 +1,52 @@
CREATE TABLE IF NOT EXISTS `CFG_TechnicianIdsHistory`
(
`Id` BIGINT(20) NOT NULL,
`TechnicianId` BIGINT NOT NULL,
`Deleted` BOOL DEFAULT FALSE,
`DateFrom` DATETIME(6) NOT NULL,
`DateTo` DATETIME(6) NOT NULL
);
DROP TRIGGER IF EXISTS `TR_CFG_TechnicianIdsUpdate`;
CREATE TRIGGER `TR_CFG_TechnicianIdsUpdate`
AFTER UPDATE
ON `CFG_TechnicianIds`
FOR EACH ROW
BEGIN
INSERT INTO `CFG_TechnicianIdsHistory` (
`Id`,
`TechnicianId`,
`DateFrom`,
`DateTo`
)
VALUES (
OLD.UserId,
OLD.TechnicianId,
OLD.LastModifiedAt,
CURRENT_TIMESTAMP(6)
);
END;
DROP TRIGGER IF EXISTS `TR_CFG_TechnicianIdsDelete`;
CREATE TRIGGER `TR_CFG_TechnicianIdsDelete`
AFTER DELETE
ON `CFG_TechnicianIds`
FOR EACH ROW
BEGIN
INSERT INTO `CFG_TechnicianIdsHistory` (
`Id`,
`TechnicianId`,
`Deleted`,
`DateFrom`,
`DateTo`
)
VALUES (
OLD.UserId,
OLD.TechnicianId,
TRUE,
OLD.LastModifiedAt,
CURRENT_TIMESTAMP(6)
);
END;

View File

@ -0,0 +1,52 @@
CREATE TABLE IF NOT EXISTS `CFG_TechnicianPingUrlsHistory`
(
`Id` BIGINT(20) NOT NULL,
`URL` VARCHAR(255) NOT NULL,
`Deleted` BOOL DEFAULT FALSE,
`DateFrom` DATETIME(6) NOT NULL,
`DateTo` DATETIME(6) NOT NULL
);
DROP TRIGGER IF EXISTS `TR_CFG_TechnicianPingUrlsUpdate`;
CREATE TRIGGER `TR_CFG_TechnicianPingUrlsUpdate`
AFTER UPDATE
ON `CFG_TechnicianPingUrls`
FOR EACH ROW
BEGIN
INSERT INTO `CFG_TechnicianPingUrlsHistory` (
`Id`,
`URL`,
`DateFrom`,
`DateTo`
)
VALUES (
OLD.UserId,
OLD.URL,
OLD.LastModifiedAt,
CURRENT_TIMESTAMP(6)
);
END;
DROP TRIGGER IF EXISTS `TR_CFG_TechnicianPingUrlsDelete`;
CREATE TRIGGER `TR_CFG_TechnicianPingUrlsDelete`
AFTER DELETE
ON `CFG_TechnicianPingUrls`
FOR EACH ROW
BEGIN
INSERT INTO `CFG_TechnicianPingUrlsHistory` (
`Id`,
`URL`,
`Deleted`,
`DateFrom`,
`DateTo`
)
VALUES (
OLD.UserId,
OLD.URL,
TRUE,
OLD.LastModifiedAt,
CURRENT_TIMESTAMP(6)
);
END;