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

fix: skip parsing partitioned Postgres tables #105

Merged
merged 4 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion src/postgres/query/constraints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use referential_constraints::*;
pub use table_constraints::*;

use super::{InformationSchema, SchemaQueryBuilder};
use crate::sqlx_types::postgres::PgRow;
use crate::{postgres::query::select_table_and_view, sqlx_types::postgres::PgRow};
use sea_query::{Alias, Condition, Expr, Iden, JoinType, Order, Query, SeaRc, SelectStatement};

#[derive(Debug, Default)]
Expand Down Expand Up @@ -162,6 +162,9 @@ impl SchemaQueryBuilder {
Expr::col((Schema::TableConstraints, Tcf::TableSchema)).eq(schema.to_string()),
)
.and_where(Expr::col((Schema::TableConstraints, Tcf::TableName)).eq(table.to_string()))
.and_where(
Expr::col((rcsq.clone(), Kcuf::TableName)).not_in_subquery(select_table_and_view()),
)
.order_by((Schema::TableConstraints, Tcf::ConstraintName), Order::Asc)
.order_by((Schema::KeyColumnUsage, Kcuf::OrdinalPosition), Order::Asc)
.order_by((rcsq.clone(), RefC::UniqueConstraintName), Order::Asc)
Expand Down
38 changes: 37 additions & 1 deletion src/postgres/query/schema.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use sea_query::{Condition, Expr, Iden, JoinType, Query, SelectStatement};

#[derive(Debug, Default)]
pub struct SchemaQueryBuilder;

#[derive(Debug, sea_query::Iden)]
#[derive(Debug, Iden)]
/// Ref: https://www.postgresql.org/docs/13/information-schema.html
pub enum InformationSchema {
#[iden = "information_schema"]
Expand All @@ -14,3 +16,37 @@ pub enum InformationSchema {
TableConstraints,
ConstraintColumnUsage,
}

pub(crate) fn select_table_and_view() -> SelectStatement {
Copy link
Member

@tyt2y3 tyt2y3 Apr 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this should be named select_XXX_table_and_view
where I highly suspect XXX = inherent?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

select_base_table_and_view 5329fb2

#[derive(Debug, Iden)]
pub enum PgClass {
tyt2y3 marked this conversation as resolved.
Show resolved Hide resolved
Table,
Relname,
Relkind,
Oid,
}

#[derive(Debug, Iden)]
pub enum PgInherits {
tyt2y3 marked this conversation as resolved.
Show resolved Hide resolved
Table,
Inhrelid,
}

Query::select()
.column((PgClass::Table, PgClass::Relname))
.from(PgInherits::Table)
.join(
JoinType::Join,
PgClass::Table,
Condition::all()
.add(
Expr::col((PgInherits::Table, PgInherits::Inhrelid))
.equals((PgClass::Table, PgClass::Oid)),
)
.add(
Expr::col((PgClass::Table, PgClass::Relkind))
.is_in(["r", "t", "v", "m", "f", "p"]),
Copy link
Member

@tyt2y3 tyt2y3 Apr 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment what these actually mean?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! d615448

),
)
.to_owned()
}
3 changes: 2 additions & 1 deletion src/postgres/query/table.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InformationSchema, SchemaQueryBuilder};
use super::{select_table_and_view, InformationSchema, SchemaQueryBuilder};
use crate::sqlx_types::postgres::PgRow;
use sea_query::{Expr, Iden, Query, SeaRc, SelectStatement};

Expand Down Expand Up @@ -46,6 +46,7 @@ impl SchemaQueryBuilder {
.from((InformationSchema::Schema, InformationSchema::Tables))
.and_where(Expr::col(TablesFields::TableSchema).eq(schema.to_string()))
.and_where(Expr::col(TablesFields::TableType).eq(TableType::BaseTable.to_string()))
.and_where(Expr::col(TablesFields::TableName).not_in_subquery(select_table_and_view()))
.take()
}
}
Expand Down