Use datasource for migrator
This commit is contained in:
parent
f9b8567a3a
commit
0a21ab6941
|
@ -21,25 +21,28 @@ package ca.uhn.fhir.jpa.migrate;
|
|||
*/
|
||||
|
||||
import ca.uhn.fhir.jpa.migrate.taskdef.BaseTask;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public abstract class BaseMigrator implements IMigrator {
|
||||
private static final Logger ourLog = LoggerFactory.getLogger(BaseMigrator.class);
|
||||
|
||||
private boolean myDryRun;
|
||||
private boolean myNoColumnShrink;
|
||||
private boolean myOutOfOrderPermitted;
|
||||
private DriverTypeEnum myDriverType;
|
||||
private String myConnectionUrl;
|
||||
private String myUsername;
|
||||
private String myPassword;
|
||||
private DataSource myDataSource;
|
||||
private List<BaseTask.ExecutedStatement> myExecutedStatements = new ArrayList<>();
|
||||
|
||||
public DataSource getDataSource() {
|
||||
return myDataSource;
|
||||
}
|
||||
|
||||
public void setDataSource(DataSource theDataSource) {
|
||||
myDataSource = theDataSource;
|
||||
}
|
||||
|
||||
public boolean isDryRun() {
|
||||
return myDryRun;
|
||||
}
|
||||
|
@ -64,30 +67,6 @@ public abstract class BaseMigrator implements IMigrator {
|
|||
myDriverType = theDriverType;
|
||||
}
|
||||
|
||||
public String getConnectionUrl() {
|
||||
return myConnectionUrl;
|
||||
}
|
||||
|
||||
public void setConnectionUrl(String theConnectionUrl) {
|
||||
myConnectionUrl = theConnectionUrl;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return myUsername;
|
||||
}
|
||||
|
||||
public void setUsername(String theUsername) {
|
||||
myUsername = theUsername;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return myPassword;
|
||||
}
|
||||
|
||||
public void setPassword(String thePassword) {
|
||||
myPassword = thePassword;
|
||||
}
|
||||
|
||||
public boolean isOutOfOrderPermitted() {
|
||||
return myOutOfOrderPermitted;
|
||||
}
|
||||
|
|
|
@ -14,8 +14,8 @@ import org.springframework.transaction.support.TransactionTemplate;
|
|||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.sql.DataSource;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.Driver;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/*-
|
||||
|
@ -110,13 +110,6 @@ public enum DriverTypeEnum {
|
|||
|
||||
public ConnectionProperties newConnectionProperties(String theUrl, String theUsername, String thePassword) {
|
||||
|
||||
Driver driver;
|
||||
try {
|
||||
driver = (Driver) Class.forName(myDriverClassName).newInstance();
|
||||
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
|
||||
throw new InternalErrorException("Unable to find driver class: " + myDriverClassName, e);
|
||||
}
|
||||
|
||||
BasicDataSource dataSource = new BasicDataSource(){
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
|
@ -129,8 +122,19 @@ public enum DriverTypeEnum {
|
|||
dataSource.setUsername(theUsername);
|
||||
dataSource.setPassword(thePassword);
|
||||
|
||||
return newConnectionProperties(dataSource);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public ConnectionProperties newConnectionProperties(DataSource theDataSource) {
|
||||
try {
|
||||
Class.forName(myDriverClassName).getConstructor().newInstance();
|
||||
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
|
||||
throw new InternalErrorException("Unable to find driver class: " + myDriverClassName, e);
|
||||
}
|
||||
|
||||
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
|
||||
transactionManager.setDataSource(dataSource);
|
||||
transactionManager.setDataSource(theDataSource);
|
||||
transactionManager.afterPropertiesSet();
|
||||
|
||||
TransactionTemplate txTemplate = new TransactionTemplate();
|
||||
|
@ -138,7 +142,7 @@ public enum DriverTypeEnum {
|
|||
txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
|
||||
txTemplate.afterPropertiesSet();
|
||||
|
||||
return new ConnectionProperties(dataSource, txTemplate, this);
|
||||
return new ConnectionProperties(theDataSource, txTemplate, this);
|
||||
}
|
||||
|
||||
public static class ConnectionProperties implements AutoCloseable {
|
||||
|
|
|
@ -22,13 +22,13 @@ package ca.uhn.fhir.jpa.migrate;
|
|||
|
||||
import ca.uhn.fhir.jpa.migrate.taskdef.BaseTask;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import org.apache.commons.dbcp2.BasicDataSource;
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.flywaydb.core.api.MigrationInfoService;
|
||||
import org.flywaydb.core.api.migration.JavaMigration;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
@ -40,22 +40,10 @@ public class FlywayMigrator extends BaseMigrator {
|
|||
private final String myMigrationTableName;
|
||||
private List<FlywayMigration> myTasks = new ArrayList<>();
|
||||
|
||||
public FlywayMigrator(String theMigrationTableName, BasicDataSource theDataSource) {
|
||||
public FlywayMigrator(String theMigrationTableName, DataSource theDataSource, DriverTypeEnum theDriverType) {
|
||||
this(theMigrationTableName);
|
||||
setConnectionUrl(theDataSource.getUrl());
|
||||
setUsername(theDataSource.getUsername());
|
||||
setPassword(theDataSource.getPassword());
|
||||
|
||||
String driverClassName = theDataSource.getDriverClassName();
|
||||
if (driverClassName == null) {
|
||||
ourLog.error(this.getClass().getSimpleName() + " constructed without a database driver");
|
||||
} else {
|
||||
DriverTypeEnum driverType = DriverTypeEnum.fromDriverClassName(driverClassName);
|
||||
if (driverType == null) {
|
||||
ourLog.error("Unknown driver class " + driverClassName);
|
||||
}
|
||||
setDriverType(driverType);
|
||||
}
|
||||
setDataSource(theDataSource);
|
||||
setDriverType(theDriverType);
|
||||
}
|
||||
|
||||
public FlywayMigrator(String theMigrationTableName) {
|
||||
|
@ -68,7 +56,7 @@ public class FlywayMigrator extends BaseMigrator {
|
|||
|
||||
@Override
|
||||
public void migrate() {
|
||||
try (DriverTypeEnum.ConnectionProperties connectionProperties = getDriverType().newConnectionProperties(getConnectionUrl(), getUsername(), getPassword())) {
|
||||
try (DriverTypeEnum.ConnectionProperties connectionProperties = getDriverType().newConnectionProperties(getDataSource())) {
|
||||
Flyway flyway = initFlyway(connectionProperties);
|
||||
flyway.repair();
|
||||
flyway.migrate();
|
||||
|
@ -85,7 +73,7 @@ public class FlywayMigrator extends BaseMigrator {
|
|||
// TODO KHS Is there a way we can use datasource instead of url, username, password here
|
||||
Flyway flyway = Flyway.configure()
|
||||
.table(myMigrationTableName)
|
||||
.dataSource(getConnectionUrl(), getUsername(), getPassword())
|
||||
.dataSource(theConnectionProperties.getDataSource())
|
||||
.baselineOnMigrate(true)
|
||||
.outOfOrder(isOutOfOrderPermitted())
|
||||
.javaMigrations(myTasks.toArray(new JavaMigration[0]))
|
||||
|
@ -106,7 +94,7 @@ public class FlywayMigrator extends BaseMigrator {
|
|||
if (getDriverType() == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
try (DriverTypeEnum.ConnectionProperties connectionProperties = getDriverType().newConnectionProperties(getConnectionUrl(), getUsername(), getPassword())) {
|
||||
try (DriverTypeEnum.ConnectionProperties connectionProperties = getDriverType().newConnectionProperties(getDataSource())) {
|
||||
Flyway flyway = initFlyway(connectionProperties);
|
||||
return Optional.of(flyway.info());
|
||||
}
|
||||
|
|
|
@ -22,13 +22,13 @@ package ca.uhn.fhir.jpa.migrate;
|
|||
|
||||
import ca.uhn.fhir.context.ConfigurationException;
|
||||
import ca.uhn.fhir.jpa.migrate.taskdef.BaseTask;
|
||||
import org.apache.commons.dbcp2.BasicDataSource;
|
||||
import org.flywaydb.core.api.MigrationInfo;
|
||||
import org.flywaydb.core.api.MigrationInfoService;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
@ -38,7 +38,7 @@ import java.util.Properties;
|
|||
public class SchemaMigrator {
|
||||
private static final Logger ourLog = LoggerFactory.getLogger(SchemaMigrator.class);
|
||||
public static final String HAPI_FHIR_MIGRATION_TABLENAME = "FLY_HFJ_MIGRATION";
|
||||
private final BasicDataSource myDataSource;
|
||||
private final DataSource myDataSource;
|
||||
private final boolean mySkipValidation;
|
||||
private final String myMigrationTableName;
|
||||
private final List<BaseTask> myMigrationTasks;
|
||||
|
@ -49,7 +49,7 @@ public class SchemaMigrator {
|
|||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public SchemaMigrator(String theMigrationTableName, BasicDataSource theDataSource, Properties jpaProperties, List<BaseTask> theMigrationTasks) {
|
||||
public SchemaMigrator(String theMigrationTableName, DataSource theDataSource, Properties jpaProperties, List<BaseTask> theMigrationTasks) {
|
||||
myDataSource = theDataSource;
|
||||
myMigrationTableName = theMigrationTableName;
|
||||
myMigrationTasks = theMigrationTasks;
|
||||
|
@ -78,7 +78,9 @@ public class SchemaMigrator {
|
|||
Optional<MigrationInfoService> migrationInfo = newMigrator().getMigrationInfo();
|
||||
if (migrationInfo.isPresent()) {
|
||||
if (migrationInfo.get().pending().length > 0) {
|
||||
throw new ConfigurationException("The database schema for " + myDataSource.getUrl() + " is out of date. " +
|
||||
|
||||
String url = connection.getMetaData().getURL();
|
||||
throw new ConfigurationException("The database schema for " + url + " is out of date. " +
|
||||
"Current database schema version is " + getCurrentVersion(migrationInfo.get()) + ". Schema version required by application is " +
|
||||
getLastVersion(migrationInfo.get()) + ". Please run the database migrator.");
|
||||
}
|
||||
|
@ -102,11 +104,9 @@ public class SchemaMigrator {
|
|||
if (myDontUseFlyway) {
|
||||
migrator = new TaskOnlyMigrator();
|
||||
migrator.setDriverType(myDriverType);
|
||||
migrator.setConnectionUrl(myDataSource.getUrl());
|
||||
migrator.setUsername(myDataSource.getUsername());
|
||||
migrator.setPassword(myDataSource.getPassword());
|
||||
migrator.setDataSource(myDataSource);
|
||||
} else {
|
||||
migrator = new FlywayMigrator(myMigrationTableName, myDataSource);
|
||||
migrator = new FlywayMigrator(myMigrationTableName, myDataSource, myDriverType);
|
||||
migrator.setOutOfOrderPermitted(myOutOfOrderPermitted);
|
||||
}
|
||||
migrator.addTasks(myMigrationTasks);
|
||||
|
|
|
@ -42,7 +42,7 @@ public class TaskOnlyMigrator extends BaseMigrator {
|
|||
|
||||
@Override
|
||||
public void migrate() {
|
||||
DriverTypeEnum.ConnectionProperties connectionProperties = getDriverType().newConnectionProperties(getConnectionUrl(), getUsername(), getPassword());
|
||||
DriverTypeEnum.ConnectionProperties connectionProperties = getDriverType().newConnectionProperties(getDataSource());
|
||||
|
||||
for (BaseTask next : myTasks) {
|
||||
next.setDriverType(getDriverType());
|
||||
|
|
|
@ -42,7 +42,7 @@ public class BaseTest {
|
|||
myDataSource.setUsername("SA");
|
||||
myDataSource.setPassword("SA");
|
||||
myDataSource.setDriverClassName(DriverTypeEnum.H2_EMBEDDED.getDriverClassName());
|
||||
myMigrator = new FlywayMigrator(SchemaMigrator.HAPI_FHIR_MIGRATION_TABLENAME, myDataSource);
|
||||
myMigrator = new FlywayMigrator(SchemaMigrator.HAPI_FHIR_MIGRATION_TABLENAME, myDataSource, DriverTypeEnum.H2_EMBEDDED);
|
||||
}
|
||||
|
||||
protected BasicDataSource getDataSource() {
|
||||
|
|
|
@ -272,7 +272,7 @@ public class SearchParamRegistryImpl implements ISearchParamRegistry {
|
|||
int overriddenCount = 0;
|
||||
List<IBaseResource> allSearchParams = allSearchParamsBp.getResources(0, size);
|
||||
for (IBaseResource nextResource : allSearchParams) {
|
||||
IBaseResource nextSp = (IBaseResource) nextResource;
|
||||
IBaseResource nextSp = nextResource;
|
||||
if (nextSp == null) {
|
||||
continue;
|
||||
}
|
||||
|
@ -327,7 +327,7 @@ public class SearchParamRegistryImpl implements ISearchParamRegistry {
|
|||
populateActiveSearchParams(activeSearchParams);
|
||||
|
||||
myLastRefresh = System.currentTimeMillis();
|
||||
ourLog.info("Refreshed search parameter cache in {}ms", sw.getMillis());
|
||||
ourLog.debug("Refreshed search parameter cache in {}ms", sw.getMillis());
|
||||
return myActiveSearchParams.size();
|
||||
} else {
|
||||
return 0;
|
||||
|
|
Loading…
Reference in New Issue