From 94f245dcaf0ea587d0459dcfc7eea3bc00d80a43 Mon Sep 17 00:00:00 2001 From: Diptesh Choudhuri Date: Sat, 26 Aug 2023 16:17:20 +0530 Subject: [PATCH 1/5] feat: add method to check for index --- sea-orm-migration/src/manager.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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") + } } From f44c013a4985b53ab5438d7305f84d941e2c40c8 Mon Sep 17 00:00:00 2001 From: Diptesh Choudhuri Date: Sat, 26 Aug 2023 16:34:52 +0530 Subject: [PATCH 2/5] tests(sea-orm-migrations): add index to cake name --- .../migration/m20220118_000001_create_cake_table.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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..dd900ad84 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,17 @@ 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> { From 187bd81ce72dffd5c52f5caf4d36f07da4032131 Mon Sep 17 00:00:00 2001 From: Diptesh Choudhuri Date: Sat, 26 Aug 2023 16:35:20 +0530 Subject: [PATCH 3/5] tests(sea-orm-migrations): check if `has_index` works --- sea-orm-migration/tests/main.rs | 3 +++ 1 file changed, 3 insertions(+) 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); From c8168b7e794912d30ceb79d59a6d8adbf3be168b Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Thu, 14 Sep 2023 15:40:24 +0800 Subject: [PATCH 4/5] Update sea-orm-migration/tests/common/migration/m20220118_000001_create_cake_table.rs --- .../tests/common/migration/m20220118_000001_create_cake_table.rs | 1 + 1 file changed, 1 insertion(+) 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 dd900ad84..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 @@ -21,6 +21,7 @@ impl MigrationTrait for Migration { .to_owned(), ) .await?; + manager .create_index( Index::create() From 55b08c873a4a35fb8e871080d046d126bdd28890 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Thu, 14 Sep 2023 15:41:41 +0800 Subject: [PATCH 5/5] Update Cargo.toml --- sea-orm-migration/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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"] }