Add schema initialization

This commit is contained in:
Tadgh 2020-05-27 17:06:52 -07:00
parent 48fcf8a39d
commit 81d5ff8f5a
14 changed files with 151 additions and 38 deletions

View File

@ -88,7 +88,6 @@ public class PartitionLookupSvcImpl implements IPartitionLookupSvc {
myPartitionDao.save(partitionEntity); myPartitionDao.save(partitionEntity);
} }
}); });
} }
@Override @Override

View File

@ -18,7 +18,7 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>ca.uhn.hapi.fhir</groupId> <groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-server-empi</artifactId> <artifactId>hapi-fhir-jpaserver-migrate</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -1,24 +0,0 @@
package ca.uhn.fhir.jpa.batch.config;
import ca.uhn.fhir.jpa.batch.svc.DummyService;
import org.springframework.batch.core.configuration.annotation.BatchConfigurer;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableBatchProcessing
public class BatchConfig {
@Bean
public DummyService dummyService() {
return new DummyService();
}
@Bean
public BatchConfigurer jpaBatchConfigurer() {
return new JpaBatchConfigurer();
}
}

View File

@ -2,17 +2,22 @@ package ca.uhn.fhir.jpa.batch.config;
import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer; import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.PlatformTransactionManager;
/*//
@Component @Component
public class JpaBatchConfigurer extends DefaultBatchConfigurer { public class JpaBatchConfigurer extends DefaultBatchConfigurer {
@Autowired @Autowired
private PlatformTransactionManager myPlatformTransactionManager; @Qualifier
private JpaTransactionManager myPlatformTransactionManager;
@Override @Override
public PlatformTransactionManager getTransactionManager() { public PlatformTransactionManager getTransactionManager() {
return myPlatformTransactionManager; return myPlatformTransactionManager;
} }
} }
*/

View File

@ -2,8 +2,9 @@ package ca.uhn.fhir.jpa.batch;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao; import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao;
import ca.uhn.fhir.jpa.batch.config.BatchConfig; import ca.uhn.fhir.jpa.batch.config.BaseTestBatchConfig;
import ca.uhn.fhir.jpa.batch.svc.DummyService; import ca.uhn.fhir.jpa.batch.svc.DummyService;
import ca.uhn.fhir.jpa.config.TestJpaR4Config;
import ca.uhn.fhir.jpa.dao.index.IdHelperService; import ca.uhn.fhir.jpa.dao.index.IdHelperService;
import ca.uhn.fhir.jpa.test.BaseJpaR4Test; import ca.uhn.fhir.jpa.test.BaseJpaR4Test;
import org.hl7.fhir.r4.model.Patient; import org.hl7.fhir.r4.model.Patient;
@ -11,17 +12,26 @@ import org.hl7.fhir.r4.model.Person;
import org.hl7.fhir.r4.model.Practitioner; import org.hl7.fhir.r4.model.Practitioner;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.PlatformTransactionManager;
import javax.annotation.PostConstruct;
import static org.slf4j.LoggerFactory.getLogger; import static org.slf4j.LoggerFactory.getLogger;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ContextConfiguration(classes = {BatchConfig.class}) @ContextConfiguration(classes = {BaseTestBatchConfig.class, TestJpaR4Config.class})
abstract public class BaseBatchR4Test extends BaseJpaR4Test { abstract public class BaseBatchR4Test extends BaseJpaR4Test {
private static final Logger ourLog = getLogger(BaseBatchR4Test.class); private static final Logger ourLog = getLogger(BaseBatchR4Test.class);
@Autowired
protected PlatformTransactionManager myPlatformTransactionManager;
@Autowired @Autowired
protected FhirContext myFhirContext; protected FhirContext myFhirContext;
@Autowired @Autowired
@ -34,4 +44,9 @@ abstract public class BaseBatchR4Test extends BaseJpaR4Test {
protected IdHelperService myIdHelperService; protected IdHelperService myIdHelperService;
@Autowired @Autowired
protected DummyService myDummyService; protected DummyService myDummyService;
@Autowired
protected JobLauncher myJobLauncher;
@Autowired
protected Job myJob;
} }

View File

@ -1,8 +1,57 @@
package ca.uhn.fhir.jpa.batch.config; package ca.uhn.fhir.jpa.batch.config;
import ca.uhn.fhir.jpa.batch.svc.DummyService;
import ca.uhn.fhir.jpa.config.TestJpaConfig;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
@Configuration @Configuration
public class BaseTestBatchConfig { @EnableBatchProcessing
public class BaseTestBatchConfig extends DefaultBatchConfigurer {
@Autowired
private JpaTransactionManager myPlatformTransactionManager;
@Autowired
private JobBuilderFactory myJobBuilderFactory;
@Autowired
private StepBuilderFactory myStepBuilderFactory;
@Override
public PlatformTransactionManager getTransactionManager() {
return myPlatformTransactionManager;
}
@Bean
public DummyService myDummyService() {
return new DummyService();
}
@Bean
public Job testJob() {
return myJobBuilderFactory.get("testJob")
.start(testStep())
.build();
}
@Bean
public Step testStep() {
return myStepBuilderFactory.get("testStep").tasklet((theStepContribution, theChunkContext) -> {
System.out.println("woo!");
return RepeatStatus.FINISHED;
}).build();
}
} }

View File

@ -2,11 +2,17 @@ package ca.uhn.fhir.jpa.batch.svc;
import ca.uhn.fhir.jpa.batch.BaseBatchR4Test; import ca.uhn.fhir.jpa.batch.BaseBatchR4Test;
import org.junit.Test; import org.junit.Test;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
public class BatchSvcTest extends BaseBatchR4Test { public class BatchSvcTest extends BaseBatchR4Test {
@Test @Test
public void testApplicationContextLoads(){ public void testApplicationContextLoads() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
myDummyService.test(); myDummyService.test();
myJobLauncher.run(myJob, new JobParameters());
} }
} }

