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 <chris.2y3@outlook.com>
This commit is contained in:
parent
f6b94c3a7a
commit
b029e87fd9
@ -25,7 +25,7 @@ clap = { version = "4.3", features = ["env", "derive"], optional = true }
|
|||||||
dotenvy = { version = "0.15", default-features = false, 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 = { 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-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 = { version = "0.1", default-features = false, features = ["log"] }
|
||||||
tracing-subscriber = { version = "0.3.17", default-features = false, features = ["env-filter", "fmt"] }
|
tracing-subscriber = { version = "0.3.17", default-features = false, features = ["env-filter", "fmt"] }
|
||||||
futures = { version = "0.3", default-features = false, features = ["std"] }
|
futures = { version = "0.3", default-features = false, features = ["std"] }
|
||||||
|
@ -136,4 +136,25 @@ impl<'c> SchemaManager<'c> {
|
|||||||
|
|
||||||
res.try_get("", "has_column")
|
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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,18 @@ impl MigrationTrait for Migration {
|
|||||||
.col(ColumnDef::new(Cake::Name).string().not_null())
|
.col(ColumnDef::new(Cake::Name).string().not_null())
|
||||||
.to_owned(),
|
.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> {
|
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||||
|
@ -182,6 +182,9 @@ where
|
|||||||
let migrations = Migrator::get_applied_migrations(db).await?;
|
let migrations = Migrator::get_applied_migrations(db).await?;
|
||||||
assert_eq!(migrations.len(), 6);
|
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();
|
let migration = migrations.get(0).unwrap();
|
||||||
assert_eq!(migration.name(), "m20220118_000001_create_cake_table");
|
assert_eq!(migration.name(), "m20220118_000001_create_cake_table");
|
||||||
assert_eq!(migration.status(), MigrationStatus::Applied);
|
assert_eq!(migration.status(), MigrationStatus::Applied);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user