From 1e5e65772181996d8b3e0174524e75c8d8ef693e Mon Sep 17 00:00:00 2001 From: pmantica11 <151664502+pmantica11@users.noreply.github.com> Date: Sat, 14 Sep 2024 11:12:00 -0400 Subject: [PATCH] Add missing index (#204) --- src/migration/m20240914_000005_init.rs | 58 ++++++++++++++++++++++++++ src/migration/mod.rs | 2 + 2 files changed, 60 insertions(+) create mode 100644 src/migration/m20240914_000005_init.rs diff --git a/src/migration/m20240914_000005_init.rs b/src/migration/m20240914_000005_init.rs new file mode 100644 index 0000000..ff55287 --- /dev/null +++ b/src/migration/m20240914_000005_init.rs @@ -0,0 +1,58 @@ +use sea_orm_migration::prelude::*; +use sea_orm_migration::sea_orm::{ConnectionTrait, DatabaseBackend, Statement}; + +use crate::migration::model::table::AccountTransactions; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +async fn execute_sql<'a>(manager: &SchemaManager<'_>, sql: &str) -> Result<(), DbErr> { + manager + .get_connection() + .execute(Statement::from_string( + manager.get_database_backend(), + sql.to_string(), + )) + .await?; + Ok(()) +} + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + if manager.get_database_backend() == DatabaseBackend::Postgres { + // Create index concurrently for Postgres + execute_sql( + manager, + "CREATE INDEX CONCURRENTLY IF NOT EXISTS account_transactions_signature_idx ON account_transactions (signature);", + ) + .await?; + } else { + // For other databases, create index normally + manager + .create_index( + Index::create() + .name("account_transactions_signature_idx") + .table(AccountTransactions::Table) + .col(AccountTransactions::Signature) + .to_owned(), + ) + .await?; + } + + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_index( + Index::drop() + .name("account_transactions_signature_idx") + .table(AccountTransactions::Table) + .to_owned(), + ) + .await?; + + Ok(()) + } +} diff --git a/src/migration/mod.rs b/src/migration/mod.rs index b3b098e..c77359d 100644 --- a/src/migration/mod.rs +++ b/src/migration/mod.rs @@ -4,6 +4,7 @@ mod m20220101_000001_init; mod m20240623_000002_init; mod m20240624_000003_init; mod m20240807_000004_init; +mod m20240914_000005_init; mod model; pub struct Migrator; @@ -16,6 +17,7 @@ impl MigratorTrait for Migrator { Box::new(m20240623_000002_init::Migration), Box::new(m20240624_000003_init::Migration), Box::new(m20240807_000004_init::Migration), + Box::new(m20240914_000005_init::Migration), ] } }