File tree Expand file tree Collapse file tree 3 files changed +74
-0
lines changed Expand file tree Collapse file tree 3 files changed +74
-0
lines changed Original file line number Diff line number Diff line change @@ -94,6 +94,45 @@ export default class PostgresMetaFunctions {
9494 return await this . retrieve ( { name, schema } )
9595 }
9696
97+ async update (
98+ id : number ,
99+ {
100+ name,
101+ schema,
102+ params,
103+ extension,
104+ } : {
105+ name : string
106+ schema ?: string
107+ params ?: string [ ] //optional params for overloaded functions
108+ extension ?: string //e.g. sqrt DEPENDS ON EXTENSION mathlib
109+ }
110+ ) : Promise < PostgresMetaResult < PostgresFunction > > {
111+ const { data : old , error : retrieveError } = await this . retrieve ( { id } )
112+ if ( retrieveError ) {
113+ return { data : null , error : retrieveError }
114+ }
115+
116+ const alter = `ALTER FUNCTION ${ ident ( old ! . schema ) } .${ ident ( old ! . name ) } ${
117+ params && params . length ? `(${ params . join ( ',' ) } )` : ''
118+ } `
119+
120+ const schemaSql =
121+ schema === undefined || name == old ! . schema ? '' : `${ alter } SET SCHEMA ${ ident ( schema ) } ;`
122+ const extSql = extension === undefined ? '' : `${ alter } DEPENDS ON EXTENSION ${ extension } ;`
123+ const nameSql =
124+ name === undefined || name == old ! . name ? '' : `${ alter } RENAME TO ${ ident ( name ) } ;`
125+ //Note: leaving out search_path and owner - should these be alterable from this api?
126+
127+ const sql = `BEGIN; ${ schemaSql } ${ extSql } ${ nameSql } COMMIT;`
128+
129+ const { error } = await this . query ( sql )
130+ if ( error ) {
131+ return { data : null , error }
132+ }
133+ return await this . retrieve ( { id } )
134+ }
135+
97136 async remove (
98137 id : number ,
99138 { cascade = false } = { }
Original file line number Diff line number Diff line change @@ -61,6 +61,28 @@ export default async (fastify: FastifyInstance) => {
6161 return data
6262 } )
6363
64+ fastify . patch < {
65+ Headers : { pg : string }
66+ Params : {
67+ id : string
68+ }
69+ Body : any
70+ } > ( '/:id(\\d+)' , async ( request , reply ) => {
71+ const connectionString = request . headers . pg
72+ const id = Number ( request . params . id )
73+
74+ const pgMeta = new PostgresMeta ( { connectionString, max : 1 } )
75+ const { data, error } = await pgMeta . functions . update ( id , request . body )
76+ await pgMeta . end ( )
77+ if ( error ) {
78+ request . log . error ( JSON . stringify ( { error, req : request . body } ) )
79+ reply . code ( 400 )
80+ if ( error . message . startsWith ( 'Cannot find' ) ) reply . code ( 404 )
81+ return { error : error . message }
82+ }
83+ return data
84+ } )
85+
6486 fastify . delete < {
6587 Headers : { pg : string }
6688 Params : {
Original file line number Diff line number Diff line change @@ -206,6 +206,19 @@ describe('/functions', () => {
206206 assert . equal ( newFunc . return_type , 'int4' )
207207 func . id = newFunc . id
208208 } )
209+ it ( 'PATCH' , async ( ) => {
210+ const updates = {
211+ name : 'test_func_renamed' ,
212+ params : [ 'integer' , 'integer' ] ,
213+ // schema: 'test_schema' // TODO: test patching function schema
214+ // extension: 'mathlib', // TODO: test patching function extension
215+ }
216+
217+ let { data : updated } = await axios . patch ( `${ URL } /functions/${ func . id } ` , updates )
218+ assert . equal ( updated . id , func . id )
219+ assert . equal ( updated . name , 'test_func_renamed' )
220+ //assert.equal(updated.schema, 'test_schema')
221+ } )
209222 it ( 'DELETE' , async ( ) => {
210223 await axios . delete ( `${ URL } /functions/${ func . id } ` )
211224 const { data : functions } = await axios . get ( `${ URL } /functions` )
You can’t perform that action at this time.
0 commit comments