From 3ef8584189eeca83d8619225aa6f6836cff57d4e Mon Sep 17 00:00:00 2001 From: jude Date: Fri, 24 Mar 2023 11:11:51 +0000 Subject: [PATCH] Use SQLx migrations --- Cargo.toml | 2 +- migration/03-reminder_variable_intervals.sql | 4 - .../05-reminder_variable_intervals_2.sql | 3 - .../20210603000000_initial.sql | 74 +++++++++---------- .../20210623000000_reminder_message_embed.sql | 2 - .../20210922000000_macro.sql | 2 - ...0201000000_reminder_variable_intervals.sql | 2 + .../20220211000000_reminder_templates.sql | 2 - ...0221210000000_reminder_daily_intervals.sql | 1 + src/main.rs | 2 + 10 files changed, 41 insertions(+), 53 deletions(-) delete mode 100644 migration/03-reminder_variable_intervals.sql delete mode 100644 migration/05-reminder_variable_intervals_2.sql rename migration/00-initial.sql => migrations/20210603000000_initial.sql (69%) rename migration/01-reminder_message_embed.sql => migrations/20210623000000_reminder_message_embed.sql (99%) rename migration/02-macro.sql => migrations/20210922000000_macro.sql (94%) create mode 100644 migrations/20220201000000_reminder_variable_intervals.sql rename migration/04-reminder_templates.sql => migrations/20220211000000_reminder_templates.sql (98%) create mode 100644 migrations/20221210000000_reminder_daily_intervals.sql diff --git a/Cargo.toml b/Cargo.toml index 5b130fc..1f0c577 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ serde_repr = "0.1" rmp-serde = "1.1" rand = "0.8" levenshtein = "1.0" -sqlx = { version = "0.6", features = ["runtime-tokio-rustls", "macros", "mysql", "bigdecimal", "chrono"]} +sqlx = { version = "0.6", features = ["runtime-tokio-rustls", "macros", "mysql", "bigdecimal", "chrono", "migrate"]} base64 = "0.13" [dependencies.postman] diff --git a/migration/03-reminder_variable_intervals.sql b/migration/03-reminder_variable_intervals.sql deleted file mode 100644 index d539db4..0000000 --- a/migration/03-reminder_variable_intervals.sql +++ /dev/null @@ -1,4 +0,0 @@ -USE reminders; - -ALTER TABLE reminders.reminders RENAME COLUMN `interval` TO `interval_seconds`; -ALTER TABLE reminders.reminders ADD COLUMN `interval_months` INT UNSIGNED DEFAULT NULL; diff --git a/migration/05-reminder_variable_intervals_2.sql b/migration/05-reminder_variable_intervals_2.sql deleted file mode 100644 index a6b8047..0000000 --- a/migration/05-reminder_variable_intervals_2.sql +++ /dev/null @@ -1,3 +0,0 @@ -USE reminders; - -ALTER TABLE reminders.reminders ADD COLUMN `interval_days` INT UNSIGNED DEFAULT NULL; diff --git a/migration/00-initial.sql b/migrations/20210603000000_initial.sql similarity index 69% rename from migration/00-initial.sql rename to migrations/20210603000000_initial.sql index f936746..458fd64 100644 --- a/migration/00-initial.sql +++ b/migrations/20210603000000_initial.sql @@ -1,10 +1,6 @@ -CREATE DATABASE IF NOT EXISTS reminders; - SET FOREIGN_KEY_CHECKS=0; -USE reminders; - -CREATE TABLE reminders.guilds ( +CREATE TABLE guilds ( id INT UNSIGNED UNIQUE NOT NULL AUTO_INCREMENT, guild BIGINT UNSIGNED UNIQUE NOT NULL, @@ -21,7 +17,7 @@ CREATE TABLE reminders.guilds ( FOREIGN KEY (default_channel_id) REFERENCES reminders.channels(id) ON DELETE SET NULL ); -CREATE TABLE reminders.channels ( +CREATE TABLE channels ( id INT UNSIGNED UNIQUE NOT NULL AUTO_INCREMENT, channel BIGINT UNSIGNED UNIQUE NOT NULL, @@ -42,7 +38,7 @@ CREATE TABLE reminders.channels ( FOREIGN KEY (guild_id) REFERENCES reminders.guilds(id) ON DELETE CASCADE ); -CREATE TABLE reminders.users ( +CREATE TABLE users ( id INT UNSIGNED AUTO_INCREMENT UNIQUE NOT NULL, user BIGINT UNSIGNED UNIQUE NOT NULL, @@ -62,7 +58,7 @@ CREATE TABLE reminders.users ( FOREIGN KEY (dm_channel) REFERENCES reminders.channels(id) ON DELETE RESTRICT ); -CREATE TABLE reminders.roles ( +CREATE TABLE roles ( id INT UNSIGNED UNIQUE NOT NULL AUTO_INCREMENT, role BIGINT UNSIGNED UNIQUE NOT NULL, @@ -74,7 +70,7 @@ CREATE TABLE reminders.roles ( FOREIGN KEY (guild_id) REFERENCES reminders.guilds(id) ON DELETE CASCADE ); -CREATE TABLE reminders.embeds ( +CREATE TABLE embeds ( id INT UNSIGNED AUTO_INCREMENT UNIQUE NOT NULL, title VARCHAR(256) NOT NULL DEFAULT '', @@ -91,7 +87,7 @@ CREATE TABLE reminders.embeds ( PRIMARY KEY (id) ); -CREATE TABLE reminders.embed_fields ( +CREATE TABLE embed_fields ( id INT UNSIGNED AUTO_INCREMENT UNIQUE NOT NULL, title VARCHAR(256) NOT NULL DEFAULT '', @@ -100,10 +96,10 @@ CREATE TABLE reminders.embed_fields ( embed_id INT UNSIGNED NOT NULL, PRIMARY KEY (id), - FOREIGN KEY (embed_id) REFERENCES reminders.embeds(id) ON DELETE CASCADE + FOREIGN KEY (embed_id) REFERENCES embeds(id) ON DELETE CASCADE ); -CREATE TABLE reminders.messages ( +CREATE TABLE messages ( id INT UNSIGNED AUTO_INCREMENT UNIQUE NOT NULL, content VARCHAR(2048) NOT NULL DEFAULT '', @@ -114,10 +110,10 @@ CREATE TABLE reminders.messages ( attachment_name VARCHAR(260), PRIMARY KEY (id), - FOREIGN KEY (embed_id) REFERENCES reminders.embeds(id) ON DELETE SET NULL + FOREIGN KEY (embed_id) REFERENCES embeds(id) ON DELETE SET NULL ); -CREATE TABLE reminders.reminders ( +CREATE TABLE reminders ( id INT UNSIGNED AUTO_INCREMENT UNIQUE NOT NULL, uid VARCHAR(64) UNIQUE NOT NULL, @@ -140,20 +136,20 @@ CREATE TABLE reminders.reminders ( set_by INT UNSIGNED, PRIMARY KEY (id), - FOREIGN KEY (message_id) REFERENCES reminders.messages(id) ON DELETE RESTRICT, - FOREIGN KEY (channel_id) REFERENCES reminders.channels(id) ON DELETE CASCADE, - FOREIGN KEY (set_by) REFERENCES reminders.users(id) ON DELETE SET NULL + FOREIGN KEY (message_id) REFERENCES messages(id) ON DELETE RESTRICT, + FOREIGN KEY (channel_id) REFERENCES channels(id) ON DELETE CASCADE, + FOREIGN KEY (set_by) REFERENCES users(id) ON DELETE SET NULL ); -CREATE TRIGGER message_cleanup AFTER DELETE ON reminders.reminders +CREATE TRIGGER message_cleanup AFTER DELETE ON reminders FOR EACH ROW - DELETE FROM reminders.messages WHERE id = OLD.message_id; + DELETE FROM messages WHERE id = OLD.message_id; -CREATE TRIGGER embed_cleanup AFTER DELETE ON reminders.messages +CREATE TRIGGER embed_cleanup AFTER DELETE ON messages FOR EACH ROW - DELETE FROM reminders.embeds WHERE id = OLD.embed_id; + DELETE FROM embeds WHERE id = OLD.embed_id; -CREATE TABLE reminders.todos ( +CREATE TABLE todos ( id INT UNSIGNED AUTO_INCREMENT UNIQUE NOT NULL, user_id INT UNSIGNED, guild_id INT UNSIGNED, @@ -161,23 +157,23 @@ CREATE TABLE reminders.todos ( value VARCHAR(2000) NOT NULL, PRIMARY KEY (id), - FOREIGN KEY (user_id) REFERENCES reminders.users(id) ON DELETE SET NULL, - FOREIGN KEY (guild_id) REFERENCES reminders.guilds(id) ON DELETE CASCADE, - FOREIGN KEY (channel_id) REFERENCES reminders.channels(id) ON DELETE SET NULL + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL, + FOREIGN KEY (guild_id) REFERENCES guilds(id) ON DELETE CASCADE, + FOREIGN KEY (channel_id) REFERENCES channels(id) ON DELETE SET NULL ); -CREATE TABLE reminders.command_restrictions ( +CREATE TABLE command_restrictions ( id INT UNSIGNED AUTO_INCREMENT UNIQUE NOT NULL, role_id INT UNSIGNED NOT NULL, command ENUM('todos', 'natural', 'remind', 'interval', 'timer', 'del', 'look', 'alias', 'countdown') NOT NULL, PRIMARY KEY (id), - FOREIGN KEY (role_id) REFERENCES reminders.roles(id) ON DELETE CASCADE, + FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE CASCADE, UNIQUE KEY (`role_id`, `command`) ); -CREATE TABLE reminders.timers ( +CREATE TABLE timers ( id INT UNSIGNED AUTO_INCREMENT UNIQUE NOT NULL, start_time TIMESTAMP NOT NULL DEFAULT NOW(), name VARCHAR(32) NOT NULL, @@ -186,7 +182,7 @@ CREATE TABLE reminders.timers ( PRIMARY KEY (id) ); -CREATE TABLE reminders.events ( +CREATE TABLE events ( id INT UNSIGNED AUTO_INCREMENT UNIQUE NOT NULL, `time` TIMESTAMP NOT NULL DEFAULT NOW(), @@ -198,12 +194,12 @@ CREATE TABLE reminders.events ( reminder_id INT UNSIGNED, PRIMARY KEY (id), - FOREIGN KEY (guild_id) REFERENCES reminders.guilds(id) ON DELETE CASCADE, - FOREIGN KEY (user_id) REFERENCES reminders.users(id) ON DELETE SET NULL, - FOREIGN KEY (reminder_id) REFERENCES reminders.reminders(id) ON DELETE SET NULL + FOREIGN KEY (guild_id) REFERENCES guilds(id) ON DELETE CASCADE, + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL, + FOREIGN KEY (reminder_id) REFERENCES reminders(id) ON DELETE SET NULL ); -CREATE TABLE reminders.command_aliases ( +CREATE TABLE command_aliases ( id INT UNSIGNED AUTO_INCREMENT UNIQUE NOT NULL, guild_id INT UNSIGNED NOT NULL, @@ -212,22 +208,22 @@ CREATE TABLE reminders.command_aliases ( command VARCHAR(2048) NOT NULL, PRIMARY KEY (id), - FOREIGN KEY (guild_id) REFERENCES reminders.guilds(id) ON DELETE CASCADE, + FOREIGN KEY (guild_id) REFERENCES guilds(id) ON DELETE CASCADE, UNIQUE KEY (`guild_id`, `name`) ); -CREATE TABLE reminders.guild_users ( +CREATE TABLE guild_users ( guild INT UNSIGNED NOT NULL, user INT UNSIGNED NOT NULL, can_access BOOL NOT NULL DEFAULT 0, - FOREIGN KEY (guild) REFERENCES reminders.guilds(id) ON DELETE CASCADE, - FOREIGN KEY (user) REFERENCES reminders.users(id) ON DELETE CASCADE, + FOREIGN KEY (guild) REFERENCES guilds(id) ON DELETE CASCADE, + FOREIGN KEY (user) REFERENCES users(id) ON DELETE CASCADE, UNIQUE KEY (guild, user) ); -CREATE EVENT reminders.event_cleanup +CREATE EVENT event_cleanup ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY ON COMPLETION PRESERVE -DO DELETE FROM reminders.events WHERE `time` < DATE_SUB(NOW(), INTERVAL 5 DAY); +DO DELETE FROM events WHERE `time` < DATE_SUB(NOW(), INTERVAL 5 DAY); diff --git a/migration/01-reminder_message_embed.sql b/migrations/20210623000000_reminder_message_embed.sql similarity index 99% rename from migration/01-reminder_message_embed.sql rename to migrations/20210623000000_reminder_message_embed.sql index 23df3c0..e6b83cb 100644 --- a/migration/01-reminder_message_embed.sql +++ b/migrations/20210623000000_reminder_message_embed.sql @@ -1,5 +1,3 @@ -USE reminders; - SET FOREIGN_KEY_CHECKS = 0; DROP TABLE IF EXISTS reminders_new; diff --git a/migration/02-macro.sql b/migrations/20210922000000_macro.sql similarity index 94% rename from migration/02-macro.sql rename to migrations/20210922000000_macro.sql index a9284e9..625eb8a 100644 --- a/migration/02-macro.sql +++ b/migrations/20210922000000_macro.sql @@ -1,5 +1,3 @@ -USE reminders; - CREATE TABLE macro ( id INT UNSIGNED AUTO_INCREMENT, guild_id INT UNSIGNED NOT NULL, diff --git a/migrations/20220201000000_reminder_variable_intervals.sql b/migrations/20220201000000_reminder_variable_intervals.sql new file mode 100644 index 0000000..afe1a61 --- /dev/null +++ b/migrations/20220201000000_reminder_variable_intervals.sql @@ -0,0 +1,2 @@ +ALTER TABLE reminders RENAME COLUMN `interval` TO `interval_seconds`; +ALTER TABLE reminders ADD COLUMN `interval_months` INT UNSIGNED DEFAULT NULL; diff --git a/migration/04-reminder_templates.sql b/migrations/20220211000000_reminder_templates.sql similarity index 98% rename from migration/04-reminder_templates.sql rename to migrations/20220211000000_reminder_templates.sql index 28afe79..b81a0e1 100644 --- a/migration/04-reminder_templates.sql +++ b/migrations/20220211000000_reminder_templates.sql @@ -1,5 +1,3 @@ -USE reminders; - CREATE TABLE reminder_template ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, diff --git a/migrations/20221210000000_reminder_daily_intervals.sql b/migrations/20221210000000_reminder_daily_intervals.sql new file mode 100644 index 0000000..c14ec9e --- /dev/null +++ b/migrations/20221210000000_reminder_daily_intervals.sql @@ -0,0 +1 @@ +ALTER TABLE reminders ADD COLUMN `interval_days` INT UNSIGNED DEFAULT NULL; diff --git a/src/main.rs b/src/main.rs index c0e4760..55ac01a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -168,6 +168,8 @@ async fn _main(tx: Sender<()>) -> Result<(), Box> { let database = Pool::connect(&env::var("DATABASE_URL").expect("No database URL provided")).await.unwrap(); + sqlx::migrate!().run(&database).await?; + let popular_timezones = sqlx::query!( "SELECT IFNULL(timezone, 'UTC') AS timezone FROM users