无法从 liquibase gradle 插件生成差异

Unable to generate difference from liquibase gradle plugin

我正在尝试在现有的 SpringBoot 项目中使用 MYSQL 数据库实施 liquibase。我希望能够生成指定实体更改时差异的变更集。

我做了什么:

我在 build.gradle 文件中添加了 liquibase 依赖项和 gradle liquibase 插件。更改域后,我 运行 gradle generateChangeLog。命令执行成功但没有任何反应。

我在某处读到此 gradle 插件仅适用于内存 h2 数据库?真的吗?如果是,那么我应该使用什么替代方法来自动生成变更日志。

我找不到基于 SpringBoot gradle 的工作示例,该示例使用 MYSQL 并通过自动更改生成功能实现了 liquibase。如果有人可以提供,那就太好了。

参考文献:

https://github.com/stevesaliman/liquibase-workshop

https://github.com/liquibase/liquibase-gradle-plugin

解决方案是编写一个 gradle 调用 liquibase diffChangeLog

的任务

在项目根目录下创建一个liquibase.gradle文件,添加liquibase-hibernate扩展,写一个gradle调用liquibase diffChangeLog命令的任务

configurations {
  liquibase
}

dependencies {
  liquibase group: 'org.liquibase.ext', name: 'liquibase-hibernate4', version: 3.5
}

//loading properties file.
Properties liquibaseProps = new Properties()
liquibaseProps.load(new FileInputStream("src/main/resources/liquibase-task.properties"))

Properties applicationProps = new Properties()
applicationProps.load(new FileInputStream("src/main/resources/application.properties"))

task liquibaseDiffChangelog(type: JavaExec) {
  group = "liquibase"


  classpath sourceSets.main.runtimeClasspath
  classpath configurations.liquibase
  main = "liquibase.integration.commandline.Main"

  args "--changeLogFile=" + liquibaseProps.getProperty('liquibase.changelog.path')+ buildTimestamp() +"_changelog.xml"
  args "--referenceUrl=hibernate:spring:" + liquibaseProps.getProperty('liquibase.domain.package') + "?dialect=" + applicationProps.getProperty('spring.jpa.properties.hibernate.dialect')
  args "--username=" + applicationProps.getProperty('spring.datasource.username')
  args "--password=" + applicationProps.getProperty('spring.datasource.password')
  args "--url=" + applicationProps.getProperty('spring.datasource.url')
  args "--driver=com.mysql.jdbc.Driver"
  args "diffChangeLog"
}

def buildTimestamp() {
  def date = new Date()
  def formattedDate = date.format('yyyyMMddHHmmss')
  return formattedDate
}

注意:我使用属性文件将参数传递给 liquibase 命令,您可以直接添加值,但这不是一个好的做法。

接下来,您需要从项目的 build.gradle 文件中应用 liquibase.gradle 文件。并添加 liquibase 依赖项

apply from: 'liquibase.gradle'
//code omitted
dependencies {
    compile (group: 'org.liquibase', name: 'liquibase-core', version: "3.4.2")
}

完成此步骤后,liquibase 将完全设置。

You can now use gradle liquibaseDiffChangeLog to generate changelogs.

plugins {
    id 'org.liquibase.gradle' version '2.0.1'
}

Gradle liquibase 插件在我添加构建资源后为我工作,类 到其运行时依赖项,如下所示:

dependencies {
    liquibaseRuntime 'org.liquibase:liquibase-core:3.5.3'
    liquibaseRuntime 'org.liquibase:liquibase-groovy-dsl:2.0.1'
    liquibaseRuntime 'mysql:mysql-connector-java:5.1.34'
    liquibaseRuntime 'org.liquibase.ext:liquibase-hibernate4:3.6'
    liquibaseRuntime 'javax.persistence:javax.persistence-api:2.2'
    liquibaseRuntime files('build/classes/java/main')
    liquibaseRuntime files('build/resources/main')

    // Your other dependencies...
}

我把它的主要activity定义为:

liquibase {
  activities {
    main {
      changeLogFile 'build/liquibase_change_log.xml'
      url 'jdbc:mysql://localhost/YOURDATABASE'
      username 'YOURUSER'
      password 'YOURPASSWORD'
      driver 'com.mysql.jdbc.Driver'
      referenceUrl 'hibernate:classic:/hibernate.cfg.xml'      
    }
  }
}

请注意,我只是使用经典的 Hibernate 配置来定义源架构。

liquibase 的 hibernate4 集成在 运行 liquibase 的 JVM 类路径中查找 /hibernate.cfg.xml。它还需要找到您的架构 类.

我还添加了这个:

diffChangeLog.dependsOn build

