Skip to content

Commit

Permalink
Add missing index (helius-labs#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmantica11 authored Sep 14, 2024
1 parent 6fadf13 commit 1e5e657
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/migration/m20240914_000005_init.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}
2 changes: 2 additions & 0 deletions src/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
]
}
}

0 comments on commit 1e5e657

Please sign in to comment.