Skip to content

Commit bcbd59c

Browse files
authored
Merge pull request #136 from supabase/fix/func-arg-types
fix: create functions with alias data types
2 parents da72623 + 191a085 commit bcbd59c

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

src/lib/PostgresMetaFunctions.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,39 @@ export default class PostgresMetaFunctions {
5353
return { data: data[0], error }
5454
}
5555
} else if (name && schema && args) {
56-
const argTypes = args.join(', ')
57-
const sql = `${enrichedFunctionsSql} WHERE schema = ${literal(schema)} AND name = ${literal(
58-
name
59-
)} AND argument_types = ${literal(argTypes)};`
56+
const sql = `${enrichedFunctionsSql} JOIN pg_proc AS p ON id = p.oid WHERE schema = ${literal(
57+
schema
58+
)} AND name = ${literal(name)} AND p.proargtypes::text = ${
59+
args.length
60+
? `(
61+
SELECT STRING_AGG(type_oid::text, ' ') FROM (
62+
SELECT (
63+
split_args.arr[
64+
array_length(
65+
split_args.arr,
66+
1
67+
)
68+
]::regtype::oid
69+
) AS type_oid FROM (
70+
SELECT STRING_TO_ARRAY(
71+
UNNEST(
72+
ARRAY[${args.map(literal)}]
73+
),
74+
' '
75+
) AS arr
76+
) AS split_args
77+
) args
78+
);`
79+
: literal('')
80+
}`
6081
const { data, error } = await this.query(sql)
6182
if (error) {
6283
return { data, error }
6384
} else if (data.length === 0) {
6485
return {
6586
data: null,
6687
error: {
67-
message: `Cannot find function "${schema}"."${name}"(${argTypes})`,
88+
message: `Cannot find function "${schema}"."${name}"(${args.join(', ')})`,
6889
},
6990
}
7091
} else {
@@ -84,7 +105,7 @@ export default class PostgresMetaFunctions {
84105
language = 'sql',
85106
behavior = 'VOLATILE',
86107
security_definer = false,
87-
config_params,
108+
config_params = {},
88109
}: {
89110
name: string
90111
schema?: string
@@ -94,7 +115,7 @@ export default class PostgresMetaFunctions {
94115
language?: string
95116
behavior?: 'IMMUTABLE' | 'STABLE' | 'VOLATILE'
96117
security_definer?: boolean
97-
config_params: { [key: string]: string }
118+
config_params?: { [key: string]: string }
98119
}): Promise<PostgresMetaResult<PostgresFunction>> {
99120
const sql = `
100121
CREATE FUNCTION ${ident(schema)}.${ident(name)}(${args.join(', ')})
@@ -178,10 +199,10 @@ export default class PostgresMetaFunctions {
178199
}
179200

180201
const enrichedFunctionsSql = `
181-
WITH functions AS (
202+
WITH f AS (
182203
${functionsSql}
183204
)
184205
SELECT
185-
*
186-
FROM functions
206+
f.*
207+
FROM f
187208
`

test/integration/index.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ describe('/functions', () => {
161161
id: null,
162162
name: 'test_func',
163163
schema: 'public',
164-
args: ['integer', 'integer'],
165-
definition: 'select $1 + $2',
164+
args: ['a int2', 'b int2'],
165+
definition: 'select a + b',
166166
return_type: 'integer',
167167
language: 'sql',
168168
behavior: 'STABLE',
@@ -213,6 +213,7 @@ describe('/functions', () => {
213213
const { data: newFunc } = await axios.post(`${URL}/functions`, func)
214214
assert.strictEqual(newFunc.name, 'test_func')
215215
assert.strictEqual(newFunc.schema, 'public')
216+
assert.strictEqual(newFunc.argument_types, 'a smallint, b smallint')
216217
assert.strictEqual(newFunc.language, 'sql')
217218
assert.strictEqual(newFunc.return_type, 'int4')
218219
assert.strictEqual(newFunc.behavior, 'STABLE')

0 commit comments

Comments
 (0)