[WIP] Added more tables #246

This commit is contained in:
Sven Heidemann 2023-03-06 17:53:15 +01:00
parent d24d3fa4de
commit bca33c6e56
8 changed files with 167 additions and 62 deletions

View File

@ -14,51 +14,6 @@ class DBHistoryMigration(MigrationABC):
self._db = db
self._cursor = db.cursor
def _game_servers(self):
self._cursor.execute(
f"""
CREATE TABLE IF NOT EXISTS `GameServersHistory`
(
`Id` BIGINT(20) NOT NULL,
`Name` VARCHAR(255) NOT NULL,
`ServerId` BIGINT(20) NOT NULL,
`ApiKeyId` BIGINT(20) NOT NULL,
`DateFrom` DATETIME(6) NOT NULL,
`DateTo` DATETIME(6) NOT NULL
);
"""
)
def _known_users(self):
self._cursor.execute(
f"""
CREATE TABLE IF NOT EXISTS `KnownUsersHistory`
(
`KnownUserId` BIGINT(20) NOT NULL,
`DiscordId` BIGINT(20) NOT NULL,
`DateFrom` DATETIME(6) NOT NULL,
`DateTo` DATETIME(6) NOT NULL
);
"""
)
def _levels(self):
self._cursor.execute(
f"""
CREATE TABLE IF NOT EXISTS `LevelsHistory`
(
`Id` BIGINT(20) NOT NULL,
`Name` VARCHAR(255) NOT NULL,
`Color` VARCHAR(8) NOT NULL,
`MinXp` BIGINT(20) NOT NULL,
`PermissionInt` BIGINT(20) NOT NULL,
`ServerId` BIGINT(20) DEFAULT NULL,
`DateFrom` DATETIME(6) NOT NULL,
`DateTo` DATETIME(6) NOT NULL
);
"""
)
def _servers(self):
self._cursor.execute(
f"""
@ -203,6 +158,15 @@ class DBHistoryMigration(MigrationABC):
self._cursor.execute(
open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/clients.sql").read()
)
self._cursor.execute(
open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/game_servers.sql").read()
)
self._cursor.execute(
open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/known_users.sql").read()
)
self._cursor.execute(
open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/levels.sql").read()
)
def downgrade(self):
self._cursor.execute("DROP TABLE `ApiKeysHistory`;")

View File

@ -0,0 +1,49 @@
ALTER TABLE `GameServers`
MODIFY `LastModifiedAt` DATETIME(6) NULL ON UPDATE CURRENT_TIMESTAMP(6);
ALTER TABLE `GameServers`
ALTER COLUMN `CreatedAt` SET DEFAULT CURRENT_TIMESTAMP(6);
ALTER TABLE `GameServers`
ALTER COLUMN `LastModifiedAt` SET DEFAULT CURRENT_TIMESTAMP(6);
CREATE TABLE IF NOT EXISTS `GameServersHistory`
(
`Id` BIGINT(20) NOT NULL,
`Name` VARCHAR(255) NOT NULL,
`ServerId` BIGINT(20) NOT NULL,
`ApiKeyId` BIGINT(20) NOT NULL,
`Deleted` BOOL DEFAULT FALSE,
`DateFrom` DATETIME(6) NOT NULL,
`DateTo` DATETIME(6) NOT NULL
);
DROP TRIGGER IF EXISTS `TR_GameServersUpdate`;
CREATE TRIGGER `TR_GameServersUpdate`
AFTER UPDATE
ON `GameServers`
FOR EACH ROW
BEGIN
INSERT INTO `GameServersHistory` (
`Id`, `Name`, `ServerId`, `ApiKeyId`, `DateFrom`, `DateTo`
)
VALUES (
OLD.Id, OLD.Name, OLD.ServerId, OLD.ApiKeyId, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)
);
END;
DROP TRIGGER IF EXISTS `TR_GameServersDelete`;
CREATE TRIGGER `TR_GameServersDelete`
AFTER DELETE
ON `GameServers`
FOR EACH ROW
BEGIN
INSERT INTO `GameServersHistory` (
`Id`, `Name`, `ServerId`, `ApiKeyId`, `Deleted`, `DateFrom`, `DateTo`
)
VALUES (
OLD.Id, OLD.Name, OLD.ServerId, OLD.ApiKeyId, TRUE, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)
);
END;

View File

@ -0,0 +1,47 @@
ALTER TABLE `KnownUsers`
MODIFY `LastModifiedAt` DATETIME(6) NULL ON UPDATE CURRENT_TIMESTAMP(6);
ALTER TABLE `KnownUsers`
ALTER COLUMN `CreatedAt` SET DEFAULT CURRENT_TIMESTAMP(6);
ALTER TABLE `KnownUsers`
ALTER COLUMN `LastModifiedAt` SET DEFAULT CURRENT_TIMESTAMP(6);
CREATE TABLE IF NOT EXISTS `KnownUsersHistory`
(
`KnownUserId` BIGINT(20) NOT NULL,
`DiscordId` BIGINT(20) NOT NULL,
`Deleted` BOOL DEFAULT FALSE,
`DateFrom` DATETIME(6) NOT NULL,
`DateTo` DATETIME(6) NOT NULL
);
DROP TRIGGER IF EXISTS `TR_KnownUsersUpdate`;
CREATE TRIGGER `TR_KnownUsersUpdate`
AFTER UPDATE
ON `KnownUsers`
FOR EACH ROW
BEGIN
INSERT INTO `KnownUsersHistory` (
`KnownUserId`, `DiscordId`, `DateFrom`, `DateTo`
)
VALUES (
OLD.KnownUserId, OLD.DiscordId, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)
);
END;
DROP TRIGGER IF EXISTS `TR_KnownUsersDelete`;
CREATE TRIGGER `TR_KnownUsersDelete`
AFTER DELETE
ON `KnownUsers`
FOR EACH ROW
BEGIN
INSERT INTO `KnownUsersHistory` (
`KnownUserId`, `DiscordId`, `Deleted`, `DateFrom`, `DateTo`
)
VALUES (
OLD.KnownUserId, OLD.DiscordId, TRUE, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)
);
END;

View File

@ -0,0 +1,53 @@
ALTER TABLE `Levels`
MODIFY `LastModifiedAt` DATETIME(6) NULL ON UPDATE CURRENT_TIMESTAMP(6);
ALTER TABLE `Levels`
ALTER COLUMN `CreatedAt` SET DEFAULT CURRENT_TIMESTAMP(6);
ALTER TABLE `Levels`
ALTER COLUMN `LastModifiedAt` SET DEFAULT CURRENT_TIMESTAMP(6);
CREATE TABLE IF NOT EXISTS `LevelsHistory`
(
`Id` BIGINT(20) NOT NULL,
`Name` VARCHAR(255) NOT NULL,
`Color` VARCHAR(8) NOT NULL,
`MinXp` BIGINT(20) NOT NULL,
`PermissionInt` BIGINT(20) NOT NULL,
`ServerId` BIGINT(20) DEFAULT NULL,
`Deleted` BOOL DEFAULT FALSE,
`DateFrom` DATETIME(6) NOT NULL,
`DateTo` DATETIME(6) NOT NULL
);
DROP TRIGGER IF EXISTS `TR_LevelsUpdate`;
CREATE TRIGGER `TR_LevelsUpdate`
AFTER UPDATE
ON `Levels`
FOR EACH ROW
BEGIN
INSERT INTO `LevelsHistory` (
`Id`, `Name`, `Color`, `MinXp`, `PermissionInt`, `ServerId`, `DateFrom`, `DateTo`
)
VALUES (
OLD.Id, OLD.Name, OLD.Color, OLD.MinXp, OLD.PermissionInt, OLD.ServerId, OLD.LastModifiedAt,
CURRENT_TIMESTAMP(6)
);
END;
DROP TRIGGER IF EXISTS `TR_LevelsDelete`;
CREATE TRIGGER `TR_LevelsDelete`
AFTER DELETE
ON `Levels`
FOR EACH ROW
BEGIN
INSERT INTO `LevelsHistory` (
`Id`, `Name`, `Color`, `MinXp`, `PermissionInt`, `ServerId`, `Deleted`, `DateFrom`, `DateTo`
)
VALUES (
OLD.Id, OLD.Name, OLD.Color, OLD.MinXp, OLD.PermissionInt, OLD.ServerId, TRUE, OLD.LastModifiedAt,
CURRENT_TIMESTAMP(6)
);
END;

View File

@ -174,8 +174,7 @@ class Client(TableABC):
`ReceivedMessageCount` = {self._received_message_count},
`DeletedMessageCount` = {self._deleted_message_count},
`ReceivedCommandsCount` = {self._received_command_count},
`MovedUsersCount` = {self._moved_users_count},
`LastModifiedAt` = '{self._modified_at}'
`MovedUsersCount` = {self._moved_users_count}
WHERE `ClientId` = {self._client_id};
"""
)

