From b029e87fd90b109dbdeb565b49b68a60f4d6a216 Mon Sep 17 00:00:00 2001 From: Diptesh Choudhuri Date: Thu, 14 Sep 2023 13:37:33 +0530 Subject: [PATCH] Check if an index exists (#1828) * feat: add method to check for index * tests(sea-orm-migrations): add index to cake name * tests(sea-orm-migrations): check if `has_index` works * Update sea-orm-migration/tests/common/migration/m20220118_000001_create_cake_table.rs * Update Cargo.toml --------- Co-authored-by: Chris Tsang --- sea-orm-migration/Cargo.toml | 2 +- sea-orm-migration/src/manager.rs | 21 +++++++++++++++++++ .../m20220118_000001_create_cake_table.rs | 13 +++++++++++- sea-orm-migration/tests/main.rs | 3 +++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/sea-orm-migration/Cargo.toml b/sea-orm-migration/Cargo.toml index 3722b9ce4..ae3f2c75e 100644 --- a/sea-orm-migration/Cargo.toml +++ b/sea-orm-migration/Cargo.toml @@ -25,7 +25,7 @@ clap = { version = "4.3", features = ["env", "derive"], optional = true } dotenvy = { version = "0.15", default-features = false, optional = true } sea-orm = { version = "0.12.2", path = "../", default-features = false, features = ["macros"] } sea-orm-cli = { version = "0.12.2", path = "../sea-orm-cli", default-features = false, optional = true } -sea-schema = { version = "0.14.0" } +sea-schema = { version = "0.14.1" } tracing = { version = "0.1", default-features = false, features = ["log"] } tracing-subscriber = { version = "0.3.17", default-features = false, features = ["env-filter", "fmt"] } futures = { version = "0.3", default-features = false, features = ["std"] } diff --git a/sea-orm-migration/src/manager.rs b/sea-orm-migration/src/manager.rs index f70c9212a..994204e14 100644 --- a/sea-orm-migration/src/manager.rs +++ b/sea-orm-migration/src/manager.rs @@ -136,4 +136,25 @@ impl<'c> SchemaManager<'c> { res.try_get("", "has_column") } + + pub async fn has_index(&self, table: T, index: I) -> Result + where + T: AsRef, + I: AsRef, + { + let stmt = match self.conn.get_database_backend() { + DbBackend::MySql => MySql::has_index(table, index), + DbBackend::Postgres => Postgres::has_index(table, index), + DbBackend::Sqlite => Sqlite::has_index(table, index), + }; + + let builder = self.conn.get_database_backend(); + let res = self + .conn + .query_one(builder.build(&stmt)) + .await? + .ok_or_else(|| DbErr::Custom("Failed to check index exists".to_owned()))?; + + res.try_get("", "has_index") + } } diff --git a/sea-orm-migration/tests/common/migration/m20220118_000001_create_cake_table.rs b/sea-orm-migration/tests/common/migration/m20220118_000001_create_cake_table.rs index 370697fbf..ea733f8c2 100644 --- a/sea-orm-migration/tests/common/migration/m20220118_000001_create_cake_table.rs +++ b/sea-orm-migration/tests/common/migration/m20220118_000001_create_cake_table.rs @@ -20,7 +20,18 @@ impl MigrationTrait for Migration { .col(ColumnDef::new(Cake::Name).string().not_null()) .to_owned(), ) - .await + .await?; + + manager + .create_index( + Index::create() + .name("cake_name_index") + .table(Cake::Table) + .col(Cake::Name) + .to_owned(), + ) + .await?; + Ok(()) } async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { diff --git a/sea-orm-migration/tests/main.rs b/sea-orm-migration/tests/main.rs index 3247bae25..673c51c48 100644 --- a/sea-orm-migration/tests/main.rs +++ b/sea-orm-migration/tests/main.rs @@ -182,6 +182,9 @@ where let migrations = Migrator::get_applied_migrations(db).await?; assert_eq!(migrations.len(), 6); + assert!(!manager.has_index("cake", "non_existent_index").await?); + assert!(manager.has_index("cake", "cake_name_index").await?); + let migration = migrations.get(0).unwrap(); assert_eq!(migration.name(), "m20220118_000001_create_cake_table"); assert_eq!(migration.status(), MigrationStatus::Applied);