Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Liquibase generate change log on db based on table name prefix

Can I generate Liquibase changelog from a DB based on table name prefix.

Example: If I have a DB schema and it has following tables:

abc
abcd
abcdef
xyz

I just want to generate ChangeLog for tables starting with "abc". So changelog for tables

abc, abcd, abcdef

Can someone help me if there's a way to do this?

like image 816
Avik Kesari Avatar asked Jan 29 '20 06:01

Avik Kesari


2 Answers

It's possible with maven or liquibase command line if you're using liquibase version > 3.3.2.

Take a look at the 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”

So try using excludeObjects or includeObjects parameters with generateChangeLog command

UPDATE

I've used liquibase command line, and this command does the trick (for mysql database):

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
like image 139
htshame Avatar answered Oct 29 '22 12:10

htshame


This works for me on 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.

You can download mysql-connector here, find the generateChangeLog documentation here and more information on includeObjects here.

like image 38
jumping_monkey Avatar answered Oct 29 '22 13:10

jumping_monkey