This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea81191
commit dd33acd
Showing
3 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
src/db/migration/m2023_05_18_000001_rename_pkey_index.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters