使用Flyway转MySQL的Spring Boot数据库迁移

时间:2016-04-16 00:55:09

标签: mysql spring-boot flyway

我有一个Gradle Spring Boot Web应用程序,它使用Flyway从H2迁移到PostgreSQL。我试图将其转换为从H2迁移到MySQL。 build.gradle文件是:

buildscript {
    ext {
        springBootVersion = '1.3.0.RELEASE'
    }
    repositories {
        maven { url "http://repo.spring.io/milestone" }
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot' 
apply plugin: 'war'


war {
    baseName = 'demo'
    version = '0.0.1-SNAPSHOT'
    archiveName = 'demo.war'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    maven { url "http://repo.spring.io/milestone" }
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-actuator")
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-starter-security")

    compile("com.h2database:h2")

    compile("org.postgresql:postgresql:9.4-1201-jdbc41")

    compile("org.flywaydb:flyway-core")    

    testCompile("org.springframework.boot:spring-boot-starter-test")
}

eclipse {
    classpath {
         containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
         containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7'
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.12'
}

有一个application.yml文件:

error:
  whitelabel:
    enabled: false

amazon:
  associate_id: habuma-20

spring:
  jpa:
    hibernate:
      ddl-auto: none

---
spring:
  profiles: production
  datasource:
    url: jdbc:postgresql://localhost:5432/readinglist
    username: habuma
    password: 
  jpa:
    database-platform: org.hibernate.dialect.PostgreSQLDialect

在build.gradle中,我替换了

    compile("org.postgresql:postgresql:9.4-1201-jdbc41")

    compile("mysql:mysql-connector-java:5.1.5")

并将application.yml更改为

error:
  whitelabel:
    enabled: false

amazon:
  associate_id: habuma-20

spring:
  jpa:
    hibernate:
      ddl-auto: none

---
spring:
  profiles: production
  datasource:
    url: jdbc:mysql://localhost:3306/readinglist
    username: root
    password: ******
  jpa:
    database-platform: org.hibernate.dialect.MySQL5Dialect

获得"生产" profile我设置了一个环境变量SPRING_PROFILES_ACTIVE = production。然后使用命令java -jar build / libs / demo.war运行。当应用程序尝试启动时,我收到以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration': Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.setFilterChainProxySecurityConfigurer(org.springframework.security.config.annotation.ObjectPostProcessor,java.util.List) throws java.lang.Exception; 
nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfig': Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private readinglist.ReaderRepository readinglist.SecurityConfig.readerRepository; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'readerRepository': Cannot create inner bean '(inner bean)#1c65a791' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1c65a791': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; 
nested exception is 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: Validate failed. Migration Checksum mismatch for migration 1

我不确定这个Checksum不匹配的解决方案是什么。

0 个答案:

没有答案