1
1
import { Router } from 'express'
2
- import format from 'pg-format'
2
+ import format , { ident } from 'pg-format'
3
3
import { coalesceRowsToArray , toTransaction } from '../lib/helpers'
4
4
import { RunQuery } from '../lib/connectionPool'
5
5
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants'
@@ -233,16 +233,35 @@ const selectSingleByName = (
233
233
const createTableSqlize = ( {
234
234
name,
235
235
schema = 'public' ,
236
+ replica_identity,
237
+ replica_identity_index,
236
238
comment,
237
239
} : {
238
240
name : string
239
241
schema ?: string
242
+ replica_identity ?: 'DEFAULT' | 'INDEX' | 'FULL' | 'NOTHING'
243
+ replica_identity_index ?: string
240
244
comment ?: string
241
245
} ) => {
242
246
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
+ }
243
257
const commentSql =
244
258
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;`
246
265
}
247
266
const alterTableName = ( previousName : string , newName : string , schema : string ) => {
248
267
return format ( 'ALTER TABLE %I.%I RENAME TO %I;' , schema , previousName , newName )
@@ -252,15 +271,19 @@ const alterTableSql = ({
252
271
name,
253
272
rls_enabled,
254
273
rls_forced,
274
+ replica_identity,
275
+ replica_identity_index,
255
276
comment,
256
277
} : {
257
278
schema ?: string
258
279
name : string
259
280
rls_enabled ?: boolean
260
281
rls_forced ?: boolean
282
+ replica_identity ?: 'DEFAULT' | 'INDEX' | 'FULL' | 'NOTHING'
283
+ replica_identity_index ?: string
261
284
comment ?: string
262
285
} ) => {
263
- let alter = format ( 'ALTER table %I.%I' , schema , name )
286
+ let alter = format ( 'ALTER TABLE %I.%I' , schema , name )
264
287
let enableRls = ''
265
288
if ( rls_enabled !== undefined ) {
266
289
let enable = `${ alter } ENABLE ROW LEVEL SECURITY;`
@@ -273,13 +296,23 @@ const alterTableSql = ({
273
296
let disable = `${ alter } NO FORCE ROW LEVEL SECURITY;`
274
297
forceRls = rls_forced ? enable : disable
275
298
}
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
+ }
276
307
const commentSql =
277
308
comment === undefined ? '' : format ( 'COMMENT ON TABLE %I.%I IS %L;' , schema , name , comment )
278
309
return `
310
+ BEGIN;
279
311
${ enableRls }
280
312
${ forceRls }
313
+ ${ replicaSql }
281
314
${ commentSql }
282
- ` . trim ( )
315
+ COMMIT;`
283
316
}
284
317
const dropTableSql = ( schema : string , name : string , cascade : boolean ) => {
285
318
return format ( `DROP TABLE %I.%I ${ cascade ? 'CASCADE' : 'RESTRICT' } ;` , schema , name )
0 commit comments