@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] DEBUG org.flywaydb.core.internal.command.DbSchemas - Schema "PUBLIC" already exists. Skipping schema creation.
任何帮助解决这个问题将非常感激。谢谢你们!
答案 0 :(得分:1)
我通过在flyway.baseline-version
的application.properties中添加Spring引导flyway定义而不是使用boxfuse gradle插件来解决这个问题。