Skip to content

Commit

Permalink
Improve migrator (hapifhir#6120)
Browse files Browse the repository at this point in the history
* wip

* spotless

* Allow schema migrator to propagate down additional CLI-style config

* wip

* implement dummy constructor so i dont break the build

* wip
  • Loading branch information
tadgh authored Jul 20, 2024
1 parent 497eb60 commit fcb69e6
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,17 @@ public DataSource getDataSource() {
return myDataSource;
}

/**
* If set to true, instead of executing migrations, will instead simply print the SQL that would be executed against the connection.
* @return A boolean indicating whether or not the migration should be a dry run
*/
public boolean isDryRun() {
return myDryRun;
}

/**
* If set to true, instead of executing migrations, will instead simply print the SQL that would be executed against the connection.
*/
public void setDryRun(boolean theDryRun) {
myDryRun = theDryRun;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,54 @@
import java.util.Properties;
import javax.sql.DataSource;

/**
* The SchemaMigrator class is responsible for managing and executing database schema migrations during standard bootup.
* It provides methods to validate the schema version and migrate the schema if necessary.
*
* This supports all the configuation needed to run via the CLI or via a bootup migrator. Specifically
* 1. Dry Run
* 2. Enable Heavyweight Tasks
* 3. Task Skipping
*
*/
public class SchemaMigrator {
public static final String HAPI_FHIR_MIGRATION_TABLENAME = "FLY_HFJ_MIGRATION";
private static final Logger ourLog = LoggerFactory.getLogger(SchemaMigrator.class);
/**
* The Schema Name
*/
private final String mySchemaName;
/**
* The datasource to connect to the Database with.
*/
private final DataSource myDataSource;
/**
* Whether to skip validation of the schema
*/
private final boolean mySkipValidation;
/**
* See {@link HapiMigrator#isRunHeavyweightSkippableTasks()}. Enables or disables Optional Heavyweight migrations
*/
private final boolean myRunHeavyweightMigrationTasks;
/**
* The name of the table in which migrations are tracked.
*/
private final String myMigrationTableName;
/**
* The actual task list, which will be filtered during construction, and having all Tasks enumerated in `theMigrationTasksToSkip` removed before execution.
*/
private final MigrationTaskList myMigrationTasks;
/**
* See {@link HapiMigrator#isDryRun()}
*/
private final boolean myDryRun;

private DriverTypeEnum myDriverType;
/**
* Inventory of callbacks to invoke for pre and post execution
*/
private List<IHapiMigrationCallback> myCallbacks = Collections.emptyList();

private final HapiMigrationStorageSvc myHapiMigrationStorageSvc;

/**
Expand All @@ -51,17 +89,45 @@ public SchemaMigrator(
String theSchemaName,
String theMigrationTableName,
DataSource theDataSource,
boolean theEnableHeavyweighTtasks,
String theMigrationTasksToSkip,
boolean theDryRun,
Properties jpaProperties,
MigrationTaskList theMigrationTasks,
HapiMigrationStorageSvc theHapiMigrationStorageSvc) {
mySchemaName = theSchemaName;
myDataSource = theDataSource;
myMigrationTableName = theMigrationTableName;
myMigrationTasks = theMigrationTasks;
myRunHeavyweightMigrationTasks = theEnableHeavyweighTtasks;
mySkipValidation = jpaProperties.containsKey(AvailableSettings.HBM2DDL_AUTO)
&& "update".equals(jpaProperties.getProperty(AvailableSettings.HBM2DDL_AUTO));
myHapiMigrationStorageSvc = theHapiMigrationStorageSvc;
// Skip the skipped versions here.
myMigrationTasks.setDoNothingOnSkippedTasks(theMigrationTasksToSkip);
myDryRun = theDryRun;
}

/**
* Temporary Dummy Constructor to remove once CDR side merges.
*/
public SchemaMigrator(
String theSchemaName,
String theMigrationTableName,
DataSource theDataSource,
Properties jpaProperties,
MigrationTaskList theMigrationTasks,
HapiMigrationStorageSvc theHapiMigrationStorageSvc) {
mySchemaName = theSchemaName;
myDataSource = theDataSource;
myMigrationTableName = theMigrationTableName;
myMigrationTasks = theMigrationTasks;
myRunHeavyweightMigrationTasks = false;
mySkipValidation = jpaProperties.containsKey(AvailableSettings.HBM2DDL_AUTO)
&& "update".equals(jpaProperties.getProperty(AvailableSettings.HBM2DDL_AUTO));
myHapiMigrationStorageSvc = theHapiMigrationStorageSvc;
// Skip the skipped versions here.
myDryRun = false;
}

public void validate() {
Expand Down Expand Up @@ -111,6 +177,8 @@ private HapiMigrator newMigrator() {
migrator = new HapiMigrator(myMigrationTableName, myDataSource, myDriverType);
migrator.addTasks(myMigrationTasks);
migrator.setCallbacks(myCallbacks);
migrator.setDryRun(myDryRun);
migrator.setRunHeavyweightSkippableTasks(myRunHeavyweightMigrationTasks);
return migrator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public MigrationTaskList getAllTasks(T... theVersionEnumValues) {
retval.addAll(nextValues);
}
}

return retval;
}

Expand Down

0 comments on commit fcb69e6

Please sign in to comment.