Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
rename notification primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatsNoMoon committed May 26, 2023
1 parent ea81191 commit dd33acd
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 1 deletion.
150 changes: 150 additions & 0 deletions src/db/migration/m2023_05_18_000001_rename_pkey_index.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
2 changes: 2 additions & 0 deletions src/db/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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),
]
}
}
10 changes: 9 additions & 1 deletion src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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(())
}
Expand Down

0 comments on commit dd33acd

Please sign in to comment.