@Configuration
@ComponentScan("com.sammy")
@EnableTransactionManagement
public class DataSourceConfig {
@Bean(destroyMethod = "shutdown")
public DataSource dataSource(){
EmbeddedDatabaseBuilder databaseBuilder = new EmbeddedDatabaseBuilder();
databaseBuilder.setType(EmbeddedDatabaseType.H2);
databaseBuilder.addScript("classpath:db/migration/V1__Create_Books_Table.sql");
databaseBuilder.addScript("classpath:db/migration/V2__Add_Books.sql");
return databaseBuilder.build();
}
@Bean(name = "entityManagerFactory")
public EntityManagerFactory managerFactory(){
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.hbm2ddl.auto", "create-drop");
jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
LocalContainerEntityManagerFactoryBean managerFactoryBean = new LocalContainerEntityManagerFactoryBean();
managerFactoryBean.setDataSource(dataSource());
managerFactoryBean.setPackagesToScan("com.sammy");
managerFactoryBean.setJpaVendorAdapter(vendorAdapter);
managerFactoryBean.setJpaProperties(jpaProperties);
managerFactoryBean.afterPropertiesSet();
return managerFactoryBean.getObject();
}
@Bean
public PlatformTransactionManager transactionManager(){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(managerFactory());
return transactionManager;
}
}
上面是我的数据配置类。
buildscript {
repositories {
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.boxfuse.client:flyway-release:${flywayVersion}"
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:${sonarVersion}"
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}
}
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'org.sonarqube'
apply plugin: 'org.flywaydb.flyway'
apply plugin: 'org.springframework.boot'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
jar{
group 'com.sammy'
version '1.0-SNAPSHOT'
}
dependencies {
testCompile "junit:junit:${junitVersion}"
testCompile "info.cukes:cucumber-java:${cucumberVersion}"
testCompile "info.cukes:cucumber-junit:${cucumberVersion}"
//testCompile "info.cukes:cucumber-spring:${cucumberVersion}"
testCompile 'org.springframework.boot:spring-boot-starter-test'
compile 'com.h2database:h2'
compile "org.flywaydb:flyway-core:${flywayVersion}"
compile "org.projectlombok:lombok:${lombokVersion}"
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile 'org.springframework.boot:spring-boot-starter-aop'
compile 'org.springframework.boot:spring-boot-starter-jetty'
compile "io.springfox:springfox-swagger2:${swaggerVersion}"
compile "io.springfox:springfox-swagger-ui:${swaggerVersion}"
compile 'org.springframework.boot:spring-boot-starter-actuator'
compile 'org.springframework.cloud:spring-cloud-starter-config'
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
//compile 'org.springframework.boot:spring-boot-starter-data-mongodb'
}
flyway{
user = 'sa'
schema = ['PUBLIC', 'testdb']
url = 'jdbc:h2:mem:testdb'
baselineVersion = 7.0
baselineOnMigrate = false
baselineDescription = "Base Migration"
}
flywayMigrate{
dependsOn flyway
}
task wrapper(type :Wrapper){
gradleVersion = '3.4.1'
}
我已经得到了 build.gradle 文件,它尝试配置 Flyway 以在 SpringBoot 应用程序中使用。我正在尝试将数据迁移到内存 H2 数据库,但不断收到以下错误:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Found non-empty schema(s) "PUBLIC" without metadata table! Use baseline() or set baselineOnMigrate to true to initialize the metadata table.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
at com.sammy.SpringDataTutorials.main(SpringDataTutorials.java:18)
Caused by: org.flywaydb.core.api.FlywayException: Found non-empty schema(s) "PUBLIC" without metadata table! Use baseline() or set baselineOnMigrate to true to initialize the metadata table.
at org.flywaydb.core.Flyway$1.execute(Flyway.java:954)
at org.flywaydb.core.Flyway$1.execute(Flyway.java:930)
at org.flywaydb.core.Flyway.execute(Flyway.java:1413)
at org.flywaydb.core.Flyway.migrate(Flyway.java:930)
at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 11 more
我在这里阅读了一些可能的解决方案,但仍然没有任何乐趣,甚至阅读了 boxfuse 官方网站以了解该怎么做,但仍然能够找到解决方案。我的文件位于 src/main/resources/db.migration 中,名称为 V1__Create_Books_Table.sql 和 V2__Add_Books.sql。我还看到日志消息:
[main] 调试 org.flywaydb.core.internal.command.DbSchemas - 架构“PUBLIC”已存在。跳过架构创建。
任何解决此问题的帮助将不胜感激。谢谢大家!
最佳答案
我通过在 flyway.baseline-version
的 application.properties 中添加 Spring boot Flyway 定义来解决这个问题,而不是使用 boxfuse gradle 插件。
关于java - 现有 PUBLIC 模式的 Flyway,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43300233/