Liquibase 根据 table 名称前缀在数据库上生成更改日志

Liquibase generate change log on db based on table name prefix

我可以根据 table 名称前缀从数据库生成 Liquibase 变更日志吗?

示例: 如果我有一个数据库模式并且它有以下 tables:

abc
abcd
abcdef
xyz

我只想为从 "abc" 开始的 table 生成 ChangeLog。所以 tables

的更新日志

abc, abcd, abcdef

有人可以帮助我吗?

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

看看release notes

Liquibase 3.3.2 is officially released. It is primarily a bugfix release, but has one major new feature: object diffChangeLog/generateChangeLog object filtering. includeObjects/excludeObjects logic

You can now set an includeObjects or excludeObjects paramter on the command line or Ant. For maven, the parameteres are diffExcludeObjects and diffIncludeObjects. The format for these parameters are:

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.

NOTE: name comparison is case sensitive. If you want insensitive logic, use the (?i) regexp flag.

Example Filters:

“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

这适用于我 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\username\Desktop>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-连接器 here, find the generateChangeLog documentation here and more information on includeObjects here