@@ -32,9 +32,11 @@ export default class PostgresMetaFunctions {
3232 async retrieve ( {
3333 id,
3434 name,
35+ schema = 'public' ,
3536 } : {
3637 id ?: number
3738 name ?: string
39+ schema ?: string
3840 } ) : Promise < PostgresMetaResult < PostgresFunction > > {
3941 if ( id ) {
4042 const sql = `${ functionsSql } WHERE p.oid = ${ literal ( id ) } ;`
@@ -47,14 +49,16 @@ export default class PostgresMetaFunctions {
4749 return { data : data [ 0 ] , error }
4850 }
4951 } else if ( name ) {
50- const sql = `${ functionsSql } WHERE p.proname = ${ literal ( name ) } ;`
52+ const sql = `${ functionsSql } WHERE p.proname = ${ literal ( name ) } AND n.nspname = ${ literal (
53+ schema
54+ ) } ;`
5155 const { data, error } = await this . query ( sql )
5256 if ( error ) {
5357 return { data, error }
5458 } else if ( data . length === 0 ) {
5559 return {
5660 data : null ,
57- error : { message : `Cannot find a function named ${ name } ` } ,
61+ error : { message : `Cannot find a function named ${ name } in schema ${ schema } ` } ,
5862 }
5963 } else {
6064 return { data : data [ 0 ] , error }
@@ -80,10 +84,10 @@ export default class PostgresMetaFunctions {
8084 language ?: string
8185 } ) : Promise < PostgresMetaResult < PostgresFunction > > {
8286 const sql = `
83- CREATE FUNCTION ${ ident ( schema ) } .${ ident ( name ) }
87+ CREATE OR REPLACE FUNCTION ${ ident ( schema ) } .${ ident ( name ) }
8488 ${ params && params . length ? `(${ params . join ( ',' ) } )` : '()' }
85- RETURNS ${ rettype || 'void' }
86- AS ' ${ definition } '
89+ RETURNS ${ rettype }
90+ AS ${ literal ( definition ) }
8791 LANGUAGE ${ language }
8892 RETURNS NULL ON NULL INPUT;
8993 `
@@ -99,40 +103,25 @@ export default class PostgresMetaFunctions {
99103 {
100104 name,
101105 schema = 'public' ,
102- params,
103- extension,
104106 } : {
105107 name : string
106108 schema ?: string
107- params ?: string [ ] //optional params for overloaded functions
108- extension ?: string //e.g. sqrt DEPENDS ON EXTENSION mathlib
109109 }
110110 ) : Promise < PostgresMetaResult < PostgresFunction > > {
111111 const { data : old , error : retrieveError } = await this . retrieve ( { id } )
112112 if ( retrieveError ) {
113113 return { data : null , error : retrieveError }
114114 }
115115
116- let alter = `ALTER FUNCTION ${ ident ( old ! . schema ) } .${ ident ( old ! . name ) } ${
117- params && params . length ? `(${ params . join ( ',' ) } )` : ''
118- } `
119-
120- let schemaSql = ''
121- if ( schema !== undefined && schema !== old ! . schema ) {
122- schemaSql = `${ alter } SET SCHEMA ${ ident ( schema ) } ;`
123- alter = `ALTER FUNCTION ${ ident ( schema ) } .${ ident ( old ! . name ) } ${
124- params && params . length ? `(${ params . join ( ',' ) } )` : ''
125- } `
126- }
127-
116+ let alter = `ALTER FUNCTION ${ ident ( old ! . name ) } `
128117 const nameSql =
129118 name === undefined || name == old ! . name ? '' : `${ alter } RENAME TO ${ ident ( name ) } ;`
130119
131- //Note: leaving out search_path and owner - should these be alterable from this api?
132- //Note: also leaving out extensions - to enable extensions, they would have to already be
133- //installed. Current Postgres docker image has no extensions installed
120+ alter = `ALTER FUNCTION ${ ident ( name ) } `
121+ const schemaSql =
122+ schema === undefined || schema == old ! . schema ? '' : ` ${ alter } SET SCHEMA ${ ident ( schema ) } ;`
134123
135- const sql = `BEGIN;${ schemaSql } ${ nameSql } COMMIT;`
124+ const sql = `BEGIN;${ nameSql } ${ schemaSql } COMMIT;`
136125
137126 const { error } = await this . query ( sql )
138127 if ( error ) {
@@ -149,9 +138,9 @@ export default class PostgresMetaFunctions {
149138 if ( error ) {
150139 return { data : null , error }
151140 }
152- const sql = `DROP FUNCTION ${ ident ( func ! . schema ) } .${ ident ( func ! . name ) } ${
153- cascade ? 'CASCADE' : 'RESTRICT'
154- } ;`
141+ const sql = `DROP FUNCTION ${ ident ( func ! . schema ) } .${ ident ( func ! . name ) }
142+ ${ func ! . argument_types ? `( ${ func ! . argument_types } )` : '()' }
143+ ${ cascade ? 'CASCADE' : 'RESTRICT' } ;`
155144 {
156145 const { error } = await this . query ( sql )
157146 if ( error ) {
0 commit comments