Liquibase can be run from the command line by running:
The command line migrator works well when you want to do migrations on demand, but don’t have Ant or Maven available such as on servers.
The command line migrator also gives you more control over the process than the servlet listener, Ant,
or Maven do, allowing you to run maintenance commands like outputting SQL and listing/releasing database changelog locks.
Any values found after the command on the command line invocation will be considered a command parameter. The command line processor will
validate whether the command line parameters are allowed for the current command. If the current command does not allow command line
parameters or the parameter appears to be an incorrect format, then an error message of ‘unexpected command parameter’ will be logged
and the execution will terminate.
Generate a changelog from a database when adding Liquibase to a new project.
Conceptually, does a diff of the current database to an empty database, creating a changelog that has all the changes needed to update an empty database.
Generates Javadoc-like documentation based on current database and change log.
Maintenance Commands
Command
Description
changelogSync
Mark all changes as executed in the database.
changelogSyncSQL
Writes SQL to mark all changes as executed in the database to STDOUT.
clearCheckSums
Removes current checksums from database. On next update changesets that
have already been deployed will have their checksums recomputed, and changesets that have not been deployed
will be deployed.
dropAll
Drops all database objects owned by the user. DANGEROUS!
listLocks
Lists who currently has locks on the database changelog.
markNextChangeSetRan
Mark the next change set as executed in the database.
releaseLocks
Releases all locks on the database changelog.
status
Outputs count (list if --verbose) of unrun change sets.
Gathers the current database schema and displays that information to STDOUT. With options, can save
the schema in JSON format, and that JSON snapshot can serve as a comparison database.
tag <tag>
"Tags" the current database state for future rollback.
tagExists <tag>
Checks whether the given tag already exists.
validate
Checks the changelog for errors.
calculateCheckSum
executeSql
Parameters:
sql
Sql to execute
sqlFile
Sql file to execute
delimiter
Required Parameters for most commands
Option
Description
--changeLogFile=<path and filename>
The changelog file to use.
--username=<value>
Database username.
--password=<value>
Database password.
--url=<value>
Database JDBC URL.
--driver=<jdbc.driver.ClassName>
Database driver class name.
Optional Parameters
Option
Description
--classpath=<value>
Classpath containing migration files and JDBC Driver.
If you do not want to always specify options on the command line, you can create a properties file that contains default values. By default, Liquibase will look for a file called “liquibase.properties” in the current working directory, but you can specify an alternate location with the --defaultsFile flag. If you have specified an option in a properties file and specify the same option on the command line, the value on the command line will override the properties file value.
Examples
Standard Update Run
Run update pulling changelogs from a .WAR file
Run update pulling changelogs from an .EAR file
Don’t execute changesets, save SQL to /tmp/script.sql
List locks on the database change log lock table
Runs Liquibase using defaults from ./liquibase.properties
liquibase.properties
Export Data from Database
This will export the data from the targeted database and put it in a folder “data” in a file name specified with <insert file name>.
Update passing changelog parameters
Unicode
MySQL
Add url parameters useUnicode=true and characterEncoding=UTF-8 to set character encoding to utf8.
Since v5.1.3 Connector/J now auto-detects servers configured with character_set_server=utf8mb4 or treats the Java encoding utf-8 passed using characterEncoding=… as utf8mb4.