View File

@ -1,7 +1,7 @@
<configuration> <configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>TRACE</level> <level>INFO</level>
</filter> </filter>
<encoder> <encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} [%file:%line] %msg%n</pattern> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} [%file:%line] %msg%n</pattern>
@ -48,7 +48,7 @@
<appender-ref ref="STDOUT" /> <appender-ref ref="STDOUT" />
</logger> </logger>
<appender name="BATCH_TROUBLESHOOTING" class="ch.qos.logback.core.rolling.RollingFileAppender"> <appender name="BATCH_TROUBLESHOOTING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter"><level>DEBUG</level></filter> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"><level>INFO</level></filter>
<file>${smile.basedir}/log/batch-troubleshooting.log</file> <file>${smile.basedir}/log/batch-troubleshooting.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${smile.basedir}/log/batch-troubleshooting.log.%i.gz</fileNamePattern> <fileNamePattern>${smile.basedir}/log/batch-troubleshooting.log.%i.gz</fileNamePattern>

View File

@ -42,6 +42,12 @@
<artifactId>hapi-fhir-jpaserver-base</artifactId> <artifactId>hapi-fhir-jpaserver-base</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<!-- Need this for the schema files from spring batch -->
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<!-- Test Database --> <!-- Test Database -->
<dependency> <dependency>

View File

@ -0,0 +1,35 @@
package ca.uhn.fhir.jpa.migrate.providers;
import ca.uhn.fhir.jpa.migrate.DriverTypeEnum;
import ca.uhn.fhir.jpa.migrate.tasks.SchemaInitializationProvider;
import com.google.common.collect.Maps;
import javax.annotation.Nonnull;
import java.util.Map;
public class SpringBatchSchemaMigrationProvider extends SchemaInitializationProvider {
private static final Map<DriverTypeEnum, String> ourBatchDriverMap;
static {
ourBatchDriverMap = Maps.newHashMap();
ourBatchDriverMap.put(DriverTypeEnum.H2_EMBEDDED, "schema-h2.sql");
ourBatchDriverMap.put(DriverTypeEnum.DERBY_EMBEDDED, "schema-derby.sql");
ourBatchDriverMap.put(DriverTypeEnum.MARIADB_10_1, "schema-mysql.sql");//not sure if correct
ourBatchDriverMap.put(DriverTypeEnum.MYSQL_5_7, "schema-mysql.sql");//not sure if correct
ourBatchDriverMap.put(DriverTypeEnum.POSTGRES_9_4, "schema-postgresql.sql");
ourBatchDriverMap.put(DriverTypeEnum.ORACLE_12C, "schema-oracle10g.sql");//not sure if correct
ourBatchDriverMap.put(DriverTypeEnum.MSSQL_2012, "schema-mysql.sql");//not sure if correct
}
public SpringBatchSchemaMigrationProvider() {
super("Spring Batch", "/org/springframework/batch/core", "BATCH_JOB_INSTANCE");
}
@Nonnull
@Override
protected String getInitScript(DriverTypeEnum theDriverType) {
return ourBatchDriverMap.get(theDriverType);
}
}

View File