通过以下设置,它可以与 liquibase-hibernateliquibase-gradle 扩展一起使用:

plugins {
    id 'org.liquibase.gradle' version '2.0.1'
}

dependencies {
    implementation 'org.liquibase:liquibase-core:3.8.0'

    liquibaseRuntime 'org.liquibase.ext:liquibase-hibernate5:3.8'
    liquibaseRuntime sourceSets.main.runtimeClasspath
    liquibaseRuntime sourceSets.main.output
}

def props = new Properties()
file("src/main/resources/liquibase.properties").withInputStream { props.load(it) }

diff.dependsOn assemble
diffChangeLog.dependsOn assemble

liquibase {
    activities {
        main {
            changeLogFile props.getProperty("liquibase.changelog.main")
            referenceUrl props.getProperty("liquibase.changelog.referenceUrl")
            url props.getProperty("spring.datasource.url")
            username props.getProperty("spring.datasource.username")
            password props.getProperty("spring.datasource.password")
            referenceDriver "liquibase.ext.hibernate.database.connection.HibernateDriver"
        }
    }
}

这将在指定的更新日志文件中生成更新日志。您可以首先使用 gradle generateChangelog、运行 您的应用程序生成初始更改日志,以将这些更改应用到数据库,然后在您的实体模型 运行 每次更改后执行 gradle diffChangelog 任务在 chanlog 文件中获取这些更改。然后应在 运行 再次执行 diffChangeLog 任务之前将这些应用到数据库,以防止更改日志中出现重复操作。

为此,您需要 liquibase.properties 中的以下属性:

liquibase.changelog.main=src/main/resources/db/changelog/db.changelog-master.xml
liquibase.changelog.classpath=classpath:db/changelog/db.changelog-master.xml
liquibase.changelog.referenceUrl=hibernate:spring:<MODEL_PACKAGE>?dialect=org.hibernate.dialect.MySQL5Dialect

重要提示:务必将 <MODEL_PACKAGE> 替换为您的休眠模型所在的包。

liquibase {
activities {
    main {
        changeLogFile 'src/main/resources/liquibase-changeLog-db1.xml'
        classpath "$projectDir/src/main/resources"
        url props["database.db1.url"]
        referenceUrl props["database.db3.url"]
        username props["database.db1.user"]
        password props["database.db1.password"]
        referenceUsername props["database.db3.user"]
        referencePassword props["database.db3.password"]

    }
    secondary {
        changeLogFile 'src/main/resources/liquibase-changeLog-db2.xml'
        classpath "$projectDir/src/main/resources"
        url props["database.db2.url"]
        username props["database.db2.user"]
        password props["database.db2.password"]
    }
    tertiary {
        changeLogFile 'src/main/resources/liquibase-changeLog-db1.xml'
        classpath "$projectDir/src/main/resources"
        url props["database.db3.url"]
        username props["database.db3.user"]
        password props["database.db3.password"]
    }
    runList =  project.ext.runList
}

}

在这里,当你 运行 命令 ./gradlew diff prunList=main 时,它将获取主数据库并将其与参考数据库进行比较,并将以以下格式在控制台中打印出差异。您可能需要在我的 application.properties 文件中的 applications.properties file.here 中添加数据库 URL 和密码 我已经定义了 3 dbs.in 我的第一个和第三个它们几乎相同,除了次要列添加。在下面的差异中,它已经确定了缺失的列。

Compared Schemas: liquibase_new -> liquibase2
Product Name: EQUAL
Product Version: EQUAL
Missing Catalog(s): NONE
Unexpected Catalog(s): NONE
Changed Catalog(s): NONE
Missing Column(s):
     liquibase_new.business_center.new
Unexpected Column(s):
     liquibase2.business_center.new_column
Changed Column(s): NONE
Missing Foreign Key(s): NONE
Unexpected Foreign Key(s): NONE
Changed Foreign Key(s): NONE
Missing Index(s): NONE
Unexpected Index(s): NONE
Changed Index(s): NONE
Missing Primary Key(s): NONE
Unexpected Primary Key(s): NONE
Changed Primary Key(s): NONE
Missing Sequence(s): NONE
Unexpected Sequence(s): NONE
Changed Sequence(s): NONE
Missing Table(s): NONE
Unexpected Table(s): NONE
Changed Table(s): NONE
Missing Unique Constraint(s): NONE
Unexpected Unique Constraint(s): NONE
Changed Unique Constraint(s): NONE
Missing View(s): NONE
Unexpected View(s): NONE
Changed View(s): NONE

注意。要应用 Hazim 对 Spring boot 2 referenceUrl 的出色回答,参数应该有 "&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy"