Skip to content

Commit 03a343e

Browse files
committed
fix(publications): update logic following #98
Probably better to specify `tables` using OIDs when creating publications, but this'll do for now.
1 parent 758d53f commit 03a343e

File tree

5 files changed

+14
-14
lines changed

5 files changed

+14
-14
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"build:server": "tsc -p tsconfig.server.json && cpy 'src/lib/sql/*.sql' bin/src/lib/sql",
2222
"start": "node bin/src/server/app.js | pino-pretty --colorize --levelFirst",
2323
"pkg": "NODE_ENV=production run-s build:server && pkg --out-path bin .pkg.config.json",
24-
"dev": "NODE_ENV=development run-s build start",
24+
"dev": "NODE_ENV=development run-s build:server start",
2525
"dev:watch": "nodemon",
2626
"test": "node -r esm ./node_modules/.bin/mocha 'test/**/*.js' --recursive "
2727
},

src/lib/PostgresMetaPublications.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export default class PostgresMetaPublications {
6868
} else if (tables.length === 0) {
6969
tableClause = ''
7070
} else {
71-
tableClause = `FOR TABLE ${tables.map(ident).join(',')}`
71+
tableClause = `FOR TABLE ${tables.join(',')}`
7272
}
7373

7474
let publishOps = []
@@ -122,7 +122,7 @@ CREATE PUBLICATION ${ident(name)} ${tableClause}
122122
// ---------|-----------|-----------------|
123123
// null | '' | 400 Bad Request |
124124
// old tables ---------|-----------|-----------------|
125-
// string[] | '' | See below |
125+
// object[] | '' | See below |
126126
//
127127
// new tables
128128
//
@@ -138,12 +138,12 @@ CREATE PUBLICATION ${ident(name)} ${tableClause}
138138
} else if (old!.tables === null) {
139139
throw new Error('Tables cannot be added to or dropped from FOR ALL TABLES publications')
140140
} else if (tables.length > 0) {
141-
tableSql = `ALTER PUBLICATION ${ident(old!.name)} SET TABLE ${tables.map(ident).join(',')};`
141+
tableSql = `ALTER PUBLICATION ${ident(old!.name)} SET TABLE ${tables.join(',')};`
142142
} else if (old!.tables.length === 0) {
143143
tableSql = ''
144144
} else {
145145
tableSql = `ALTER PUBLICATION ${ident(old!.name)} DROP TABLE ${old!.tables
146-
.map(ident)
146+
.map((table) => `${ident(table.schema)}.${ident(table.name)}`)
147147
.join(',')};`
148148
}
149149

src/lib/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export interface PostgresPublication {
110110
publish_update: boolean
111111
publish_delete: boolean
112112
publish_truncate: boolean
113-
tables: string[] | null
113+
tables: { id: number; name: string; schema: string }[] | null
114114
}
115115

116116
export interface PostgresRelationship {

src/old/api/publications.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ const createPublicationSqlize = ({
8989
publish_truncate?: boolean
9090
tables?: string[]
9191
}) => {
92-
let tableClause: string = `FOR TABLE ${tables!.map(ident).join(',')}`
92+
let tableClause: string = `FOR TABLE ${tables!.join(',')}`
9393
if (tables === undefined) {
9494
tableClause = 'FOR ALL TABLES'
9595
} else if (tables.length === 0) {
@@ -145,7 +145,7 @@ const alterPublicationSqlize = ({
145145
// ---------|-----------|-----------------|
146146
// null | '' | 400 Bad Request |
147147
// old tables ---------|-----------|-----------------|
148-
// string[] | '' | See below |
148+
// object[] | '' | See below |
149149
//
150150
// new tables
151151
//
@@ -161,15 +161,15 @@ const alterPublicationSqlize = ({
161161
} else if (oldPublication.tables === null) {
162162
throw Error('Tables cannot be added to or dropped from FOR ALL TABLES publications')
163163
} else if (tables.length > 0) {
164-
tableSql = `ALTER PUBLICATION ${ident(oldPublication.name)} SET TABLE ${tables
165-
.map(ident)
166-
.join(',')}`
164+
tableSql = `ALTER PUBLICATION ${ident(oldPublication.name)} SET TABLE ${tables.join(',')}`
167165
} else if (oldPublication.tables.length === 0) {
168166
tableSql = ''
169167
} else {
170168
tableSql = `ALTER PUBLICATION ${ident(
171169
oldPublication.name
172-
)} DROP TABLE ${oldPublication.tables.map(ident).join(',')}`
170+
)} DROP TABLE ${oldPublication.tables
171+
.map((table: any) => `${ident(table.schema)}.${ident(table.name)}`)
172+
.join(',')}`
173173
}
174174

175175
let publishOps = []

test/integration/index.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ describe('/publications', () => {
661661
assert.equal(newPublication.publish_update, publication.publish_update)
662662
assert.equal(newPublication.publish_delete, publication.publish_delete)
663663
assert.equal(newPublication.publish_truncate, publication.publish_truncate)
664-
assert.equal(newPublication.tables.includes('users'), true)
664+
assert.equal(newPublication.tables.some(table => `${table.schema}.${table.name}` === 'public.users'), true)
665665
})
666666
it('GET', async () => {
667667
const res = await axios.get(`${URL}/publications`)
@@ -679,7 +679,7 @@ describe('/publications', () => {
679679
})
680680
assert.equal(updated.name, 'b')
681681
assert.equal(updated.publish_insert, false)
682-
assert.equal(updated.tables.includes('users'), false)
682+
assert.equal(updated.tables.some(table => `${table.schema}.${table.name}` === 'public.users'), false)
683683
})
684684
it('DELETE', async () => {
685685
const res = await axios.get(`${URL}/publications`)

0 commit comments

Comments
 (0)