Skip to content

Commit e7a975b

Browse files
committed
refactor(/functions): a few more improvements & clean up after code review from @w3b6x9
1 parent 96806a0 commit e7a975b

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/lib/PostgresMetaFunctions.ts

+16-10
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export default class PostgresMetaFunctions {
8484
language?: string
8585
}): Promise<PostgresMetaResult<PostgresFunction>> {
8686
const sql = `
87-
CREATE OR REPLACE FUNCTION ${ident(schema)}.${ident(name)}
87+
CREATE FUNCTION ${ident(schema)}.${ident(name)}
8888
${params && params.length ? `(${params.join(',')})` : '()'}
8989
RETURNS ${rettype}
9090
AS ${literal(definition)}
@@ -102,9 +102,9 @@ export default class PostgresMetaFunctions {
102102
id: number,
103103
{
104104
name,
105-
schema = 'public',
105+
schema,
106106
}: {
107-
name: string
107+
name?: string
108108
schema?: string
109109
}
110110
): Promise<PostgresMetaResult<PostgresFunction>> {
@@ -113,15 +113,21 @@ export default class PostgresMetaFunctions {
113113
return { data: null, error: retrieveError }
114114
}
115115

116-
let alter = `ALTER FUNCTION ${ident(old!.name)}`
117-
const nameSql =
118-
name === undefined || name == old!.name ? '' : `${alter} RENAME TO ${ident(name)};`
116+
const updateNameSql =
117+
name && name !== old!.name
118+
? `ALTER FUNCTION ${ident(old!.schema)}.${ident(old!.name)}(${
119+
old!.argument_types
120+
}) RENAME TO ${ident(name)};`
121+
: ''
119122

120-
alter = `ALTER FUNCTION ${ident(name)}`
121-
const schemaSql =
122-
schema === undefined || schema == old!.schema ? '' : `${alter} SET SCHEMA ${ident(schema)};`
123+
const updateSchemaSql =
124+
schema && schema !== old!.schema
125+
? `ALTER FUNCTION ${ident(old!.schema)}.${ident(name || old!.name)}(${
126+
old!.argument_types
127+
}) SET SCHEMA ${ident(schema)};`
128+
: ''
123129

124-
const sql = `BEGIN;${nameSql} ${schemaSql} COMMIT;`
130+
const sql = `BEGIN;${updateNameSql} ${updateSchemaSql} COMMIT;`
125131

126132
const { error } = await this.query(sql)
127133
if (error) {

0 commit comments

Comments
 (0)