Skip to content

Commit 988bc66

Browse files
epeartreepeartree
authored andcommitted
CLI tool command migrate-database executing in dry-run mode insert entries into table FLY_HFJ_MIGRATION (#5487)
* initial test * Solution with changelog. * making spotless hapi * addressing comments from code reviews * making the test better. * addressing code review comment and adding test. --------- Co-authored-by: peartree <[email protected]>
1 parent 97b11b1 commit 988bc66

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/HapiFlywayMigrateDatabaseCommandTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import ca.uhn.fhir.jpa.migrate.DriverTypeEnum;
44
import 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;
58
import ca.uhn.fhir.system.HapiSystemProperties;
69
import com.google.common.base.Charsets;
710
import 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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
type: fix
3+
issue: 5486
4+
jira: SMILE-7457
5+
title: "Previously, testing database migration with cli migrate-database command in dry-run mode would insert in the
6+
migration task table. The issue has been fixed."

hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/HapiMigrator.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ private void preExecute(BaseTask theTask) {
188188
}
189189

190190
private void postExecute(BaseTask theNext, StopWatch theStopWatch, boolean theSuccess) {
191-
myHapiMigrationStorageSvc.saveTask(theNext, Math.toIntExact(theStopWatch.getMillis()), theSuccess);
191+
if (!theNext.isDryRun()) {
192+
myHapiMigrationStorageSvc.saveTask(theNext, Math.toIntExact(theStopWatch.getMillis()), theSuccess);
193+
}
192194
}
193195

194196
public void addTasks(Iterable<BaseTask> theMigrationTasks) {
@@ -219,6 +221,8 @@ public void removeAllTasksForUnitTest() {
219221
}
220222

221223
public void createMigrationTableIfRequired() {
222-
myHapiMigrationStorageSvc.createMigrationTableIfRequired();
224+
if (!myDryRun) {
225+
myHapiMigrationStorageSvc.createMigrationTableIfRequired();
226+
}
223227
}
224228
}

0 commit comments

Comments
 (0)