Skip to content

Commit e78e10a

Browse files
w3b6x9soedirgo
authored andcommitted
fix: update and delete function based on its identity args
1 parent 9afbc89 commit e78e10a

File tree

4 files changed

+19
-21
lines changed

4 files changed

+19
-21
lines changed

src/lib/PostgresMetaFunctions.ts

+16-21
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export default class PostgresMetaFunctions {
119119
}
120120

121121
const args = currentFunc!.argument_types.split(', ')
122+
const identityArgs = currentFunc!.identity_argument_types
122123

123124
const updateDefinitionSql =
124125
typeof definition === 'string'
@@ -133,40 +134,34 @@ export default class PostgresMetaFunctions {
133134
)
134135
: ''
135136

136-
const retrieveFunctionSql = this.generateRetrieveFunctionSql({
137-
schema: currentFunc!.schema,
138-
name: currentFunc!.name,
139-
args,
140-
})
141-
142137
const updateNameSql =
143138
name && name !== currentFunc!.name
144-
? `ALTER FUNCTION ${ident(currentFunc!.schema)}.${ident(currentFunc!.name)}(${
145-
currentFunc!.argument_types
146-
}) RENAME TO ${ident(name)};`
139+
? `ALTER FUNCTION ${ident(currentFunc!.schema)}.${ident(
140+
currentFunc!.name
141+
)}(${identityArgs}) RENAME TO ${ident(name)};`
147142
: ''
148143

149144
const updateSchemaSql =
150145
schema && schema !== currentFunc!.schema
151-
? `ALTER FUNCTION ${ident(currentFunc!.schema)}.${ident(name || currentFunc!.name)}(${
152-
currentFunc!.argument_types
153-
}) SET SCHEMA ${ident(schema)};`
146+
? `ALTER FUNCTION ${ident(currentFunc!.schema)}.${ident(
147+
name || currentFunc!.name
148+
)}(${identityArgs}) SET SCHEMA ${ident(schema)};`
154149
: ''
155150

156151
const sql = `
157152
DO LANGUAGE plpgsql $$
158-
DECLARE
159-
function record;
160153
BEGIN
161154
IF ${typeof definition === 'string' ? 'TRUE' : 'FALSE'} THEN
162155
${updateDefinitionSql}
163156
164-
${retrieveFunctionSql} INTO function;
165-
166-
IF function.id != ${id} THEN
167-
RAISE EXCEPTION 'Cannot find function "${currentFunc!.schema}"."${currentFunc!.name}"(${
168-
currentFunc!.argument_types
169-
})';
157+
IF (
158+
SELECT id
159+
FROM (${functionsSql}) AS f
160+
WHERE f.identity_argument_types = ${literal(identityArgs)}
161+
) != ${id} THEN
162+
RAISE EXCEPTION 'Cannot find function "${currentFunc!.schema}"."${
163+
currentFunc!.name
164+
}"(${identityArgs})';
170165
END IF;
171166
END IF;
172167
@@ -197,7 +192,7 @@ export default class PostgresMetaFunctions {
197192
return { data: null, error }
198193
}
199194
const sql = `DROP FUNCTION ${ident(func!.schema)}.${ident(func!.name)}
200-
(${func!.argument_types})
195+
(${func!.identity_argument_types})
201196
${cascade ? 'CASCADE' : 'RESTRICT'};`
202197
{
203198
const { error } = await this.query(sql)

src/lib/sql/functions.sql

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ SELECT
1212
ELSE pg_get_functiondef(p.oid)
1313
END AS complete_statement,
1414
pg_get_function_arguments(p.oid) AS argument_types,
15+
pg_get_function_identity_arguments(p.oid) AS identity_argument_types,
1516
t.typname AS return_type,
1617
CASE
1718
WHEN p.provolatile = 'i' THEN 'IMMUTABLE'

src/lib/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ const postgresFunctionSchema = Type.Object({
7878
definition: Type.String(),
7979
complete_statement: Type.String(),
8080
argument_types: Type.String(),
81+
identity_argument_types: Type.String(),
8182
return_type: Type.String(),
8283
behavior: Type.Union([
8384
Type.Literal('IMMUTABLE'),

test/integration/index.spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ describe('/functions', () => {
214214
assert.strictEqual(newFunc.name, 'test_func')
215215
assert.strictEqual(newFunc.schema, 'public')
216216
assert.strictEqual(newFunc.argument_types, 'a smallint, b smallint')
217+
assert.strictEqual(newFunc.identity_argument_types, 'a smallint, b smallint')
217218
assert.strictEqual(newFunc.language, 'sql')
218219
assert.strictEqual(newFunc.definition, 'select a + b')
219220
assert.strictEqual(

0 commit comments

Comments
 (0)