Skip to content

Commit

Permalink
Check if an index exists (#1828)
Browse files Browse the repository at this point in the history
* 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 <chris.2y3@outlook.com>
  • Loading branch information
IgnisDa and tyt2y3 committed Sep 14, 2023
1 parent f6b94c3 commit b029e87
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sea-orm-migration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
21 changes: 21 additions & 0 deletions sea-orm-migration/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,25 @@ impl<'c> SchemaManager<'c> {

res.try_get("", "has_column")
}

pub async fn has_index<T, I>(&self, table: T, index: I) -> Result<bool, DbErr>
where
T: AsRef<str>,
I: AsRef<str>,
{
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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
3 changes: 3 additions & 0 deletions sea-orm-migration/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b029e87

Please sign in to comment.