2022-08-30 15:00:58 +00:00
|
|
|
|
using MySqlConnector;
|
|
|
|
|
using System;
|
|
|
|
|
|
2023-02-10 06:07:59 +00:00
|
|
|
|
namespace SharpChat.EventStorage {
|
|
|
|
|
public partial class MariaDBEventStorage {
|
|
|
|
|
private void DoMigration(string name, Action action) {
|
2024-05-10 19:18:55 +00:00
|
|
|
|
bool done = (long?)RunQueryValue(
|
2023-02-08 03:17:07 +00:00
|
|
|
|
"SELECT COUNT(*) FROM `sqc_migrations` WHERE `migration_name` = @name",
|
|
|
|
|
new MySqlParameter("name", name)
|
2022-08-30 15:00:58 +00:00
|
|
|
|
) > 0;
|
2023-02-07 15:01:56 +00:00
|
|
|
|
if(!done) {
|
2023-02-08 03:17:07 +00:00
|
|
|
|
Logger.Write($"Running migration '{name}'...");
|
2022-08-30 15:00:58 +00:00
|
|
|
|
action();
|
|
|
|
|
RunCommand(
|
2023-02-08 03:17:07 +00:00
|
|
|
|
"INSERT INTO `sqc_migrations` (`migration_name`) VALUES (@name)",
|
|
|
|
|
new MySqlParameter("name", name)
|
2022-08-30 15:00:58 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 06:07:59 +00:00
|
|
|
|
public void RunMigrations() {
|
2022-08-30 15:00:58 +00:00
|
|
|
|
RunCommand(
|
2023-02-08 03:17:07 +00:00
|
|
|
|
"CREATE TABLE IF NOT EXISTS `sqc_migrations` ("
|
|
|
|
|
+ "`migration_name` VARCHAR(255) NOT NULL,"
|
|
|
|
|
+ "`migration_completed` TIMESTAMP NOT NULL DEFAULT current_timestamp(),"
|
|
|
|
|
+ "UNIQUE INDEX `migration_name` (`migration_name`),"
|
|
|
|
|
+ "INDEX `migration_completed` (`migration_completed`)"
|
|
|
|
|
+ ") COLLATE='utf8mb4_unicode_ci' ENGINE=InnoDB;"
|
2022-08-30 15:00:58 +00:00
|
|
|
|
);
|
|
|
|
|
|
2023-02-08 03:17:07 +00:00
|
|
|
|
DoMigration("create_events_table", CreateEventsTable);
|
2023-07-23 21:31:13 +00:00
|
|
|
|
DoMigration("allow_null_target", AllowNullTarget);
|
2024-05-10 19:23:19 +00:00
|
|
|
|
DoMigration("update_event_type_names", UpdateEventTypeNames);
|
2024-05-24 00:23:31 +00:00
|
|
|
|
DoMigration("deprecate_event_flags", DeprecateEventFlags);
|
2024-05-24 16:01:11 +00:00
|
|
|
|
DoMigration("update_collations_and_use_json_type", UpdateCollationsAndUseJsonType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateCollationsAndUseJsonType() {
|
|
|
|
|
RunCommand("UPDATE sqc_events SET event_target = LOWER(event_target)");
|
|
|
|
|
RunCommand("UPDATE sqc_events SET event_sender_nick = NULL WHERE event_sender_nick = ''");
|
|
|
|
|
RunCommand(
|
|
|
|
|
"ALTER TABLE `sqc_events` COLLATE='utf8mb4_unicode_520_ci',"
|
|
|
|
|
+ " CHANGE COLUMN `event_type` `event_type` VARCHAR(255) NOT NULL COLLATE 'ascii_general_ci' AFTER `event_id`,"
|
|
|
|
|
+ " CHANGE COLUMN `event_created` `event_created` TIMESTAMP NOT NULL DEFAULT current_timestamp() AFTER `event_type`,"
|
|
|
|
|
+ " CHANGE COLUMN `event_deleted` `event_deleted` TIMESTAMP NULL DEFAULT NULL AFTER `event_created`,"
|
|
|
|
|
+ " CHANGE COLUMN `event_target` `event_channel` VARCHAR(255) NULL DEFAULT NULL COLLATE 'ascii_general_ci' AFTER `event_deleted`,"
|
|
|
|
|
+ " CHANGE COLUMN `event_sender_name` `event_sender_name` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8mb4_unicode_520_ci' AFTER `event_sender`,"
|
|
|
|
|
+ " CHANGE COLUMN `event_sender_nick` `event_sender_nick` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8mb4_unicode_520_ci' AFTER `event_sender_rank`,"
|
|
|
|
|
+ " CHANGE COLUMN `event_data` `event_data` JSON NOT NULL DEFAULT '{}' AFTER `event_sender_perms`,"
|
|
|
|
|
+ " DROP INDEX `event_target`, ADD INDEX `event_channel` (`event_channel`)"
|
|
|
|
|
);
|
2024-05-24 00:23:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DeprecateEventFlags() {
|
|
|
|
|
// StoredEventFlags.Action is just a field in the data object
|
|
|
|
|
RunCommand(@"UPDATE sqc_events SET event_data = JSON_MERGE_PATCH(event_data, JSON_OBJECT('action', true)) WHERE event_flags & 1");
|
|
|
|
|
|
|
|
|
|
// StoredEventFlags.Broadcast can be implied by just having a NULL as the channel name
|
|
|
|
|
RunCommand(@"UPDATE sqc_events SET event_target = NULL WHERE event_flags & 2");
|
|
|
|
|
|
|
|
|
|
// StoredEventFlags.Log was never meaningfully used by anything and basically just meant "not-msg:add"
|
|
|
|
|
// StoredEventFlags.Private was also never meaningfully used, can be determined by checking if the channel name starts with @
|
|
|
|
|
RunCommand(@"ALTER TABLE sqc_events DROP COLUMN event_flags");
|
2024-05-10 19:23:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateEventTypeNames() {
|
|
|
|
|
RunCommand(@"UPDATE sqc_events SET event_type = ""msg:add"" WHERE event_type = ""SharpChat.Events.ChatMessage""");
|
|
|
|
|
RunCommand(@"UPDATE sqc_events SET event_type = ""user:connect"" WHERE event_type = ""SharpChat.Events.UserConnectEvent""");
|
|
|
|
|
RunCommand(@"UPDATE sqc_events SET event_type = ""user:disconnect"" WHERE event_type = ""SharpChat.Events.UserDisconnectEvent""");
|
|
|
|
|
RunCommand(@"UPDATE sqc_events SET event_type = ""chan:join"" WHERE event_type = ""SharpChat.Events.UserChannelJoinEvent""");
|
|
|
|
|
RunCommand(@"UPDATE sqc_events SET event_type = ""chan:leave"" WHERE event_type = ""SharpChat.Events.UserChannelLeaveEvent""");
|
2023-07-23 21:31:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AllowNullTarget() {
|
|
|
|
|
RunCommand(
|
|
|
|
|
"ALTER TABLE `sqc_events`"
|
|
|
|
|
+ " CHANGE COLUMN `event_target` `event_target` VARBINARY(255) NULL AFTER `event_type`;"
|
|
|
|
|
);
|
2022-08-30 15:00:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 06:07:59 +00:00
|
|
|
|
private void CreateEventsTable() {
|
2022-08-30 15:00:58 +00:00
|
|
|
|
RunCommand(
|
2023-02-08 03:17:07 +00:00
|
|
|
|
"CREATE TABLE `sqc_events` ("
|
|
|
|
|
+ "`event_id` BIGINT(20) NOT NULL,"
|
|
|
|
|
+ "`event_sender` BIGINT(20) UNSIGNED NULL DEFAULT NULL,"
|
|
|
|
|
+ "`event_sender_name` VARCHAR(255) NULL DEFAULT NULL,"
|
|
|
|
|
+ "`event_sender_colour` INT(11) NULL DEFAULT NULL,"
|
|
|
|
|
+ "`event_sender_rank` INT(11) NULL DEFAULT NULL,"
|
|
|
|
|
+ "`event_sender_nick` VARCHAR(255) NULL DEFAULT NULL,"
|
|
|
|
|
+ "`event_sender_perms` INT(11) NULL DEFAULT NULL,"
|
|
|
|
|
+ "`event_created` TIMESTAMP NOT NULL DEFAULT current_timestamp(),"
|
|
|
|
|
+ "`event_deleted` TIMESTAMP NULL DEFAULT NULL,"
|
|
|
|
|
+ "`event_type` VARBINARY(255) NOT NULL,"
|
|
|
|
|
+ "`event_target` VARBINARY(255) NOT NULL,"
|
|
|
|
|
+ "`event_flags` TINYINT(3) UNSIGNED NOT NULL,"
|
|
|
|
|
+ "`event_data` BLOB NULL DEFAULT NULL,"
|
|
|
|
|
+ "PRIMARY KEY (`event_id`),"
|
|
|
|
|
+ "INDEX `event_target` (`event_target`),"
|
|
|
|
|
+ "INDEX `event_type` (`event_type`),"
|
|
|
|
|
+ "INDEX `event_sender` (`event_sender`),"
|
|
|
|
|
+ "INDEX `event_datetime` (`event_created`),"
|
|
|
|
|
+ "INDEX `event_deleted` (`event_deleted`)"
|
|
|
|
|
+ ") COLLATE='utf8mb4_unicode_ci' ENGINE=InnoDB;"
|
2022-08-30 15:00:58 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|