@ -21,6 +21,7 @@ package ca.uhn.fhir.jpa.migrate.tasks;
*/ */
import ca.uhn.fhir.jpa.migrate.DriverTypeEnum; import ca.uhn.fhir.jpa.migrate.DriverTypeEnum;
import ca.uhn.fhir.jpa.migrate.providers.SpringBatchSchemaMigrationProvider;
import ca.uhn.fhir.jpa.migrate.taskdef.AddColumnTask; import ca.uhn.fhir.jpa.migrate.taskdef.AddColumnTask;
import ca.uhn.fhir.jpa.migrate.taskdef.ArbitrarySqlTask; import ca.uhn.fhir.jpa.migrate.taskdef.ArbitrarySqlTask;
import ca.uhn.fhir.jpa.migrate.taskdef.BaseTableColumnTypeTask; import ca.uhn.fhir.jpa.migrate.taskdef.BaseTableColumnTypeTask;
@ -103,6 +104,8 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks<VersionEnum> {
empiLink.addIndex("20200517.5", "IDX_EMPI_PERSON_TGT").unique(true).withColumns("PERSON_PID", "TARGET_PID"); empiLink.addIndex("20200517.5", "IDX_EMPI_PERSON_TGT").unique(true).withColumns("PERSON_PID", "TARGET_PID");
version.initializeSchema("20200526.1", "Spring Batch", new SpringBatchSchemaMigrationProvider());
} }
protected void init500() { // 20200218 - 20200519 protected void init500() { // 20200218 - 20200519

View File

@ -2,6 +2,7 @@ package ca.uhn.fhir.jpa.migrate.taskdef;
import ca.uhn.fhir.jpa.migrate.DriverTypeEnum; import ca.uhn.fhir.jpa.migrate.DriverTypeEnum;
import ca.uhn.fhir.jpa.migrate.JdbcUtils; import ca.uhn.fhir.jpa.migrate.JdbcUtils;
import ca.uhn.fhir.jpa.migrate.providers.SpringBatchSchemaMigrationProvider;
import ca.uhn.fhir.jpa.migrate.tasks.api.ISchemaInitializationProvider; import ca.uhn.fhir.jpa.migrate.tasks.api.ISchemaInitializationProvider;
import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.junit.Test; import org.junit.Test;
@ -12,6 +13,7 @@ import java.util.List;
import java.util.function.Supplier; import java.util.function.Supplier;
import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasItems;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
public class InitializeSchemaTaskTest extends BaseTest { public class InitializeSchemaTaskTest extends BaseTest {
@ -34,6 +36,21 @@ public class InitializeSchemaTaskTest extends BaseTest {
getMigrator().migrate(); getMigrator().migrate();
} }
@Test
public void testInitializeBatch() throws SQLException {
InitializeSchemaTask task = new InitializeSchemaTask("1", "2", new SpringBatchSchemaMigrationProvider());
getMigrator().addTask(task);
getMigrator().migrate();
assertThat(JdbcUtils.getTableNames(getConnectionProperties()), hasItems(
"BATCH_JOB_INSTANCE",
"BATCH_JOB_EXECUTION",
"BATCH_JOB_EXECUTION_PARAMS",
"BATCH_JOB_EXECUTION_CONTEXT",
"BATCH_STEP_EXECUTION",
"BATCH_STEP_EXECUTION_CONTEXT"
));
}
private class TestProvider implements ISchemaInitializationProvider { private class TestProvider implements ISchemaInitializationProvider {
@Override @Override
public List<String> getSqlStatements(DriverTypeEnum theDriverType) { public List<String> getSqlStatements(DriverTypeEnum theDriverType) {

View File

@ -24,10 +24,13 @@ import ca.uhn.fhir.jpa.api.config.DaoConfig;
import ca.uhn.fhir.jpa.model.config.PartitionSettings; import ca.uhn.fhir.jpa.model.config.PartitionSettings;
import ca.uhn.fhir.jpa.model.entity.ModelConfig; import ca.uhn.fhir.jpa.model.entity.ModelConfig;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaTransactionManager;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
// TODO RC1 can use in jpa tests?
@Configuration
public class TestJpaConfig { public class TestJpaConfig {
@Bean @Bean
public DaoConfig daoConfig() { public DaoConfig daoConfig() {
@ -41,6 +44,7 @@ public class TestJpaConfig {
} }
@Bean @Bean
@Primary
public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager retVal = new JpaTransactionManager(); JpaTransactionManager retVal = new JpaTransactionManager();
retVal.setEntityManagerFactory(entityManagerFactory); retVal.setEntityManagerFactory(entityManagerFactory);

View File

@ -22,8 +22,6 @@ package ca.uhn.fhir.jpa.test;
import ca.uhn.fhir.jpa.config.TestJpaR4Config; import ca.uhn.fhir.jpa.config.TestJpaR4Config;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
@ContextConfiguration(classes = {TestJpaR4Config.class})
public abstract class BaseJpaR4Test extends BaseJpaTest { public abstract class BaseJpaR4Test extends BaseJpaTest {
} }