3

请我需要在 spring(基于 JAVA 的配置)中解决上述问题,而不是在 spring boot 中。我在网上找到的大多数教程都是基于 spring boot,但我的应用程序没有在 spring boot 上运行,只是 spring mvc。我的类路径中已经有必要的依赖项,即 liquibase-core-3.5.3 和 liquibase-hibernate5-3.6 jars。

我还创建了 liquibase 属性文件(也在类路径中),如下所示

driver=com.mysql.jdbc.Driver
changeLogFile=classpath:db-changelog.xml
url=jdbc:mysql://localhost:3306/mydatabase
username=myusername
password=mypassword
referenceUrl=hibernate:spring:com.mypackage.entity?dialect=org.hibernate.dialect.MySQL5InnoDBDialect
diffChangeLogFile=com/mypackage/dbchangelog/diff-changelog.xml

现在的问题是我如何执行它来生成一个差异更改日志文件?

4

2 回答 2

2

您将不得不使用 liquibase 插件为 maven 或 gradle 执行此操作,具体取决于您用于构建的内容。

Maven 插件是关于如何运行 liquibase 命令的非常好的文档。

如果您使用的是 gradle,则 liquibase 插件在调用diffChangeLog.

根据这个答案,创建一个名为liquibase.gradle. 我已经根据它编辑了一些属性,hibernate5因为它不再支持ImprovedNamingStrategy

def diffLog = "$projectDir/src/main/resources/db/changelog/db.changelog.yaml"

configurations {
    liquibase
}

dependencies {
    liquibase 'org.liquibase.ext:liquibase-hibernate5:3.6'
}

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

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

    args "--changeLogFile=" + diffLog
    args "--referenceUrl=hibernate:spring:com.mypackage.entity?hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&dialect=org.hibernate.dialect.MySQL5Dialect"
    args "--username=<username>"
    args "--password=<password>"
    args "--url=jdbc:mysql://<db_host_and_port>/<db_name>"
    args "--referenceDriver=liquibase.ext.hibernate.database.connection.HibernateDriver"
    args "diffChangeLog"
}

添加apply from: 'liquibase.gradle'你的build.gradle.

现在,要生成 diffChangeLog,请调用gradle liquibaseDiffChangelog.

请注意,该示例使用 Spring Boot 的类之​​一作为 Hibernate Physical Naming Strategy。您可以通过参考此答案来创建自己的实现。

于 2017-07-20T04:44:57.070 回答
0

这是spring bean配置:

@Bean
public SpringLiquibase liquibase() {
    SpringLiquibase liquibase = new SpringLiquibase();
    liquibase.setChangeLog("classpath:db-changelog.xml");
    liquibase.setDataSource(dataSource());
    return liquibase;
}

和来自 [liquibase - 网站][1] 的 xml 配置

<bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">
      <property name="dataSource" ref="myDataSource" />
      <property name="changeLog" value="classpath:db-changelog.xml" />

      <!--
      contexts specifies the runtime contexts to use.
      -->
      <property name="contexts" value="test, production" />  

来源:SpringLiquibase - 这是代码执行更新,它是默认行为。

protected void performUpdate(Liquibase liquibase) throws LiquibaseException{
    if (tag != null) {
        liquibase.update(tag, new Contexts(getContexts()), new LabelExpression(getLabels()));
    } else {
        liquibase.update(new Contexts(getContexts()), new LabelExpression(getLabels()));
    }
}

Liquibase 类有方法

public DiffResult diff(Database referenceDatabase,
                       Database targetDatabase, 
                       CompareControl compareControl)

因此,您可以创建自定义 SpringLiquibase 并使用 diff 而不是 update

public class MyDiffSpringLiquibase extends SpringLiquibase {

    @Override
    protected void performUpdate(Liquibase liquibase) throws LiquibaseException {
    Database referenceDatabase = new MySQLDatabase();
    referenceDatabase.setConnection();

    Database targetDatabase = new MySQLDatabase();
    targetDatabase.setConnection();

    CatalogAndSchema catalogAndSchemaReference = new CatalogAndSchema();
    CatalogAndSchema catalogAndSchemacomparison = new CatalogAndSchema();

    Set<Class<? extends DatabaseObject>> finalCompareTypes = null;
    Class<? extends DatabaseObject>[] snapshotTypes = new Class[]{Table.class ,View.class......};
    if (snapshotTypes != null && snapshotTypes.length > 0) {
        finalCompareTypes = new HashSet<Class<? extends DatabaseObject>>(Arrays.asList(snapshotTypes));
    }

    CompareControl compareControl = new CompareControl(new CompareControl.SchemaComparison[]{new CompareControl.SchemaComparison(catalogAndSchemaReference, catalogAndSchemacomparison)}, finalCompareTypes);
    liquibase.diff(referenceDatabase, targetDatabase, compareControl);
    }
}

并将其注册为bean

@Bean
public SpringLiquibase liquibase() {
    SpringLiquibase liquibase = new MyDiffSpringLiquibase ();
    liquibase.setChangeLog("classpath:db-changelog.xml");
    liquibase.setDataSource(dataSource());
    return liquibase;
}
于 2017-07-01T16:41:39.867 回答