22
33import ca .uhn .fhir .jpa .migrate .DriverTypeEnum ;
44import ca .uhn .fhir .jpa .migrate .JdbcUtils ;
5+ import ca .uhn .fhir .jpa .migrate .SchemaMigrator ;
6+ import ca .uhn .fhir .jpa .migrate .dao .HapiMigrationDao ;
7+ import ca .uhn .fhir .jpa .migrate .entity .HapiMigrationEntity ;
58import ca .uhn .fhir .system .HapiSystemProperties ;
69import com .google .common .base .Charsets ;
710import org .apache .commons .io .FileUtils ;
@@ -123,11 +126,13 @@ public void testMigrateFrom340_dryRun() throws IOException, SQLException {
123126
124127 String url = "jdbc:h2:" + location .getAbsolutePath ();
125128 DriverTypeEnum .ConnectionProperties connectionProperties = DriverTypeEnum .H2_EMBEDDED .newConnectionProperties (url , "" , "" );
129+ HapiMigrationDao hapiMigrationDao = new HapiMigrationDao (connectionProperties .getDataSource (), connectionProperties .getDriverType (), SchemaMigrator .HAPI_FHIR_MIGRATION_TABLENAME );
126130
127131 String initSql = "/persistence_create_h2_340.sql" ;
128132 executeSqlStatements (connectionProperties , initSql );
129133
130134 seedDatabase340 (connectionProperties );
135+ seedDatabaseMigration340 (hapiMigrationDao );
131136
132137 ourLog .info ("**********************************************" );
133138 ourLog .info ("Done Setup, Starting Migration..." );
@@ -160,6 +165,7 @@ public void testMigrateFrom340_dryRun() throws IOException, SQLException {
160165 // Verify that foreign key FK_SEARCHRES_RES on HFJ_SEARCH_RESULT exists
161166 foreignKeys = JdbcUtils .getForeignKeys (connectionProperties , "HFJ_RESOURCE" , "HFJ_SEARCH_RESULT" );
162167 assertTrue (foreignKeys .contains ("FK_SEARCHRES_RES" ));
168+ int expectedMigrationEntities = hapiMigrationDao .findAll ().size ();
163169
164170 App .main (args );
165171
@@ -181,6 +187,8 @@ public void testMigrateFrom340_dryRun() throws IOException, SQLException {
181187 // Verify that foreign key FK_SEARCHRES_RES on HFJ_SEARCH_RESULT still exists
182188 foreignKeys = JdbcUtils .getForeignKeys (connectionProperties , "HFJ_RESOURCE" , "HFJ_SEARCH_RESULT" );
183189 assertTrue (foreignKeys .contains ("FK_SEARCHRES_RES" ));
190+ assertTrue (expectedMigrationEntities == hapiMigrationDao .findAll ().size ());
191+
184192 }
185193
186194 @ Test
@@ -210,6 +218,38 @@ public void testMigrateFromEmptySchema() throws IOException, SQLException {
210218 assertTrue (JdbcUtils .getTableNames (connectionProperties ).contains ("HFJ_BLK_EXPORT_JOB" )); // Late table
211219 }
212220
221+ @ Test
222+ public void testMigrateFrom340_dryRun_whenNoMigrationTableExists () throws IOException , SQLException {
223+
224+ File location = getLocation ("migrator_h2_test_340_dryrun" );
225+
226+ String url = "jdbc:h2:" + location .getAbsolutePath ();
227+ DriverTypeEnum .ConnectionProperties connectionProperties = DriverTypeEnum .H2_EMBEDDED .newConnectionProperties (url , "" , "" );
228+ HapiMigrationDao hapiMigrationDao = new HapiMigrationDao (connectionProperties .getDataSource (), connectionProperties .getDriverType (), SchemaMigrator .HAPI_FHIR_MIGRATION_TABLENAME );
229+
230+ String initSql = "/persistence_create_h2_340.sql" ;
231+ executeSqlStatements (connectionProperties , initSql );
232+
233+ seedDatabase340 (connectionProperties );
234+
235+ ourLog .info ("**********************************************" );
236+ ourLog .info ("Done Setup, Starting Migration..." );
237+ ourLog .info ("**********************************************" );
238+
239+ String [] args = new String []{
240+ BaseFlywayMigrateDatabaseCommand .MIGRATE_DATABASE ,
241+ "-d" , "H2_EMBEDDED" ,
242+ "-u" , url ,
243+ "-n" , "" ,
244+ "-p" , "" ,
245+ "-r"
246+ };
247+
248+ App .main (args );
249+
250+ assertFalse (JdbcUtils .getTableNames (connectionProperties ).contains ("FLY_HFJ_MIGRATION" ));
251+ }
252+
213253 @ Nonnull
214254 private File getLocation (String theDatabaseName ) throws IOException {
215255 File directory = new File (DB_DIRECTORY );
@@ -360,4 +400,16 @@ private void executeSqlStatements(DriverTypeEnum.ConnectionProperties theConnect
360400
361401 }
362402
403+ private void seedDatabaseMigration340 (HapiMigrationDao theHapiMigrationDao ) {
404+ theHapiMigrationDao .createMigrationTableIfRequired ();
405+ HapiMigrationEntity hapiMigrationEntity = new HapiMigrationEntity ();
406+ hapiMigrationEntity .setPid (1 );
407+ hapiMigrationEntity .setVersion ("3.4.0.20180401.1" );
408+ hapiMigrationEntity .setDescription ("some sql statement" );
409+ hapiMigrationEntity .setExecutionTime (25 );
410+ hapiMigrationEntity .setSuccess (true );
411+
412+ theHapiMigrationDao .save (hapiMigrationEntity );
413+ }
414+
363415}
0 commit comments