From dd33acd066661d8b260d851c0ba922039c40970f Mon Sep 17 00:00:00 2001 From: ThatsNoMoon Date: Thu, 18 May 2023 15:23:28 -0600 Subject: [PATCH] rename notification primary key --- .../m2023_05_18_000001_rename_pkey_index.rs | 150 ++++++++++++++++++ src/db/migration/mod.rs | 2 + src/db/mod.rs | 10 +- 3 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 src/db/migration/m2023_05_18_000001_rename_pkey_index.rs diff --git a/src/db/migration/m2023_05_18_000001_rename_pkey_index.rs b/src/db/migration/m2023_05_18_000001_rename_pkey_index.rs new file mode 100644 index 0000000..956af3d --- /dev/null +++ b/src/db/migration/m2023_05_18_000001_rename_pkey_index.rs @@ -0,0 +1,150 @@ +use sea_orm::sea_query::{Alias, Index, Query}; +use sea_orm_migration::prelude::{ + async_trait, ColumnDef, DbErr, DeriveMigrationName, MigrationTrait, + SchemaManager, Table, +}; + +use crate::db::notification::{self, Column}; + +#[derive(DeriveMigrationName)] +pub(crate) struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + let tmp_table = Alias::new("__migrated_sent_notifications"); + manager + .create_table( + Table::create() + .table(tmp_table.clone()) + .col( + ColumnDef::new(Column::UserId).big_integer().not_null(), + ) + .col( + ColumnDef::new(Column::OriginalMessage) + .big_integer() + .not_null(), + ) + .col( + ColumnDef::new(Column::NotificationMessage) + .big_integer() + .not_null(), + ) + .col(ColumnDef::new(Column::Keyword).string().not_null()) + .primary_key( + Index::create() + .name("sent_notifications_pkey") + .col(Column::NotificationMessage) + .col(Column::Keyword), + ) + .to_owned(), + ) + .await?; + + manager + .exec_stmt( + Query::insert() + .into_table(tmp_table.clone()) + .columns([ + Column::UserId, + Column::OriginalMessage, + Column::NotificationMessage, + Column::Keyword, + ]) + .select_from( + Query::select() + .from(notification::Entity) + .columns([ + Column::UserId, + Column::OriginalMessage, + Column::NotificationMessage, + Column::Keyword, + ]) + .to_owned(), + ) + .map_err(|e| DbErr::Migration(e.to_string()))? + .to_owned(), + ) + .await?; + + manager + .drop_table(Table::drop().table(notification::Entity).to_owned()) + .await?; + + manager + .rename_table( + Table::rename() + .table(tmp_table, notification::Entity) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + let tmp_table = Alias::new("__migrated_sent_notifications"); + manager + .create_table( + Table::create() + .table(tmp_table.clone()) + .col( + ColumnDef::new(Column::UserId).big_integer().not_null(), + ) + .col( + ColumnDef::new(Column::OriginalMessage) + .big_integer() + .not_null(), + ) + .col( + ColumnDef::new(Column::NotificationMessage) + .big_integer() + .not_null(), + ) + .col(ColumnDef::new(Column::Keyword).string().not_null()) + .primary_key( + Index::create() + .col(Column::NotificationMessage) + .col(Column::Keyword), + ) + .to_owned(), + ) + .await?; + + manager + .exec_stmt( + Query::insert() + .into_table(tmp_table.clone()) + .columns([ + Column::UserId, + Column::OriginalMessage, + Column::NotificationMessage, + Column::Keyword, + ]) + .select_from( + Query::select() + .from(notification::Entity) + .columns([ + Column::UserId, + Column::OriginalMessage, + Column::NotificationMessage, + Column::Keyword, + ]) + .to_owned(), + ) + .map_err(|e| DbErr::Migration(e.to_string()))? + .to_owned(), + ) + .await?; + + manager + .drop_table(Table::drop().table(notification::Entity).to_owned()) + .await?; + + manager + .rename_table( + Table::rename() + .table(tmp_table, notification::Entity) + .to_owned(), + ) + .await + } +} diff --git a/src/db/migration/mod.rs b/src/db/migration/mod.rs index b36b3a6..2d508be 100644 --- a/src/db/migration/mod.rs +++ b/src/db/migration/mod.rs @@ -2,6 +2,7 @@ use sea_orm_migration::{MigrationTrait, MigratorTrait}; mod m2022_08_04_000001_init; mod m2023_01_08_000001_composite_notification_key; +mod m2023_05_18_000001_rename_pkey_index; pub(crate) struct Migrator; @@ -10,6 +11,7 @@ impl MigratorTrait for Migrator { vec![ Box::new(m2022_08_04_000001_init::Migration), Box::new(m2023_01_08_000001_composite_notification_key::Migration), + Box::new(m2023_05_18_000001_rename_pkey_index::Migration), ] } } diff --git a/src/db/mod.rs b/src/db/mod.rs index 1f06fbc..7b52af8 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -27,6 +27,7 @@ use once_cell::sync::OnceCell; use sea_orm::{Database, DatabaseConnection}; use sea_orm_migration::MigratorTrait; use serenity::model::id::{ChannelId, GuildId, MessageId, UserId}; +use tracing::info; use self::migration::Migrator; #[cfg(feature = "bot")] @@ -114,7 +115,14 @@ pub(crate) async fn init() -> Result<()> { #[cfg(not(feature = "sqlite"))] init_connection(settings().database.url.to_string()).await?; - Migrator::up(connection(), None).await?; + let conn = connection(); + + let migrations = Migrator::get_pending_migrations(conn).await?.len(); + + if migrations > 0 { + info!("Applying {migrations} database migrations"); + Migrator::up(conn, None).await?; + } Ok(()) }