Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
255 lines (194 sloc) 11.9 KB
layout title
default
Command line | Liquibase Docs

Liquibase Command Line

Liquibase can be run from the command line by running:

{% highlight bat %} liquibase [options] [command] [command parameters] {% endhighlight %}

(Optionally, replace the liquibase command with java -jar <path-to-liquibase-jar>)

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 parmeter' will be logged and the execution will terminate.

The command line migrator also allows you to

If you run the command line migrator without any arguments, you will get a help message listing these available parameters:

Database Update Commands

CommandDescription
updateUpdates database to current version.
updateCount <value>Applies the next <value> change sets.
updateSQLWrites SQL to update database to current version to STDOUT.
updateCountSQL <value>Writes SQL to apply the next <value> change sets to STDOUT.

Database Rollback Commands

CommandDescription
rollback <tag>Rolls back the database to the state it was in when the tag was applied.
rollbackToDate <date/time>Rolls back the database to the state it was in at the given date/time.
rollbackCount <value>Rolls back the last <value> change sets.
rollbackSQL <tag>Writes SQL to roll back the database to the state it was in when the tag was applied to STDOUT.
rollbackToDateSQL <date/time>Writes SQL to roll back the database to the state it was in at the given date/time version to STDOUT.
rollbackCountSQL <value>Writes SQL to roll back the last <value> change sets to STDOUT.
futureRollbackSQLWrites SQL to roll back the database to the current state after the changes in the changeslog have been applied.
updateTestingRollbackUpdates the database, then rolls back changes before updating again.
generateChangeLoggenerateChangeLog of the database to standard out. v1.8 requires the dataDir parameter currently.

Diff Commands

CommandDescription
diff [diff parameters]Writes description of differences to standard out.
diffChangeLog [diff parameters]Writes Change Log XML to update the base database to the target database to standard out.

Documentation Commands

CommandDescription
dbDoc <outputDirectory>Generates Javadoc-like documentation based on current database and change log.

Maintenance Commands

CommandDescription
tag <tag>"Tags" the current database state for future rollback.
tagExists <tag>Checks whether the given tag is already existing.
statusOutputs count (list if --verbose) of unrun change sets.
validateChecks the changelog for errors.
changelogSyncMark all changes as executed in the database.
changelogSyncSQLWrites SQL to mark all changes as executed in the database to STDOUT.
markNextChangeSetRanMark the next change set as executed in the database.
listLocksLists who currently has locks on the database changelog.
releaseLocksReleases all locks on the database changelog.
dropAllDrops all database objects owned by the user. Note that functions, procedures and packages are not dropped (limitation in 1.8.1).
clearCheckSumsRemoves current checksums from database. On next run checksums will be recomputed.

Required Parameters

OptionDescription
--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

OptionDescription
--classpath=<value>Classpath containing migration files and JDBC Driver.
--contexts=<value>ChangeSet contexts to execute.
--defaultSchemaName=<schema>Specifies the default schema to use for managed database objects and for Liquibase control tables.
--databaseClass=<custom.DatabaseImpl>Specifies a custom Database implementation to use
--defaultsFile=</path/to/file>File containing default option values. (default: ./liquibase.properties)
--includeSystemClasspath=<true or false>Include the system classpath in the Liquibase classpath. (default: true)
--promptForNonLocalDatabase=<true or false>Prompt if non-localhost databases. (default: false)
--currentDateTimeFunction=<value>Overrides current date time function used in SQL. Useful for unsupported databases.
--logLevel=<level>Execution log level (debug, info, warning, severe, off).
--logFile=<filename>Rather than logging to console, send logging messages to a file.
--liquibaseProLicenseKey=<licenKey>A Liquibase Pro license key (base64 encoded string) unlocks additional features.
--helpOutput command line parameter help.
--exportDataDirDirectory where insert statement csv files will be kept (required by generateChangeLog command).
--propertyProviderClass=<properties.ClassName>custom Properties implementation to use

Required Diff Parameters

OptionDescription
--referenceUsername=<value>Base Database username.
--referencePassword=<value>Base Database password.
--referenceUrl=<value>Base Database URL.

Optional Diff Parameters

OptionDescription
--referenceDriver=<jdbc.driver.ClassName>Base Database driver class name.
--referenceDefaultSchemaName=<schema>Base Database default schema name.

Change Log Properties

OptionDescription
-D<property.name>=<property.value>Pass a name/value pair for substitution of ${} blocks in the change log(s).

Using a liquibase.properties file

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 Migrator Run

{% highlight bat %} java -jar liquibase.jar
--driver=oracle.jdbc.OracleDriver
--classpath=\path\to\classes:jdbcdriver.jar
--changeLogFile=com/example/db.changelog.xml
--url="jdbc:oracle:thin:@localhost:1521:oracle"
--username=scott
--password=tiger
update {% endhighlight %}

Run Migrator pulling changelogs from a .WAR file

{% highlight bat %} java -jar liquibase.jar
--driver=oracle.jdbc.OracleDriver
--classpath=website.war
--changeLogFile=com/example/db.changelog.xml
--url=jdbc:oracle:thin:@localhost:1521:oracle
--username=scott
--password=tiger
update {% endhighlight %}

Run Migrator pulling changelogs from an .EAR file

{% highlight bat %} java -jar liquibase.jar
--driver=oracle.jdbc.OracleDriver
--classpath=application.ear
--changeLogFile=com/example/db.changelog.xml
--url=jdbc:oracle:thin:@localhost:1521:oracle
--username=scott
--password=tiger {% endhighlight %}

Don't execute changesets, save SQL to /tmp/script.sql

{% highlight bat %} java -jar liquibase.jar
--driver=oracle.jdbc.OracleDriver
--classpath=jdbcdriver.jar
--url=jdbc:oracle:thin:@localhost:1521:oracle
--username=scott
--password=tiger
updateSQL > /tmp/script.sql {% endhighlight %}

List locks on the database change log

{% highlight bat %} java -jar liquibase.jar
--driver=oracle.jdbc.OracleDriver
--classpath=jdbcdriver.jar
--url=jdbc:oracle:thin:@localhost:1521:oracle
--username=scott
--password=tiger
listLocks {% endhighlight %}

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.

{% highlight bat %} --url="jdbc:mysql://localhost/dbname?useUnicode=true&characterEncoding=UTF-8 {% endhighlight %}

more information about MySQL Connector J Using Character Sets and Unicode

Runs Liquibase using defaults from ./liquibase.properties

{% highlight bat %} java -jar liquibase.jar update {% endhighlight %}

#liquibase.properties {% highlight properties %} driver: oracle.jdbc.OracleDriver classpath: jdbcdriver.jar url: jdbc:oracle:thin:@localhost:1521:oracle username: scott password: tiger {% endhighlight %}

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>.

{% highlight bat %} java -jar liquibase.jar --changeLogFile="./data/ " --diffTypes="data" generateChangeLog {% endhighlight %}

Update passing changelog parameters

{% highlight bat %} liquibase.bat update -Dengine=myisam {% endhighlight %}

You can’t perform that action at this time.