【问题标题】:Flyway for Existing PUBLIC schema现有 PUBLIC 模式的 Flyway
【发布时间】:2017-09-04 03:26:36
【问题描述】:
    @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.sqlV2__Add_Books.sql。我还看到了日志消息: [main] DEBUG org.flywaydb.core.internal.command.DbSchemas - Schema "PUBLIC" already exists. Skipping schema creation. 任何解决此问题的帮助将不胜感激。谢谢大家!

【问题讨论】:

    标签: java database intellij-idea gradle flyway


    【解决方案1】:

    我已经解决了这个问题,方法是在 application.properties 中为flyway.baseline-version 添加 Spring boot flyway 定义,而不是使用 boxfuse gradle 插件。

    【讨论】:

    • 在 Spring boot 2 中它现在是 spring.flyway.baselineVersion
    猜你喜欢
    • 1970-01-01
    • 2022-01-24
    • 2020-08-21
    • 2018-12-12
    • 2021-09-16
    • 2014-10-23
    • 2015-10-17
    • 2012-09-01
    • 2018-07-10
    相关资源
    最近更新 更多