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

Query to check for index existence #115

Merged
merged 7 commits into from
Sep 6, 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
27 changes: 25 additions & 2 deletions src/mysql/probe.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use sea_query::{Condition, Expr, Query, SelectStatement, SimpleExpr};
use sea_query::{Condition, Expr, Iden, Query, SelectStatement, SimpleExpr};

use super::query::{InformationSchema as Schema, TablesFields};
use super::MySql;
use crate::probe::SchemaProbe;
use crate::mysql::query::InformationSchema;
use crate::probe::{DatabaseSchema, Has, SchemaProbe};

impl SchemaProbe for MySql {
fn get_current_schema() -> SimpleExpr {
Expand All @@ -21,4 +22,26 @@ impl SchemaProbe for MySql {
)
.take()
}

fn has_index<T, C>(table: T, index: C) -> SelectStatement
where
T: AsRef<str>,
C: AsRef<str>,
{
Query::select()
.expr_as(Expr::cust("COUNT(*) > 0"), Has::Index)
.from((DatabaseSchema::Info, InformationSchema::Statistics))
.cond_where(
Condition::all()
.add(Expr::col(DatabaseSchema::TableSchema).eq(Self::get_current_schema()))
.add(Expr::col(DatabaseSchema::TableName).eq(table.as_ref()))
.add(Expr::col(InternalDatabaseSchema::IndexName).eq(index.as_ref())),
)
.take()
}
}

#[derive(Debug, Iden)]
pub enum InternalDatabaseSchema {
IndexName,
}
31 changes: 29 additions & 2 deletions src/postgres/probe.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use sea_query::{Condition, Expr, Query, SelectStatement, SimpleExpr};
use sea_query::{Alias, Condition, Expr, Iden, Query, SelectStatement, SimpleExpr};

use super::query::{InformationSchema as Schema, TablesFields};
use super::Postgres;
use crate::probe::SchemaProbe;
use crate::probe::{Has, SchemaProbe};

impl SchemaProbe for Postgres {
fn get_current_schema() -> SimpleExpr {
Expand All @@ -23,4 +23,31 @@ impl SchemaProbe for Postgres {
)
.take()
}

fn has_index<T, C>(table: T, index: C) -> SelectStatement
where
T: AsRef<str>,
C: AsRef<str>,
{
Query::select()
.expr_as(Expr::cust("COUNT(*) > 0"), Has::Index)
.from(Alias::new("pg_indexes"))
.cond_where(
Condition::all()
.add(Expr::col(DatabaseSchema::Schema).eq(Self::get_current_schema()))
.add(Expr::col(DatabaseSchema::Table).eq(table.as_ref()))
.add(Expr::col(DatabaseSchema::Index).eq(index.as_ref())),
)
.take()
}
}

#[derive(Debug, Iden)]
enum DatabaseSchema {
#[iden = "tablename"]
Table,
#[iden = "schemaname"]
Schema,
#[iden = "indexname"]
Index,
}
21 changes: 14 additions & 7 deletions src/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub trait SchemaProbe {
T: AsRef<str>,
{
let mut subquery = Self::query_tables();
subquery.cond_where(Expr::col(Schema::TableName).eq(table.as_ref()));
subquery.cond_where(Expr::col(DatabaseSchema::TableName).eq(table.as_ref()));
Query::select()
.expr_as(Expr::cust("COUNT(*) > 0"), Has::Table)
.from_subquery(subquery, Subquery)
Expand All @@ -24,30 +24,37 @@ pub trait SchemaProbe {
{
Query::select()
.expr_as(Expr::cust("COUNT(*) > 0"), Has::Column)
.from((Schema::Info, Schema::Columns))
.from((DatabaseSchema::Info, DatabaseSchema::Columns))
.cond_where(
Condition::all()
.add(
Expr::expr(Self::get_current_schema())
.equals((Schema::Columns, Schema::TableSchema)),
.equals((DatabaseSchema::Columns, DatabaseSchema::TableSchema)),
)
.add(Expr::col(Schema::TableName).eq(table.as_ref()))
.add(Expr::col(Schema::ColumnName).eq(column.as_ref())),
.add(Expr::col(DatabaseSchema::TableName).eq(table.as_ref()))
.add(Expr::col(DatabaseSchema::ColumnName).eq(column.as_ref())),
)
.take()
}

fn has_index<T, C>(table: T, index: C) -> SelectStatement
where
T: AsRef<str>,
C: AsRef<str>;
}

#[derive(Debug, Iden)]
enum Has {
pub enum Has {
#[iden = "has_table"]
Table,
#[iden = "has_column"]
Column,
#[iden = "has_index"]
Index,
}

#[derive(Debug, Iden)]
enum Schema {
pub enum DatabaseSchema {
#[iden = "information_schema"]
Info,
Columns,
Expand Down
20 changes: 19 additions & 1 deletion src/sqlite/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use sea_query::{Condition, Expr, Iden, IntoTableRef, Query, SelectStatement, Sim

use super::query::SqliteMaster;
use super::Sqlite;
use crate::probe::SchemaProbe;
use crate::probe::{Has, SchemaProbe};

impl SchemaProbe for Sqlite {
fn get_current_schema() -> SimpleExpr {
Expand Down Expand Up @@ -34,11 +34,29 @@ impl SchemaProbe for Sqlite {
.and_where(Expr::col(Schema::Name).eq(column.as_ref()))
.take()
}

fn has_index<T, C>(table: T, index: C) -> SelectStatement
where
T: AsRef<str>,
C: AsRef<str>,
{
Query::select()
.expr_as(Expr::cust("COUNT(*) > 0"), Has::Index)
.from(SqliteMaster.into_table_ref())
.cond_where(
Condition::all()
.add(Expr::col(Schema::Type).eq("index"))
.add(Expr::col(Schema::TblName).eq(table.as_ref()))
.add(Expr::col(Schema::Name).eq(index.as_ref())),
)
.take()
}
}

#[derive(Debug, Iden)]
enum Schema {
Name,
Type,
TableName,
TblName,
}