@@ -190,6 +190,61 @@ test("with pool client", async (t) => {
190
190
}
191
191
} )
192
192
193
+ test ( "with custom migration table name" , async ( t ) => {
194
+ const databaseName = "migration-test-custom-migration-table"
195
+ const dbConfig = {
196
+ database : databaseName ,
197
+ user : "postgres" ,
198
+ password : PASSWORD ,
199
+ host : "localhost" ,
200
+ port,
201
+ }
202
+
203
+ const migrateWithCustomMigrationTable = ( ) =>
204
+ migrate ( dbConfig , "src/__tests__/fixtures/success-first" , {
205
+ migrationTableName : "my_migrations" ,
206
+ } )
207
+
208
+ await createDb ( databaseName , dbConfig )
209
+ await migrateWithCustomMigrationTable ( )
210
+
211
+ t . truthy ( await doesTableExist ( dbConfig , "my_migrations" ) )
212
+ t . truthy ( await doesTableExist ( dbConfig , "success" ) )
213
+
214
+ await migrateWithCustomMigrationTable ( )
215
+ } )
216
+
217
+ test ( "with custom migration table name in a custom schema" , async ( t ) => {
218
+ const databaseName = "migration-test-custom-schema-custom-migration-table"
219
+ const dbConfig = {
220
+ database : databaseName ,
221
+ user : "postgres" ,
222
+ password : PASSWORD ,
223
+ host : "localhost" ,
224
+ port,
225
+ }
226
+
227
+ const migrateWithCustomMigrationTable = ( ) =>
228
+ migrate ( dbConfig , "src/__tests__/fixtures/success-first" , {
229
+ migrationTableName : "my_schema.my_migrations" ,
230
+ } )
231
+
232
+ const pool = new pg . Pool ( dbConfig )
233
+
234
+ try {
235
+ await createDb ( databaseName , dbConfig )
236
+ await pool . query ( "CREATE SCHEMA IF NOT EXISTS my_schema" )
237
+ await migrateWithCustomMigrationTable ( )
238
+
239
+ t . truthy ( await doesTableExist ( dbConfig , "my_schema.my_migrations" ) )
240
+ t . truthy ( await doesTableExist ( dbConfig , "success" ) )
241
+
242
+ await migrateWithCustomMigrationTable ( )
243
+ } finally {
244
+ await pool . end ( )
245
+ }
246
+ } )
247
+
193
248
test ( "successful first migration" , ( t ) => {
194
249
const databaseName = "migration-test-success-first"
195
250
const dbConfig = {
@@ -284,6 +339,31 @@ test("successful complex js migration", (t) => {
284
339
} )
285
340
} )
286
341
342
+ test ( "successful migration on an existing database" , async ( t ) => {
343
+ const databaseName = "migration-test-success-existing-db"
344
+ const dbConfig = {
345
+ database : databaseName ,
346
+ user : "postgres" ,
347
+ password : PASSWORD ,
348
+ host : "localhost" ,
349
+ port,
350
+ }
351
+
352
+ const pool = new pg . Pool ( dbConfig )
353
+
354
+ try {
355
+ await createDb ( databaseName , dbConfig )
356
+ await pool . query ( require ( "./fixtures/success-existing-db/restore.sql" ) )
357
+ await migrate (
358
+ dbConfig ,
359
+ "src/__tests__/fixtures/success-existing-db/migrations" ,
360
+ )
361
+ t . truthy ( await doesTableExist ( dbConfig , "success" ) )
362
+ } finally {
363
+ await pool . end ( )
364
+ }
365
+ } )
366
+
287
367
test ( "bad arguments - no db config" , ( t ) => {
288
368
// tslint:disable-next-line no-any
289
369
return t . throwsAsync ( ( migrate as any ) ( ) ) . then ( ( err ) => {
@@ -695,28 +775,27 @@ function doesTableExist(dbConfig: pg.ClientConfig, tableName: string) {
695
775
. connect ( )
696
776
. then ( ( ) =>
697
777
client . query ( SQL `
698
- SELECT EXISTS (
699
- SELECT 1
700
- FROM pg_catalog.pg_class c
701
- WHERE c.relname = ${ tableName }
702
- AND c.relkind = 'r'
703
- );
778
+ SELECT to_regclass(${ tableName } ) as matching_tables
704
779
` ) ,
705
780
)
706
781
. then ( ( result ) => {
707
782
try {
708
783
return client
709
784
. end ( )
710
785
. then ( ( ) => {
711
- return result . rows . length > 0 && result . rows [ 0 ] . exists
786
+ return (
787
+ result . rows . length > 0 && result . rows [ 0 ] . matching_tables !== null
788
+ )
712
789
} )
713
790
. catch ( ( error ) => {
714
791
console . log ( "Async error in 'doesTableExist" , error )
715
- return result . rows . length > 0 && result . rows [ 0 ] . exists
792
+ return (
793
+ result . rows . length > 0 && result . rows [ 0 ] . matching_tables !== null
794
+ )
716
795
} )
717
796
} catch ( error ) {
718
797
console . log ( "Sync error in 'doesTableExist" , error )
719
- return result . rows . length > 0 && result . rows [ 0 ] . exists
798
+ return result . rows . length > 0 && result . rows [ 0 ] . matching_tables !== null
720
799
}
721
800
} )
722
801
}
0 commit comments