我想知道如何正确地为我的SpringBoot应用程序执行Databasemigration。 到目前为止,我读了很多有关Liquibase的文章,也读了一些有关Flyaway的文章,因为它们都在Springdoc中提到。但是Liquibase和Flyaway都无法完成我想做的事情。 Ofc阅读了有关插件等的指南,但稍后会介绍。
我想要的:
最好的情况是几乎自动运行的Databasemigration进程。您将运行gradle任务并创建diffChangelog,仔细检查它,然后进行部署/迁移。 如果相关,我们可以简单地部署为jar容器,而无需docker等。
我到目前为止所做的事情:
我尝试使用Liquibase,gradle plugin和liquibase-hibernate plugin。我遇到了很多错误,现在它不再崩溃,但仍然没有执行应做的事情。这将采用持久性实体并在它们与现有数据库之间创建差异。 在this guide,this guide和大量其他文献中进行了描述。 但是不确定是否可以使用,因为liquibase-hibernate插件wasn't updated for 2 years。
现在我的问题 是否可以使用Liquibase-Hibernate插件甚至liquibase完全替代我的描述? 如果没有,我该如何工作? XD
以下代码: build.gralde
apply plugin: "org.liquibase.gradle"
buildscript {
ext {
springBootVersion = '2.0.5.RELEASE'
gitProperiesPluginVersion = '1.5.2'
}
repositories {
jcenter()
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}",
"org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.5",
"org.ajoberstar:grgit:1.5.0",
"mysql:mysql-connector-java:8.0.12",
"gradle.plugin.com.gorylenko.gradle-git-properties:gradle-git-properties:${gitProperiesPluginVersion}",
"com.github.fgiannesini.libsass.gradle.plugin:libsass-gradle-plugin:+",
"org.liquibase:liquibase-gradle-plugin:2.0.1")
}
}
//retrieve spring datasource properties from application.properties
def applicationProps = new Properties()
//TODO change to production
applicationProps.load(new FileInputStream("src/main/resources/application-local-mysql.properties"))
liquibase {
activities {
main{
url applicationProps.getProperty('spring.datasource.url')
username applicationProps.getProperty('spring.datasource.username')
password applicationProps.getProperty('spring.datasource.password')
driver applicationProps.getProperty('spring.datasource.driver-class-name')
referenceUrl applicationProps.getProperty('liquibaseDiff.referenceUrl')
changeLogFile "$projectDir/src/main/resources/db/changelog/db.changelog_initial_${new Date().format('yyyyMMdd_HHmmss')}.xml"
logLevel 'debug'
}
}
}
dependencies {
//Liquibase for Databasemigration
compile group: 'org.liquibase', name: 'liquibase-core'
liquibaseRuntime group: 'org.liquibase', name: 'liquibase-core'
liquibaseRuntime "mysql:mysql-connector-java:6.0.6"
liquibaseRuntime "org.hibernate:hibernate-core"
liquibaseRuntime "org.liquibase.ext:liquibase-hibernate5:3.6"
liquibaseRuntime "org.springframework.boot:spring-boot-starter-data-jpa"
liquibaseRuntime files('src/main')
liquibaseRuntime group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
//liquibaseRuntime sourceSets.main.output
}
application-local-mysql-properties
#Datasource Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/negoisst4?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&zeroDateTimeBehavior=convertToNull&useSSL=false&nullNamePatternMatchesAll=true
spring.datasource.username=root
spring.datasource.password=
spring.datasource.platform=mysql
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
liquibaseDiff.referenceUrl=hibernate:spring:com.my.packages?dialect=org.hibernate.dialect.MySQL5Dialect&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
当我运行diffChangeLog时,它会创建一个只删除所有内容的日志。 这是我运行差异时的一些输出:
DEBUG 02.03.19 10:53: liquibase: Connected to root@localhost@jdbc:mysql://localhost:3306/negoisst4?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&zeroDateTimeBehavior=convertToNull&useSSL=false&nullNamePatternMatchesAll=true
DEBUG 02.03.19 10:53: liquibase: Setting auto commit to false from true
DEBUG 02.03.19 10:53: liquibase: Connected to null@hibernate:spring:de.hohenheim.wi1.negoisst.model?dialect=org.hibernate.dialect.MySQL5Dialect&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
DEBUG 02.03.19 10:53: liquibase: Not adjusting the auto commit mode; it is already false
INFO 02.03.19 10:53: liquibase-hibernate: Reading hibernate configuration hibernate:spring:de.hohenheim.wi1.negoisst.model?dialect=org.hibernate.dialect.MySQL5Dialect&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
INFO 02.03.19 10:53: liquibase-hibernate: Found package de.hohenheim.wi1.negoisst.model
10:53:26.953 [main] DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver - Resolved classpath location [META-INF/persistence.xml] to resources []
10:53:26.958 [main] DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver - Resolved classpath location [de/hohenheim/wi1/negoisst/model/] to resources []
10:53:26.958 [main] DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver - Resolved location pattern [classpath*:de/hohenheim/wi1/negoisst/model/**/*.class] to resources []
10:53:27.003 [main] DEBUG org.jboss.logging - Logging Provider: org.jboss.logging.Log4j2LoggerProvider
.
.
.
DEBUG 02.03.19 10:53: liquibase: Computed checksum for 1551520408913 as fc2a473c724b14fe86312dab07ee3cd2
DEBUG 02.03.19 10:53: liquibase: Executing QUERY database command: select const.CONSTRAINT_NAME, COLUMN_NAME from information_schema.table_constraints const join information_schema.key_column_usage col on const.constraint_schema=col.constraint_schema and const.table_name=col.table_name and const.constraint_name=col.constraint_name where const.constraint_schema='negoisst4' and const.table_name='trust_measurement_trust_statement_answers' and const.constraint_name='uk_awuterxuh4721br8p554yeykb'order by ordinal_position
DEBUG 02.03.19 10:53: liquibase: Executing QUERY database command: select const.CONSTRAINT_NAME, COLUMN_NAME from information_schema.table_constraints const join information_schema.key_column_usage col on const.constraint_schema=col.constraint_schema and const.table_name=col.table_name and const.constraint_name=col.constraint_name where const.constraint_schema='negoisst4' and const.table_name='treatment_participant_configuration' and const.constraint_name='uk_qnfjakthv8sfhkd7r2c79h58o'order by ordinal_position
DEBUG 02.03.19 10:53: liquibase: Executing QUERY database command: select const.CONSTRAINT_NAME, COLUMN_NAME from information_schema.table_constraints const join information_schema.key_column_usage col on const.constraint_schema=col.constraint_schema and const.table_name=col.table_name and const.constraint_name=col.constraint_name where const.constraint_schema='negoisst4' and const.table_name='ase_preference_measurement_next_identified_comparisons' and const.constraint_name='uk_rpdsg51d6v7mbswdcts5r70rq'order by ordinal_position
DEBUG 02.03.19 10:53: liquibase: Executing QUERY database command: select const.CONSTRAINT_NAME, COLUMN_NAME from information_schema.table_constraints const join information_schema.key_column_usage col on const.constraint_schema=col.constraint_schema and const.table_name=col.table_name and const.constraint_name=col.constraint_name where const.constraint_schema='negoisst4' and const.table_name='negoisst_user' and const.constraint_name='uk_aeokjpmeqqqid4u9m54qf23pf'order by ordinal_position
DEBUG 02.03.19 10:53: liquibase: Executing QUERY database command: select const.CONSTRAINT_NAME, COLUMN_NAME from information_schema.table_constraints const join information_schema.key_column_usage col on const.constraint_schema=col.constraint_schema and const.table_name=col.table_name and const.constraint_name=col.constraint_name where const.constraint_schema='negoisst4' and const.table_name='dispute_resolution_participants' and const.constraint_name='uk_jh90shxowuibpegitcpy50c1m'order by ordinal_position
DEBUG 02.03.19 10:53: liquibase: Executing QUERY database command: select const.CONSTRAINT_NAME, COLUMN_NAME from information_schema.table_constraints const join information_schema.key_column_usage col on const.constraint_schema=col.constraint_schema and const.table_name=col.table_name and const.constraint_name=col.constraint_name where const.constraint_schema='negoisst4' and const.table_name='ase_pm_issue_rated_values' and const.constraint_name='uk_30bexu74whbs1xtpnmbupkggi'order by ordinal_position
DEBUG 02.03.19 10:53: liquibase: Executing QUERY database command: select const.CONSTRAINT_NAME, COLUMN_NAME from information_schema.table_constraints const join information_schema.key_column_usage col on const.constraint_schema=col.constraint_schema and const.table_name=col.table_name and const.constraint_name=col.constraint_name where const.constraint_schema='negoisst4' and const.table_name='ase_preference_measurement_conducted_paired_comparisons' and const.constraint_name='uk_13onu5our6cipfuoisy0qq7su'order by ordinal_position
DEBUG 02.03.19 10:53: liquibase: Executing QUERY database command: select const.CONSTRAINT_NAME, COLUMN_NAME from information_schema.table_constraints const join information_schema.key_column_usage col on const.constraint_schema=col.constraint_schema and const.table_name=col.table_name and const.constraint_name=col.constraint_name where const.constraint_schema='negoisst4' and const.table_name='preference_model_associated_weighted_issues' and const.constraint_name='uk_d0nk6tg0ghjaho5u4g1r71cq'order by ordinal_position
.
.
.
Changed Primary Key(s): NONE
Missing Sequence(s): NONE
Unexpected Sequence(s): NONE
Changed Sequence(s): NONE
Missing Stored Procedure(s): NONE
Unexpected Stored Procedure(s): NONE
Changed Stored Procedure(s): NONE
Missing Table(s): NONE
Unexpected Table(s):
agenda
agenda_aud
consent_form
email_verification_token
event
event_reminder_date
experiment
hibernate_sequence
issue
issue_aud
issue_group
issue_group_aud
issue_group_issue_aud
issue_interval
issue_w_rated_values
message
.
.
.