Skip to content

chore(schema_cache): add query for triggers #398

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

Merged
merged 4 commits into from
May 19, 2025
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/pgt_schema_cache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pgt_diagnostics.workspace = true
serde.workspace = true
serde_json.workspace = true
sqlx.workspace = true
strum = { workspace = true }
tokio.workspace = true

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/pgt_schema_cache/src/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl From<char> for ColumnClassKind {
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq)]
pub struct Column {
pub name: String,

Expand Down
2 changes: 1 addition & 1 deletion crates/pgt_schema_cache/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl From<Option<JsonValue>> for FunctionArgs {
}
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Function {
/// The Id (`oid`).
pub id: i64,
Expand Down
2 changes: 2 additions & 0 deletions crates/pgt_schema_cache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod policies;
mod schema_cache;
mod schemas;
mod tables;
mod triggers;
mod types;
mod versions;

Expand All @@ -16,3 +17,4 @@ pub use functions::{Behavior, Function, FunctionArg, FunctionArgs};
pub use schema_cache::SchemaCache;
pub use schemas::Schema;
pub use tables::{ReplicaIdentity, Table};
pub use triggers::{Trigger, TriggerAffected, TriggerEvent};
2 changes: 1 addition & 1 deletion crates/pgt_schema_cache/src/policies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl From<PolicyQueried> for Policy {
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq)]
pub struct Policy {
name: String,
table_name: String,
Expand Down
17 changes: 17 additions & 0 deletions crates/pgt_schema_cache/src/queries/triggers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- we need to join tables from the pg_catalog since "TRUNCATE" triggers are
-- not available in the information_schema.trigger table.
select
t.tgname as "name!",
c.relname as "table_name!",
p.proname as "proc_name!",
n.nspname as "schema_name!",
t.tgtype as "details_bitmask!"
from
pg_catalog.pg_trigger t
left join pg_catalog.pg_proc p on t.tgfoid = p.oid
left join pg_catalog.pg_class c on t.tgrelid = c.oid
left join pg_catalog.pg_namespace n on c.relnamespace = n.oid
where
-- triggers enforcing constraints (e.g. unique fields) should not be included.
t.tgisinternal = false and
t.tgconstraint = 0;
8 changes: 6 additions & 2 deletions crates/pgt_schema_cache/src/schema_cache.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use sqlx::postgres::PgPool;

use crate::Trigger;
use crate::columns::Column;
use crate::functions::Function;
use crate::policies::Policy;
Expand All @@ -8,7 +9,7 @@ use crate::tables::Table;
use crate::types::PostgresType;
use crate::versions::Version;

#[derive(Debug, Clone, Default)]
#[derive(Debug, Default)]
pub struct SchemaCache {
pub schemas: Vec<Schema>,
pub tables: Vec<Table>,
Expand All @@ -17,18 +18,20 @@ pub struct SchemaCache {
pub versions: Vec<Version>,
pub columns: Vec<Column>,
pub policies: Vec<Policy>,
pub triggers: Vec<Trigger>,
}

impl SchemaCache {
pub async fn load(pool: &PgPool) -> Result<SchemaCache, sqlx::Error> {
let (schemas, tables, functions, types, versions, columns, policies) = futures_util::try_join!(
let (schemas, tables, functions, types, versions, columns, policies, triggers) = futures_util::try_join!(
Schema::load(pool),
Table::load(pool),
Function::load(pool),
PostgresType::load(pool),
Version::load(pool),
Column::load(pool),
Policy::load(pool),
Trigger::load(pool),
)?;

Ok(SchemaCache {
Expand All @@ -39,6 +42,7 @@ impl SchemaCache {
versions,
columns,
policies,
triggers,
})
}

Expand Down
2 changes: 1 addition & 1 deletion crates/pgt_schema_cache/src/schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use sqlx::PgPool;

use crate::schema_cache::SchemaCacheItem;

#[derive(Debug, Clone, Default)]
#[derive(Debug, Default)]
pub struct Schema {
pub id: i64,
pub name: String,
Expand Down
2 changes: 1 addition & 1 deletion crates/pgt_schema_cache/src/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl From<String> for ReplicaIdentity {
}
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[derive(Debug, Default, PartialEq, Eq)]
pub struct Table {
pub id: i64,
pub schema: String,
Expand Down
Loading