Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/lib/PostgresMetaPublications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,16 @@ export default class PostgresMetaPublications {
if (publish_delete) publishOps.push('delete')
if (publish_truncate) publishOps.push('truncate')

// When no publish operations are specified, omit the WITH clause entirely.
// PostgreSQL does not accept an empty publish list (WITH (publish = '')) and
// will return an error. Omitting the clause uses PostgreSQL's default, which
// publishes all operations (insert, update, delete, truncate).
const publishClause =
publishOps.length > 0 ? `WITH (publish = '${publishOps.join(',')}')` : ''

const sql = `
CREATE PUBLICATION ${ident(name)} ${tableClause}
WITH (publish = '${publishOps.join(',')}');`
${publishClause};`
const { error } = await this.query(sql)
if (error) {
return { data: null, error }
Expand Down
18 changes: 18 additions & 0 deletions test/lib/publications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,24 @@ test('update no tables -> all tables', async () => {
await pgMeta.publications.remove(res.data!.id)
})

test('create with no publish operations uses PostgreSQL defaults', async () => {
// Regression test: when no publish_* flags are set, the old code generated
// `WITH (publish = '')` which is invalid SQL. The fix omits the WITH clause,
// causing PostgreSQL to default to publishing all operations.
const res = await pgMeta.publications.create({
name: 'pub_no_ops',
})
expect(res.error).toBeNull()
expect(res.data).toMatchObject({
name: 'pub_no_ops',
publish_insert: true,
publish_update: true,
publish_delete: true,
publish_truncate: true,
})
await pgMeta.publications.remove(res.data!.id)
})

test('update all tables -> no tables', async () => {
const { data } = await pgMeta.publications.create({
name: 'pub',
Expand Down