Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if an index exists #1828

Merged
merged 5 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
tyt2y3 marked this conversation as resolved.
Show resolved Hide resolved
.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