【问题标题】:Liquibase generate change log on db based on table name prefixLiquibase 根据表名前缀在 db 上生成更改日志
【发布时间】:2020-05-14 14:50:03
【问题描述】:

我可以根据表名前缀从数据库生成 Liquibase 更改日志吗?

示例: 如果我有一个数据库架构并且它有以下表格:

abc
abcd
abcdef
xyz

我只想为以“abc”开头的表生成 ChangeLog。所以表格的更新日志

ABC, A B C D, abcdef

如果有办法,有人可以帮助我吗?

【问题讨论】:

    标签: java database-migration maven-plugin liquibase


    【解决方案1】:

    如果您使用的是 liquibase 版本 > 3.3.2,则可以使用 maven 或 liquibase 命令行。

    看看release notes

    Liquibase 3.3.2 正式发布。它主要是一个错误修复 发布,但有一个主要的新特性:对象 diffChangeLog/generateChangeLog 对象过滤。 includeObjects/excludeObjects 逻辑

    您现在可以在 命令行或 Ant。对于 maven,参数是 diffExcludeObjects 和 diffIncludeObjects。这些参数的格式是:

    An object name (actually a regexp) will match any object whose name matches the regexp.
    A type:name syntax that matches the regexp name for objects of the given type
    If you want multiple expressions, comma separate them
    The type:name logic will be applied to the tables containing columns, indexes, etc.
    

    注意:名称比较区分大小写。如果你想要不敏感 逻辑,使用 (?i) 正则表达式标志。

    过滤器示例:

    “table_name” will match a table called “table_name” but not “other_table” or “TABLE_NAME”
    “(i?)table_name” will match a table called “table_name” and “TABLE_NAME”
    “table_name” will match all columns in the table table_name
    “table:table_name” will match a table called table_name but not a column named table_name
    “table:table_name, column:*._lock” will match a table called table_name and all columns that end with “_lock”
    

    所以尝试在generateChangeLog 命令中使用excludeObjectsincludeObjects 参数

    更新

    我使用了 liquibase 命令行,这个命令可以解决问题(对于 mysql 数据库):

    liquibase 
    --changeLogFile=change.xml 
    --username=username 
    --password=password 
    --driver=com.mysql.cj.jdbc.Driver 
    --url=jdbc:mysql://localhost:3306/mydatabase
    --classpath=mysql-connector-java-8.0.18.jar 
    --includeObjects="table:abc.*" 
    generateChangeLog
    

    【讨论】:

    • 我正在运行以下命令 mvn liquibase:generateChangeLog -DincludeObjects="abc*" 但仍然面临问题
    • 你必须写 -DincludeObjects="table:abc*" 如stackoverflow.com/questions/38706429/…中所见
    • @AvikKesari 我已经更新了答案。您需要使用--includeObjects="table:abc.*"
    【解决方案2】:

    这对我有用 Windows 10:

    liquibase.properties:

    changeLogFile=dbchangelog.xml
    classpath=C:/Program\ Files/liquibase/lib/mysql-connector-java-8.0.20.jar
    driver=com.mysql.cj.jdbc.Driver
    url=jdbc:mysql://localhost:3306/liquibase?serverTimezone=UTC
    username=root
    password=password
    schemas=liquibase
    includeSchema=true
    includeTablespace=true
    includeObjects=table:persons
    

    C:\Users\用户名\桌面>liquibase generateChangeLog

    Liquibase Community 4.0.0 by Datical
    Starting Liquibase at 11:34:35 (version 4.0.0 #19 built at 2020-07-13 19:45+0000)
    Liquibase command 'generateChangeLog' was executed successfully.
    

    您可以下载 mysql-connector here,查找 generateChangeLog 文档 here 以及有关 includeObjects here 的更多信息。

    【讨论】:

      猜你喜欢
      • 2020-02-16
      • 2013-09-29
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2015-05-19
      • 1970-01-01
      • 2021-01-23
      相关资源
      最近更新 更多