11import { Router } from 'express'
2- import format from 'pg-format'
2+ import format , { ident } from 'pg-format'
33import { coalesceRowsToArray , toTransaction } from '../lib/helpers'
44import { RunQuery } from '../lib/connectionPool'
55import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants'
@@ -233,16 +233,35 @@ const selectSingleByName = (
233233const createTableSqlize = ( {
234234 name,
235235 schema = 'public' ,
236+ replica_identity,
237+ replica_identity_index,
236238 comment,
237239} : {
238240 name : string
239241 schema ?: string
242+ replica_identity ?: 'DEFAULT' | 'INDEX' | 'FULL' | 'NOTHING'
243+ replica_identity_index ?: string
240244 comment ?: string
241245} ) => {
242246 const tableSql = format ( 'CREATE TABLE IF NOT EXISTS %I.%I ();' , schema , name )
247+ let replicaSql : string
248+ if ( replica_identity === undefined ) {
249+ replicaSql = ''
250+ } else if ( replica_identity === 'INDEX' ) {
251+ replicaSql = `ALTER TABLE ${ ident ( schema ) } .${ ident (
252+ name
253+ ) } REPLICA IDENTITY USING INDEX ${ replica_identity_index } ;`
254+ } else {
255+ replicaSql = `ALTER TABLE ${ ident ( schema ) } .${ ident ( name ) } REPLICA IDENTITY ${ replica_identity } ;`
256+ }
243257 const commentSql =
244258 comment === undefined ? '' : format ( 'COMMENT ON TABLE %I.%I IS %L;' , schema , name , comment )
245- return `${ tableSql } ${ commentSql } `
259+ return `
260+ BEGIN;
261+ ${ tableSql }
262+ ${ replicaSql }
263+ ${ commentSql }
264+ COMMIT;`
246265}
247266const alterTableName = ( previousName : string , newName : string , schema : string ) => {
248267 return format ( 'ALTER TABLE %I.%I RENAME TO %I;' , schema , previousName , newName )
@@ -252,15 +271,19 @@ const alterTableSql = ({
252271 name,
253272 rls_enabled,
254273 rls_forced,
274+ replica_identity,
275+ replica_identity_index,
255276 comment,
256277} : {
257278 schema ?: string
258279 name : string
259280 rls_enabled ?: boolean
260281 rls_forced ?: boolean
282+ replica_identity ?: 'DEFAULT' | 'INDEX' | 'FULL' | 'NOTHING'
283+ replica_identity_index ?: string
261284 comment ?: string
262285} ) => {
263- let alter = format ( 'ALTER table %I.%I' , schema , name )
286+ let alter = format ( 'ALTER TABLE %I.%I' , schema , name )
264287 let enableRls = ''
265288 if ( rls_enabled !== undefined ) {
266289 let enable = `${ alter } ENABLE ROW LEVEL SECURITY;`
@@ -273,13 +296,23 @@ const alterTableSql = ({
273296 let disable = `${ alter } NO FORCE ROW LEVEL SECURITY;`
274297 forceRls = rls_forced ? enable : disable
275298 }
299+ let replicaSql : string
300+ if ( replica_identity === undefined ) {
301+ replicaSql = ''
302+ } else if ( replica_identity === 'INDEX' ) {
303+ replicaSql = `${ alter } REPLICA IDENTITY USING INDEX ${ replica_identity_index } ;`
304+ } else {
305+ replicaSql = `${ alter } REPLICA IDENTITY ${ replica_identity } ;`
306+ }
276307 const commentSql =
277308 comment === undefined ? '' : format ( 'COMMENT ON TABLE %I.%I IS %L;' , schema , name , comment )
278309 return `
310+ BEGIN;
279311 ${ enableRls }
280312 ${ forceRls }
313+ ${ replicaSql }
281314 ${ commentSql }
282- ` . trim ( )
315+ COMMIT;`
283316}
284317const dropTableSql = ( schema : string , name : string , cascade : boolean ) => {
285318 return format ( `DROP TABLE %I.%I ${ cascade ? 'CASCADE' : 'RESTRICT' } ;` , schema , name )
0 commit comments