Skip to content

Commit 4e57995

Browse files
chore(schema_cache): add query for triggers (#398)
1 parent 2ada420 commit 4e57995

14 files changed

+378
-9
lines changed

.sqlx/query-df57cc22f7d63847abce1d0d15675ba8951faa1be2ea6b2bf6714b1aa9127a6f.json

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pgt_schema_cache/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pgt_diagnostics.workspace = true
2020
serde.workspace = true
2121
serde_json.workspace = true
2222
sqlx.workspace = true
23+
strum = { workspace = true }
2324
tokio.workspace = true
2425

2526
[dev-dependencies]

crates/pgt_schema_cache/src/columns.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl From<char> for ColumnClassKind {
3737
}
3838
}
3939

40-
#[derive(Debug, Clone, PartialEq, Eq)]
40+
#[derive(Debug, PartialEq, Eq)]
4141
pub struct Column {
4242
pub name: String,
4343

crates/pgt_schema_cache/src/functions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl From<Option<JsonValue>> for FunctionArgs {
5858
}
5959
}
6060

61-
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
61+
#[derive(Debug, Default, Serialize, Deserialize)]
6262
pub struct Function {
6363
/// The Id (`oid`).
6464
pub id: i64,

crates/pgt_schema_cache/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod policies;
88
mod schema_cache;
99
mod schemas;
1010
mod tables;
11+
mod triggers;
1112
mod types;
1213
mod versions;
1314

@@ -16,3 +17,4 @@ pub use functions::{Behavior, Function, FunctionArg, FunctionArgs};
1617
pub use schema_cache::SchemaCache;
1718
pub use schemas::Schema;
1819
pub use tables::{ReplicaIdentity, Table};
20+
pub use triggers::{Trigger, TriggerAffected, TriggerEvent};

crates/pgt_schema_cache/src/policies.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl From<PolicyQueried> for Policy {
5454
}
5555
}
5656

57-
#[derive(Debug, Clone, PartialEq, Eq)]
57+
#[derive(Debug, PartialEq, Eq)]
5858
pub struct Policy {
5959
name: String,
6060
table_name: String,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- we need to join tables from the pg_catalog since "TRUNCATE" triggers are
2+
-- not available in the information_schema.trigger table.
3+
select
4+
t.tgname as "name!",
5+
c.relname as "table_name!",
6+
p.proname as "proc_name!",
7+
n.nspname as "schema_name!",
8+
t.tgtype as "details_bitmask!"
9+
from
10+
pg_catalog.pg_trigger t
11+
left join pg_catalog.pg_proc p on t.tgfoid = p.oid
12+
left join pg_catalog.pg_class c on t.tgrelid = c.oid
13+
left join pg_catalog.pg_namespace n on c.relnamespace = n.oid
14+
where
15+
-- triggers enforcing constraints (e.g. unique fields) should not be included.
16+
t.tgisinternal = false and
17+
t.tgconstraint = 0;

crates/pgt_schema_cache/src/schema_cache.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use sqlx::postgres::PgPool;
22

3+
use crate::Trigger;
34
use crate::columns::Column;
45
use crate::functions::Function;
56
use crate::policies::Policy;
@@ -8,7 +9,7 @@ use crate::tables::Table;
89
use crate::types::PostgresType;
910
use crate::versions::Version;
1011

11-
#[derive(Debug, Clone, Default)]
12+
#[derive(Debug, Default)]
1213
pub struct SchemaCache {
1314
pub schemas: Vec<Schema>,
1415
pub tables: Vec<Table>,
@@ -17,18 +18,20 @@ pub struct SchemaCache {
1718
pub versions: Vec<Version>,
1819
pub columns: Vec<Column>,
1920
pub policies: Vec<Policy>,
21+
pub triggers: Vec<Trigger>,
2022
}
2123

2224
impl SchemaCache {
2325
pub async fn load(pool: &PgPool) -> Result<SchemaCache, sqlx::Error> {
24-
let (schemas, tables, functions, types, versions, columns, policies) = futures_util::try_join!(
26+
let (schemas, tables, functions, types, versions, columns, policies, triggers) = futures_util::try_join!(
2527
Schema::load(pool),
2628
Table::load(pool),
2729
Function::load(pool),
2830
PostgresType::load(pool),
2931
Version::load(pool),
3032
Column::load(pool),
3133
Policy::load(pool),
34+
Trigger::load(pool),
3235
)?;
3336

3437
Ok(SchemaCache {
@@ -39,6 +42,7 @@ impl SchemaCache {
3942
versions,
4043
columns,
4144
policies,
45+
triggers,
4246
})
4347
}
4448

crates/pgt_schema_cache/src/schemas.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use sqlx::PgPool;
22

33
use crate::schema_cache::SchemaCacheItem;
44

5-
#[derive(Debug, Clone, Default)]
5+
#[derive(Debug, Default)]
66
pub struct Schema {
77
pub id: i64,
88
pub name: String,

crates/pgt_schema_cache/src/tables.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl From<String> for ReplicaIdentity {
2323
}
2424
}
2525

26-
#[derive(Debug, Clone, Default, PartialEq, Eq)]
26+
#[derive(Debug, Default, PartialEq, Eq)]
2727
pub struct Table {
2828
pub id: i64,
2929
pub schema: String,

0 commit comments

Comments
 (0)