Add migration for guild IDs to use discord ID
This commit is contained in:
parent
94bfd39085
commit
3d08027325
86
migration/05-restructure-guild-table.sql
Normal file
86
migration/05-restructure-guild-table.sql
Normal file
@ -0,0 +1,86 @@
|
||||
SET FOREIGN_KEY_CHECKS = 0;
|
||||
|
||||
ALTER TABLE channels DROP FOREIGN KEY `channels_ibfk_1`;
|
||||
ALTER TABLE channels ADD CONSTRAINT `guild_id_fk`
|
||||
FOREIGN KEY (`guild_id`)
|
||||
REFERENCES guilds(`id`)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE command_aliases DROP FOREIGN KEY `command_aliases_ibfk_1`;
|
||||
ALTER TABLE command_aliases ADD CONSTRAINT `guild_id_fk`
|
||||
FOREIGN KEY (`guild_id`)
|
||||
REFERENCES guilds(`id`)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE command_restrictions DROP FOREIGN KEY `command_restrictions_ibfk_1`;
|
||||
ALTER TABLE command_restrictions ADD CONSTRAINT `guild_id_fk`
|
||||
FOREIGN KEY (`guild_id`)
|
||||
REFERENCES guilds(`id`)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE events DROP FOREIGN KEY `events_ibfk_1`;
|
||||
ALTER TABLE events ADD CONSTRAINT `guild_id_fk`
|
||||
FOREIGN KEY (`guild_id`)
|
||||
REFERENCES guilds(`id`)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE guild_users DROP FOREIGN KEY `guild_users_ibfk_1`;
|
||||
ALTER TABLE guild_users ADD CONSTRAINT `guild_id_fk`
|
||||
FOREIGN KEY (`guild_id`)
|
||||
REFERENCES guilds(`id`)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE macro DROP FOREIGN KEY `macro_ibfk_1`;
|
||||
ALTER TABLE macro ADD CONSTRAINT `guild_id_fk`
|
||||
FOREIGN KEY (`guild_id`)
|
||||
REFERENCES guilds(`id`)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE roles DROP FOREIGN KEY `roles_ibfk_1`;
|
||||
ALTER TABLE roles ADD CONSTRAINT `guild_id_fk`
|
||||
FOREIGN KEY (`guild_id`)
|
||||
REFERENCES guilds(`id`)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE todos DROP FOREIGN KEY `todos_ibfk_2`;
|
||||
ALTER TABLE todos ADD CONSTRAINT `guild_id_fk`
|
||||
FOREIGN KEY (`guild_id`)
|
||||
REFERENCES guilds(`id`)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE reminder_template DROP FOREIGN KEY ``
|
||||
ALTER TABLE roles ADD CONSTRAINT `guild_id_fk`
|
||||
FOREIGN KEY (`guild_id`)
|
||||
REFERENCES guilds(`id`)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE guilds MODIFY `id` BIGINT UNSIGNED NOT NULL;
|
||||
UPDATE guilds SET `id` = `guild`;
|
||||
ALTER TABLE guilds DROP COLUMN `guild`;
|
||||
ALTER TABLE guilds ADD COLUMN `default_channel` BIGINT UNSIGNED;
|
||||
ALTER TABLE guilds ADD CONSTRAINT `default_channel_fk`
|
||||
FOREIGN KEY (`default_channel`)
|
||||
REFERENCES channels(`id`)
|
||||
ON DELETE SET NULL
|
||||
ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE channels MODIFY `guild_id` BIGINT UNSIGNED;
|
||||
ALTER TABLE command_aliases MODIFY `guild_id` BIGINT UNSIGNED;
|
||||
ALTER TABLE command_restrictions MODIFY `guild_id` BIGINT UNSIGNED;
|
||||
ALTER TABLE events MODIFY `guild_id` BIGINT UNSIGNED;
|
||||
ALTER TABLE guild_users MODIFY `guild_id` BIGINT UNSIGNED;
|
||||
ALTER TABLE macro MODIFY `guild_id` BIGINT UNSIGNED;
|
||||
ALTER TABLE roles MODIFY `guild_id` BIGINT UNSIGNED;
|
||||
ALTER TABLE todos MODIFY `guild_id` BIGINT UNSIGNED;
|
||||
ALTER TABLE reminder_template MODIFY `guild_id` BIGINT UNSIGNED;
|
||||
|
||||
SET FOREIGN_KEY_CHECKS = 1;
|
51
src/models/guild_data.rs
Normal file
51
src/models/guild_data.rs
Normal file
@ -0,0 +1,51 @@
|
||||
use poise::serenity_prelude::{model::channel::Channel, Guild};
|
||||
use sqlx::MySqlPool;
|
||||
|
||||
pub struct GuildData {
|
||||
pub id: u64,
|
||||
pub default_channel: Option<u64>,
|
||||
}
|
||||
|
||||
impl GuildData {
|
||||
pub async fn from_guild(guild: &Guild, pool: &MySqlPool) -> Result<Self, sqlx::Error> {
|
||||
let guild_id = guild.id.0;
|
||||
|
||||
if let Ok(row) = sqlx::query_as_unchecked!(
|
||||
Self,
|
||||
"
|
||||
SELECT id, default_channel FROM guilds WHERE id = ?
|
||||
",
|
||||
guild_id
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
{
|
||||
Ok(row)
|
||||
} else {
|
||||
sqlx::query!(
|
||||
"
|
||||
INSERT IGNORE INTO guilds (id) VALUES (?)
|
||||
",
|
||||
guild_id
|
||||
)
|
||||
.execute(&pool.clone())
|
||||
.await?;
|
||||
|
||||
Ok(Self { id: guild_id, default_channel: None })
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn commit_changes(&self, pool: &MySqlPool) -> Result<(), sqlx::Error> {
|
||||
sqlx::query!(
|
||||
"
|
||||
UPDATE guilds SET default_channel = ? WHERE id = ?
|
||||
",
|
||||
self.default_channel,
|
||||
self.id
|
||||
)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
pub mod channel_data;
|
||||
pub mod command_macro;
|
||||
pub mod guild_data;
|
||||
pub mod reminder;
|
||||
pub mod timer;
|
||||
pub mod user_data;
|
||||
|
Loading…
Reference in New Issue
Block a user