Skip to content

Commit df7d15a

Browse files
w3b6x9soedirgo
authored andcommitted
chore(triggers): clean up PostgresMetaTriggers and triggers sql
1 parent 6a7b0e5 commit df7d15a

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

src/lib/PostgresMetaTriggers.ts

+20-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import format, { ident, literal } from 'pg-format'
1+
import { ident, literal } from 'pg-format'
22
import { triggersSql } from './sql'
33
import { PostgresMetaResult, PostgresTrigger } from './types'
44

@@ -35,7 +35,7 @@ export default class PostgresMetaTriggers {
3535
table?: string
3636
}): Promise<PostgresMetaResult<PostgresTrigger>> {
3737
if (id) {
38-
const sql = `${enrichedTriggersSql} WHERE triggers.id = ${literal(id)};`
38+
const sql = `${enrichedTriggersSql} WHERE id = ${literal(id)};`
3939

4040
const { data, error } = await this.query(sql)
4141

@@ -53,9 +53,9 @@ export default class PostgresMetaTriggers {
5353
}
5454

5555
if (name && schema && table) {
56-
const sql = `${enrichedTriggersSql} WHERE triggers.name = ${literal(
57-
name
58-
)} AND triggers.schema = ${literal(schema)} AND triggers.table = ${literal(table)};`
56+
const sql = `${enrichedTriggersSql} WHERE name = ${literal(name)} AND schema = ${literal(
57+
schema
58+
)} AND triggers.table = ${literal(table)};`
5959

6060
const { data, error } = await this.query(sql)
6161

@@ -125,17 +125,12 @@ export default class PostgresMetaTriggers {
125125
}): Promise<PostgresMetaResult<PostgresTrigger>> {
126126
const qualifiedTableName = `${ident(schema)}.${ident(table)}`
127127
const qualifiedFunctionName = `${ident(function_schema)}.${ident(function_name)}`
128+
const triggerEvents = events.join(' OR ')
129+
const triggerOrientation = orientation ? `FOR EACH ${orientation}` : ''
130+
const triggerCondition = condition ? `WHEN (${condition})` : ''
131+
const functionArgs = `${function_args?.map(literal).join(',') ?? ''}`
128132

129-
const triggerOrientation = orientation ? `FOR EACH ${format.string(orientation)}` : ''
130-
const triggerCondition = condition ? `WHEN ( ${format.string(condition)} )` : ''
131-
const triggerEvents = Array.isArray(events) ? `${format.string(events.join(' OR '))}` : ''
132-
const functionArgs = Array.isArray(function_args)
133-
? `${function_args.map((arg) => literal(arg)).join(',')}`
134-
: ''
135-
136-
const sql = `CREATE TRIGGER ${ident(name)} ${format.string(
137-
activation
138-
)} ${triggerEvents} ON ${qualifiedTableName} ${triggerOrientation} ${triggerCondition} EXECUTE FUNCTION ${qualifiedFunctionName} ( ${functionArgs} );`
133+
const sql = `CREATE TRIGGER ${ident(name)} ${activation} ${triggerEvents} ON ${qualifiedTableName} ${triggerOrientation} ${triggerCondition} EXECUTE FUNCTION ${qualifiedFunctionName}(${functionArgs});`
139134

140135
const { error } = await this.query(sql)
141136

@@ -206,8 +201,9 @@ export default class PostgresMetaTriggers {
206201
}
207202

208203
const { name, schema, table } = triggerRecord!
209-
const qualifiedTableName = `${ident(schema)}.${ident(table)}`
210-
const sql = `DROP TRIGGER ${ident(name)} ON ${qualifiedTableName} ${cascade ? 'CASCADE' : ''};`
204+
const sql = `DROP TRIGGER ${ident(name)} ON ${ident(schema)}.${ident(table)} ${
205+
cascade ? 'CASCADE' : ''
206+
};`
211207

212208
{
213209
const { error } = await this.query(sql)
@@ -222,7 +218,10 @@ export default class PostgresMetaTriggers {
222218
}
223219

224220
const enrichedTriggersSql = `
225-
WITH triggers AS (${triggersSql})
226-
SELECT
227-
*
228-
FROM triggers`
221+
WITH triggers AS (
222+
${triggersSql}
223+
)
224+
SELECT
225+
*
226+
FROM triggers
227+
`

src/lib/sql/triggers.sql

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ GROUP BY
4242
is_t.event_object_schema,
4343
is_t.action_condition,
4444
is_t.action_orientation,
45-
is_t.action_statement,
4645
is_t.action_timing,
4746
pg_p.proname,
4847
pg_n.nspname

0 commit comments

Comments
 (0)