8000 feat(/functions): implements PATCH /functions for function schema and… · DavraYoung/postgres-meta@2caf733 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2caf733

Browse files
committed
feat(/functions): implements PATCH /functions for function schema and name
1 parent 13799a8 commit 2caf733

File tree

2 files changed

+44
-24
lines changed

2 files changed

+44
-24
lines changed

src/lib/PostgresMetaFunctions.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export default class PostgresMetaFunctions {
9898
id: number,
9999
{
100100
name,
101-
schema,
101+
schema = 'public',
102102
params,
103103
extension,
104104
}: {
@@ -113,18 +113,26 @@ export default class PostgresMetaFunctions {
113113
return { data: null, error: retrieveError }
114114
}
115115

116-
const alter = `ALTER FUNCTION ${ident(old!.schema)}.${ident(old!.name)}${
116+
let alter = `ALTER FUNCTION ${ident(old!.schema)}.${ident(old!.name)}${
117117
params && params.length ? `(${params.join(',')})` : ''
118118
}`
119119

120-
const schemaSql =
121-
schema === undefined || name == old!.schema ? '' : `${alter} SET SCHEMA ${ident(schema)};`
122-
const extSql = extension === undefined ? '' : `${alter} DEPENDS ON EXTENSION ${extension};`
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+
123128
const nameSql =
124129
name === undefined || name == old!.name ? '' : `${alter} RENAME TO ${ident(name)};`
130+
125131
//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
126134

127-
const sql = `BEGIN; ${schemaSql} ${extSql} ${nameSql} COMMIT;`
135+
const sql = `BEGIN;${schemaSql} ${nameSql} COMMIT;`
128136

129137
const { error } = await this.query(sql)
130138
if (error) {

test/integration/index.spec.js

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ describe('/functions', () => {
170170
await axios.post(`${URL}/query`, {
171171
query: `DROP FUNCTION IF EXISTS "${func.name}";`,
172172
})
173+
await axios.post(`${URL}/query`, {
174+
query: `CREATE SCHEMA IF NOT EXISTS test_schema;`,
175+
})
173176
})
174177
it('GET', async () => {
175178
const res = await axios.get(`${URL}/functions`)
@@ -210,14 +213,14 @@ describe('/functions', () => {
210213
const updates = {
211214
name: 'test_func_renamed',
212215
params: ['integer', 'integer'],
213-
// schema: 'test_schema' // TODO: test patching function schema
216+
schema: 'test_schema',
214217
// extension: 'mathlib', // TODO: test patching function extension
215218
}
216219

217220
let { data: updated } = await axios.patch(`${URL}/functions/${func.id}`, updates)
218221
assert.equal(updated.id, func.id)
219222
assert.equal(updated.name, 'test_func_renamed')
220-
//assert.equal(updated.schema, 'test_schema')
223+
assert.equal(updated.schema, 'test_schema')
221224
})
222225
it('DELETE', async () => {
223226
await axios.delete(`${URL}/functions/${func.id}`)
@@ -309,7 +312,9 @@ describe('/tables', async () => {
309312
assert.equal(true, !!included)
310313
})
311314
it('GET enum /columns with quoted name', async () => {
312-
await axios.post(`${URL}/query`, { query: 'CREATE TYPE "T" AS ENUM (\'v\'); CREATE TABLE t ( c "T" );' })
315+
await axios.post(`${URL}/query`, {
316+
query: 'CREATE TYPE "T" AS ENUM (\'v\'); CREATE TABLE t ( c "T" );',
317+
})
313318
const { data: columns } = await axios.get(`${URL}/columns`)
314319
const column = columns.find((x) => x.table == 't')
315320
await axios.post(`${URL}/query`, { query: 'DROP TABLE t; DROP TYPE "T";' })
@@ -875,18 +880,21 @@ describe('/publications FOR ALL TABLES', () => {
875880
describe('/triggers', () => {
876881
const renamedTriggerName = 'test_trigger_renamed'
877882
const trigger = {
878-
name: 'test_trigger',
879-
schema: 'public',
880-
table: 'users_audit',
881-
function_schema: 'public',
882-
function_name: 'audit_action',
883-
function_args: ['test1', 'test2'],
884-
activation: 'AFTER',
885-
events: ['UPDATE'],
886-
orientation: 'ROW',
887-
condition: '(old.* IS DISTINCT FROM new.*)',
883+
name: 'test_trigger',
884+
schema: 'public',
885+
table: 'users_audit',
886+
function_schema: 'public',
887+
function_name: 'audit_action',
888+
function_args: ['test1', 'test2'],
889+
activation: 'AFTER',
890+
events: ['UPDATE'],
891+
orientation: 'ROW',
892+
condition: '(old.* IS DISTINCT FROM new.*)',
893+
}
894+
const multiEventTrigger = {
895+
...trigger,
896+
...{ name: 'test_multi_event_trigger', events: ['insert', 'update', 'delete'], condition: '' },
888897
}
889-
const multiEventTrigger = { ...trigger, ...{ name: 'test_multi_event_trigger', events: ['insert', 'update', 'delete'], condition: '' } }
890898

891899
before(async () => {
892900
await axios.post(`${URL}/query`, {
@@ -946,10 +954,14 @@ describe('/triggers', () => {
946954

947955
const sortedTriggerData = triggerData.sort((a, b) => a.name.length - b.name.length)
948956

949-
const { data: singleEventTriggerRecord } = await axios.get(`${URL}/triggers/${sortedTriggerData[0].id}`)
957+
const { data: singleEventTriggerRecord } = await axios.get(
958+
`${URL}/triggers/${sortedTriggerData[0].id}`
959+
)
950960
assert.strictEqual(singleEventTriggerRecord.name, 'test_trigger')
951961

952-
const { data: multiEventTriggerRecord } = await axios.get(`${URL}/triggers/${sortedTriggerData[1].id}`)
962+
const { data: multiEventTriggerRecord } = await axios.get(
963+
`${URL}/triggers/${sortedTriggerData[1].id}`
964+
)
953965
assert.strictEqual(multiEventTriggerRecord.name, 'test_multi_event_trigger')
954966
})
955967

@@ -962,14 +974,14 @@ describe('/triggers', () => {
962974

963975
const { data: updatedTriggerRecord } = await axios.patch(`${URL}/triggers/${id}`, {
964976
name: 'test_trigger_renamed',
965-
enabled_mode: 'DISABLED'
977+
enabled_mode: 'DISABLED',
966978
})
967979

968980
assert.strictEqual(updatedTriggerRecord.name, 'test_trigger_renamed')
969981
assert.strictEqual(updatedTriggerRecord.enabled_mode, 'DISABLED')
970982

971983
const { data: reEnabledTriggerRecord } = await axios.patch(`${URL}/triggers/${id}`, {
972-
enabled_mode: 'REPLICA'
984+
enabled_mode: 'REPLICA',
973985
})
974986

975987
assert.strictEqual(reEnabledTriggerRecord.enabled_mode, 'REPLICA')

0 commit comments

Comments
 (0)
0