View File

@ -102,13 +102,11 @@ class GameServer(TableABC):
return str(
f"""
INSERT INTO `GameServers` (
`Name`, `ServerId`, `ApiKeyId`, `CreatedAt`, `LastModifiedAt`
`Name`, `ServerId`, `ApiKeyId`
) VALUES (
'{self._name}',
{self._server.id},
{self._api_key.id},
'{self._created_at}',
'{self._modified_at}'
{self._api_key.id}
);
"""
)
@ -118,7 +116,7 @@ class GameServer(TableABC):
return str(
f"""
UPDATE `GameServers`
SET `LastModifiedAt` = '{self._modified_at}'
SET `Name` = '{self._name}'
WHERE `Id` = {self._id};
"""
)

View File

@ -57,11 +57,9 @@ class KnownUser(TableABC):
return str(
f"""
INSERT INTO `KnownUsers` (
`DiscordId`, `CreatedAt`, `LastModifiedAt`
`DiscordId`
) VALUES (
{self._discord_id},
'{self._created_at}',
'{self._modified_at}'
{self._discord_id}
);
"""
)

View File

@ -109,15 +109,13 @@ class Level(TableABC):
return str(
f"""
INSERT INTO `Levels` (
`Name`, `Color`, `MinXp`, `PermissionInt`, `ServerId`, `CreatedAt`, `LastModifiedAt`
`Name`, `Color`, `MinXp`, `PermissionInt`, `ServerId`
) VALUES (
'{self._name}',
'{self._color}',
{self._min_xp},
{self._permissions},
{self._server.id},
'{self._created_at}',
'{self._modified_at}'
{self._server.id}
);
"""
)
@ -130,8 +128,7 @@ class Level(TableABC):
SET `Name` = '{self._name}',
`Color` = '{self._color}',
`MinXp` = {self._min_xp},
`PermissionInt` = {self._permissions},
`LastModifiedAt` = '{self._modified_at}'
`PermissionInt` = {self._permissions}
WHERE `Id` = {self._id};
"""
)