Updated and added script order #428
This commit is contained in:
parent
39b9def76c
commit
e018fdcbdf
@ -1,12 +0,0 @@
|
|||||||
DROP TABLE `Servers`;
|
|
||||||
|
|
||||||
DROP TABLE `Users`;
|
|
||||||
|
|
||||||
DROP TABLE `Clients`;
|
|
||||||
|
|
||||||
DROP TABLE `KnownUsers`;
|
|
||||||
|
|
||||||
DROP TABLE `UserJoinedServers`;
|
|
||||||
|
|
||||||
DROP TABLE `UserJoinedVoiceChannel`;
|
|
||||||
|
|
@ -1,70 +0,0 @@
|
|||||||
CREATE TABLE IF NOT EXISTS `Servers`
|
|
||||||
(
|
|
||||||
`ServerId` BIGINT NOT NULL AUTO_INCREMENT,
|
|
||||||
`DiscordServerId` BIGINT NOT NULL,
|
|
||||||
`CreatedAt` DATETIME(6),
|
|
||||||
`LastModifiedAt` DATETIME(6),
|
|
||||||
PRIMARY KEY (`ServerId`)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS `Users`
|
|
||||||
(
|
|
||||||
`UserId` BIGINT NOT NULL AUTO_INCREMENT,
|
|
||||||
`DiscordId` BIGINT NOT NULL,
|
|
||||||
`XP` BIGINT NOT NULL DEFAULT 0,
|
|
||||||
`ServerId` BIGINT,
|
|
||||||
`CreatedAt` DATETIME(6),
|
|
||||||
`LastModifiedAt` DATETIME(6),
|
|
||||||
FOREIGN KEY (`ServerId`) REFERENCES Servers (`ServerId`),
|
|
||||||
PRIMARY KEY (`UserId`)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS `Clients`
|
|
||||||
(
|
|
||||||
`ClientId` BIGINT NOT NULL AUTO_INCREMENT,
|
|
||||||
`DiscordClientId` BIGINT NOT NULL,
|
|
||||||
`SentMessageCount` BIGINT NOT NULL DEFAULT 0,
|
|
||||||
`ReceivedMessageCount` BIGINT NOT NULL DEFAULT 0,
|
|
||||||
`DeletedMessageCount` BIGINT NOT NULL DEFAULT 0,
|
|
||||||
`ReceivedCommandsCount` BIGINT NOT NULL DEFAULT 0,
|
|
||||||
`MovedUsersCount` BIGINT NOT NULL DEFAULT 0,
|
|
||||||
`ServerId` BIGINT,
|
|
||||||
`CreatedAt` DATETIME(6),
|
|
||||||
`LastModifiedAt` DATETIME(6),
|
|
||||||
FOREIGN KEY (`ServerId`) REFERENCES Servers (`ServerId`),
|
|
||||||
PRIMARY KEY (`ClientId`)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS `KnownUsers`
|
|
||||||
(
|
|
||||||
`KnownUserId` BIGINT NOT NULL AUTO_INCREMENT,
|
|
||||||
`DiscordId` BIGINT NOT NULL,
|
|
||||||
`CreatedAt` DATETIME(6),
|
|
||||||
`LastModifiedAt` DATETIME(6),
|
|
||||||
PRIMARY KEY (`KnownUserId`)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS `UserJoinedServers`
|
|
||||||
(
|
|
||||||
`JoinId` BIGINT NOT NULL AUTO_INCREMENT,
|
|
||||||
`UserId` BIGINT NOT NULL,
|
|
||||||
`JoinedOn` DATETIME(6) NOT NULL,
|
|
||||||
`LeavedOn` DATETIME(6),
|
|
||||||
`CreatedAt` DATETIME(6),
|
|
||||||
`LastModifiedAt` DATETIME(6),
|
|
||||||
FOREIGN KEY (`UserId`) REFERENCES Users (`UserId`),
|
|
||||||
PRIMARY KEY (`JoinId`)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS `UserJoinedVoiceChannel`
|
|
||||||
(
|
|
||||||
`JoinId` BIGINT NOT NULL AUTO_INCREMENT,
|
|
||||||
`UserId` BIGINT NOT NULL,
|
|
||||||
`DiscordChannelId` BIGINT NOT NULL,
|
|
||||||
`JoinedOn` DATETIME(6) NOT NULL,
|
|
||||||
`LeavedOn` DATETIME(6),
|
|
||||||
`CreatedAt` DATETIME(6),
|
|
||||||
`LastModifiedAt` DATETIME(6),
|
|
||||||
FOREIGN KEY (`UserId`) REFERENCES Users (`UserId`),
|
|
||||||
PRIMARY KEY (`JoinId`)
|
|
||||||
);
|
|
@ -1,4 +0,0 @@
|
|||||||
DROP TABLE `AutoRoles`;
|
|
||||||
|
|
||||||
DROP TABLE `AutoRoleRules`;
|
|
||||||
|
|
@ -1,24 +0,0 @@
|
|||||||
CREATE TABLE IF NOT EXISTS `AutoRoles`
|
|
||||||
(
|
|
||||||
`AutoRoleId` BIGINT NOT NULL AUTO_INCREMENT,
|
|
||||||
`ServerId` BIGINT,
|
|
||||||
`DiscordMessageId` BIGINT NOT NULL,
|
|
||||||
`CreatedAt` DATETIME(6),
|
|
||||||
`LastModifiedAt` DATETIME(6),
|
|
||||||
PRIMARY KEY (`AutoRoleId`),
|
|
||||||
FOREIGN KEY (`ServerId`) REFERENCES `Servers` (`ServerId`)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS `AutoRoleRules`
|
|
||||||
(
|
|
||||||
`AutoRoleRuleId` BIGINT NOT NULL AUTO_INCREMENT,
|
|
||||||
`AutoRoleId` BIGINT,
|
|
||||||
`DiscordEmojiName` VARCHAR(64),
|
|
||||||
`DiscordRoleId` BIGINT NOT NULL,
|
|
||||||
`CreatedAt` DATETIME(6),
|
|
||||||
`LastModifiedAt` DATETIME(6),
|
|
||||||
PRIMARY KEY (`AutoRoleRuleId`),
|
|
||||||
FOREIGN KEY (`AutoRoleId`) REFERENCES `AutoRoles` (`AutoRoleId`)
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
|||||||
DROP TABLE `AuthUsers`;
|
|
||||||
|
|
||||||
DROP TABLE `AuthUserUsersRelations`;
|
|
||||||
|
|
@ -1,31 +0,0 @@
|
|||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS `AuthUsers` (
|
|
||||||
`Id` BIGINT NOT NULL AUTO_INCREMENT,
|
|
||||||
`FirstName` VARCHAR(255),
|
|
||||||
`LastName` VARCHAR(255),
|
|
||||||
`EMail` VARCHAR(255),
|
|
||||||
`Password` VARCHAR(255),
|
|
||||||
`PasswordSalt` VARCHAR(255),
|
|
||||||
`RefreshToken` VARCHAR(255),
|
|
||||||
`ConfirmationId` VARCHAR(255) DEFAULT NULL,
|
|
||||||
`ForgotPasswordId` VARCHAR(255) DEFAULT NULL,
|
|
||||||
`OAuthId` VARCHAR(255) DEFAULT NULL,
|
|
||||||
`RefreshTokenExpiryTime` DATETIME(6) NOT NULL,
|
|
||||||
`AuthRole` INT NOT NULL DEFAULT 0,
|
|
||||||
`CreatedAt` DATETIME(6) NOT NULL,
|
|
||||||
`LastModifiedAt` DATETIME(6) NOT NULL,
|
|
||||||
PRIMARY KEY(`Id`)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS `AuthUserUsersRelations`(
|
|
||||||
`Id` BIGINT NOT NULL AUTO_INCREMENT,
|
|
||||||
`AuthUserId` BIGINT DEFAULT NULL,
|
|
||||||
`UserId` BIGINT DEFAULT NULL,
|
|
||||||
`CreatedAt` DATETIME(6) NOT NULL,
|
|
||||||
`LastModifiedAt` DATETIME(6) NOT NULL,
|
|
||||||
PRIMARY KEY(`Id`),
|
|
||||||
FOREIGN KEY (`AuthUserId`) REFERENCES `AuthUsers`(`Id`),
|
|
||||||
FOREIGN KEY (`UserId`) REFERENCES `Users`(`UserId`)
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
|||||||
|
|
||||||
ALTER TABLE AutoRoles DROP COLUMN DiscordChannelId;
|
|
||||||
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
|||||||
|
|
||||||
ALTER TABLE AutoRoles ADD DiscordChannelId BIGINT NOT NULL AFTER ServerId;
|
|
||||||
|
|
||||||
|
|
@ -1,2 +0,0 @@
|
|||||||
DROP TABLE `Levels`;
|
|
||||||
|
|
@ -1,14 +0,0 @@
|
|||||||
CREATE TABLE IF NOT EXISTS `Levels` (
|
|
||||||
`Id` BIGINT NOT NULL AUTO_INCREMENT,
|
|
||||||
`Name` VARCHAR(255) NOT NULL,
|
|
||||||
`Color` VARCHAR(8) NOT NULL,
|
|
||||||
`MinXp` BIGINT NOT NULL,
|
|
||||||
`PermissionInt` BIGINT NOT NULL,
|
|
||||||
`ServerId` BIGINT,
|
|
||||||
`CreatedAt` DATETIME(6),
|
|
||||||
`LastModifiedAt` DATETIME(6),
|
|
||||||
PRIMARY KEY(`Id`),
|
|
||||||
FOREIGN KEY (`ServerId`) REFERENCES `Servers`(`ServerId`)
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
@ -1,2 +0,0 @@
|
|||||||
DROP TABLE `Statistics`;
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
|||||||
CREATE TABLE IF NOT EXISTS `Statistics` (
|
|
||||||
`Id` BIGINT NOT NULL AUTO_INCREMENT,
|
|
||||||
`Name` VARCHAR(255) NOT NULL,
|
|
||||||
`Description` VARCHAR(255) NOT NULL,
|
|
||||||
`Code` LONGTEXT NOT NULL,
|
|
||||||
`ServerId` BIGINT,
|
|
||||||
`CreatedAt` DATETIME(6),
|
|
||||||
`LastModifiedAt` DATETIME(6),
|
|
||||||
PRIMARY KEY(`Id`),
|
|
||||||
FOREIGN KEY (`ServerId`) REFERENCES `Servers`(`ServerId`)
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
@ -1,2 +0,0 @@
|
|||||||
DROP TABLE `UserMessageCountPerHour`;
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
|||||||
CREATE TABLE IF NOT EXISTS `UserMessageCountPerHour` (
|
|
||||||
`Id` BIGINT NOT NULL AUTO_INCREMENT,
|
|
||||||
`Date` DATETIME(6) NOT NULL,
|
|
||||||
`Hour` BIGINT,
|
|
||||||
`XPCount` BIGINT,
|
|
||||||
`UserId` BIGINT,
|
|
||||||
`CreatedAt` DATETIME(6),
|
|
||||||
`LastModifiedAt` DATETIME(6),
|
|
||||||
PRIMARY KEY(`Id`),
|
|
||||||
FOREIGN KEY (`UserId`) REFERENCES `Users`(`UserId`)
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
@ -1,3 +1,6 @@
|
|||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
|
||||||
from cpl_core.console import Console
|
from cpl_core.console import Console
|
||||||
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
|
||||||
@ -18,24 +21,31 @@ class MigrationService:
|
|||||||
self._db = db
|
self._db = db
|
||||||
self._cursor = db.cursor
|
self._cursor = db.cursor
|
||||||
|
|
||||||
self._migrations: List[MigrationABC] = (
|
# self._migrations: List[MigrationABC] = (
|
||||||
List(type, MigrationABC.__subclasses__()).order_by(lambda x: x.name.split("_")[0]).then_by(lambda x: x.prio)
|
# List(type, MigrationABC.__subclasses__()).order_by(lambda x: x.name.split("_")[0]).then_by(lambda x: x.prio)
|
||||||
)
|
# )
|
||||||
|
|
||||||
def migrate(self):
|
def migrate(self):
|
||||||
for migration in self._migrations:
|
path = f"../../src/bot_data/scripts"
|
||||||
migration_id = migration.__name__
|
if not os.path.exists(path):
|
||||||
|
os.makedirs(path)
|
||||||
|
else:
|
||||||
|
shutil.rmtree(path)
|
||||||
|
os.makedirs(path)
|
||||||
|
|
||||||
|
for migration in self._services.get_services(MigrationABC):
|
||||||
|
migration_id = type(migration).__name__
|
||||||
try:
|
try:
|
||||||
migration_as_service: MigrationABC = self._services.get_service(migration)
|
# migration_as_service: MigrationABC = self._services.get_service(migration)
|
||||||
# save upgrade scripts
|
# save upgrade scripts
|
||||||
self._db.set_migration(migration_as_service.name, True)
|
self._db.set_migration(migration.name, True)
|
||||||
migration_as_service.upgrade()
|
migration.upgrade()
|
||||||
self._cursor.execute(MigrationHistory(migration_id).insert_string)
|
self._cursor.execute(MigrationHistory(migration_id).insert_string)
|
||||||
self._db.save_changes()
|
self._db.save_changes()
|
||||||
|
|
||||||
# save downgrade scripts
|
# save downgrade scripts
|
||||||
self._db.set_migration(migration_as_service.name)
|
self._db.set_migration(migration.name)
|
||||||
migration_as_service.downgrade()
|
migration.downgrade()
|
||||||
self._cursor.execute(MigrationHistory(migration_id).insert_string)
|
self._cursor.execute(MigrationHistory(migration_id).insert_string)
|
||||||
self._db.save_changes()
|
self._db.save_changes()
|
||||||
|
|
||||||
|
@ -15,6 +15,11 @@ class MockDBContext(DatabaseContextABC):
|
|||||||
self._migration_name: Optional[str] = None
|
self._migration_name: Optional[str] = None
|
||||||
self._migration_script: Optional[str] = None
|
self._migration_script: Optional[str] = None
|
||||||
|
|
||||||
|
self._old_version: Optional[str] = None
|
||||||
|
self._old_name: Optional[str] = None
|
||||||
|
|
||||||
|
self._index: int = 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def cursor(self) -> MockCursor:
|
def cursor(self) -> MockCursor:
|
||||||
cursor = MockCursor()
|
cursor = MockCursor()
|
||||||
@ -43,11 +48,25 @@ class MockDBContext(DatabaseContextABC):
|
|||||||
if not os.path.exists(path):
|
if not os.path.exists(path):
|
||||||
os.makedirs(path)
|
os.makedirs(path)
|
||||||
|
|
||||||
|
if (
|
||||||
|
self._old_name is not None
|
||||||
|
and self._migration_name is not None
|
||||||
|
and self._old_name.split("_")[0] == self._migration_name.split("_")[0]
|
||||||
|
):
|
||||||
|
pass
|
||||||
|
elif self._old_version == self._migration_version:
|
||||||
|
self._index += 1
|
||||||
|
else:
|
||||||
|
self._index = 1
|
||||||
|
|
||||||
script = textwrap.dedent(self._migration_script)
|
script = textwrap.dedent(self._migration_script)
|
||||||
with open(f"{path}/{self._migration_name}.sql", "w+") as f:
|
with open(f"{path}/{self._index}_{self._migration_name}.sql", "w+") as f:
|
||||||
f.write(script)
|
f.write(script)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
self._old_version = self._migration_version
|
||||||
|
self._old_name = self._migration_name
|
||||||
|
|
||||||
self._migration_name = None
|
self._migration_name = None
|
||||||
self._migration_version = None
|
self._migration_version = None
|
||||||
self._migration_script = None
|
self._migration_script = None
|
||||||
|
Loading…
Reference in New Issue
Block a user