@@ -190,6 +190,61 @@ test("with pool client", async (t) => {
190190 }
191191} )
192192
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+
193248test ( "successful first migration" , ( t ) => {
194249 const databaseName = "migration-test-success-first"
195250 const dbConfig = {
@@ -284,6 +339,31 @@ test("successful complex js migration", (t) => {
284339 } )
285340} )
286341
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+
287367test ( "bad arguments - no db config" , ( t ) => {
288368 // tslint:disable-next-line no-any
289369 return t . throwsAsync ( ( migrate as any ) ( ) ) . then ( ( err ) => {
@@ -695,28 +775,27 @@ function doesTableExist(dbConfig: pg.ClientConfig, tableName: string) {
695775 . connect ( )
696776 . then ( ( ) =>
697777 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
704779 ` ) ,
705780 )
706781 . then ( ( result ) => {
707782 try {
708783 return client
709784 . end ( )
710785 . 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+ )
712789 } )
713790 . catch ( ( error ) => {
714791 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+ )
716795 } )
717796 } catch ( error ) {
718797 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
720799 }
721800 } )
722801}
0 commit comments