Using Liquibase with H2

H2 is a standard SQL database that you can use to simulate anything required to be done in your own database.

Note: For more information, see the H2 page.

The web-based console allows you to see all objects in your database in the left navigation and run SQL statements on the right side. The console includes the Refresh button, which you can use to reload the left-side object view if you make changes to your database with Liquibase.

Supported Versions

  • 1.4.200

Note: The supported version is the version that was tested with Liquibase Test Harness.

Prerequisites

Before using Liquibase and H2, ensure you have completed the following:

Driver Information

To use Liquibase and H2, you need to have the JDBC driver jar file. Liquibase includes the H2 in-memory database and the h2-version.jar file in the installation package.

You can find the H2 database in the ../examples: start-h2.bat folder along with the sample SQL and XML changelogs and the liquibase.properties file. You can find the h2-version.jar file in the liquibase/lib directory.

If you use Maven, put the H2 driver in a location that your Maven build can access. Configure the Maven pom.xml file to use the local copy of the driver jar file. For example:

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>

Testing Your Connection

For Liquibase and H2 to work, you need to:

  1. Start the H2 database by navigating to the examples folder in the CLI and running start-h2.
  2. Note: To stop the example H2 database, you can use ctrl-c.

    The start-h2 script starts a local H2 database on port 9090 and opens the database console on the same port in the browser.

    Note: The example H2 database does not store data and will reset to its starting state when the start-h2 process ends.

    • A developer database corresponds to what you may use as a local database.
    • An integration database corresponds to another database in your pipeline.

    Note: You can view the integration database with the link provided from the developer database browser. You can also see which database you are connected to by looking at the URL at the top of the object view.

  1. Check the status of the database by entering create table test_table (id int) in the text area and select Run. You will see TEST_TABLE appear in the object view.
  2. Use a sample XML or SQL changelog file, or create a new text file called changelog (.xml, .sql, .json, or .yaml).
  3. Add a changeset.
  4. Tip: If you already have a Liquibase Pro key and want to apply it to your project, add the following property to your liquibase.properties file: liquibaseProLicenseKey: <paste license key>. For more information about the liquibase.properties file, see Creating and configuring a liquibase.properties file.

  1. Run the status command to see whether the connection is successful. Specify the name of the changelog you created in place of changelog.xml. Also, to run Liquibase commands, you need to specify your username and password in the liquibase.properties file or on the command line. However, the username and password attributes are not required for connections and systems which use alternate means of authentication.
  2. liquibase --username=test --password=test --changeLogFile=changelog.xml status

    Note: Alternatively, you can add the changeLogFile attribute to your liquibase.properties file.

  1. Run your first update with the Liquibase update command, which will make changes to your database:
  2. liquibase --changeLogFile=changelog.xml update

After your first update, you will see a new table along with the DATABASECHANGELOG and DATABASECHANGELOGLOCK tables added to the database:

  • DATABASECHANGELOG table. This table keeps a record of all the changesets that were deployed. When you deploy, the changesets in the changelog are compared with the DATABASECHANGELOG tracking table, and only the new changesets that were not found in the DATABASECHANGELOG will be deployed.
  • DATABASECHANGELOGLOCK table. This table is used internally by Liquibase to manage access to the DATABASECHANGELOG table during deployment and ensure only one instance of Liquibase is updating the database at a time, whether that is creating, updating or deleting changes.

Supported Commands and Change Types

Change Type Supported Command Supported
addAutoIncrement Supported calculateCheckSum <id> Supported
addCheckConstraint Not Supported changelogSync Supported
addColumn Supported changelogSyncSQL Supported
addDefaultValue Supported changelogSyncToTag Supported
addForeignKeyConstraint Supported changelogSyncToTagSQL Supported
addLookupTable Supported clearCheckSums Supported
addNotNullConstraint Supported dbDoc Supported
addPrimaryKey Supported deactivateChangeLog Supported
addUniqueConstraint Supported diff Supported
alterSequence Supported diff JSON Supported
createFunction Supported diffChangeLog Supported
createIndex Supported dropAll Supported
createPackage Supported futureRollbackCountSQL <value> Supported
createPackageBody Supported future-rollback-from-tag-sql <tag> Supported
createProcedure Supported futureRollbackSQL Supported
createSequence Supported generateChangeLog Supported
createSynonym Not Supported help Supported
createTable Supported history Supported
createTrigger Supported listLocks Supported
createView Supported markNextChangeSetRan Supported
customChange Supported markNextChangeSetRanSQL Supported
delete Supported registerChangeLog Supported
disableCheckConstraint Not Supported releaseLocks Supported
disableTrigger Not Supported rollback <tag> Supported
dropAllForeignKeyConstraints Supported rollbackCount <value> Supported
dropCheckConstraint Not Supported rollbackCountSQL <value> Supported
dropColumn Supported rollbackOneChangeSet Supported
dropDefaultValue Supported rollbackOneChangeSetSQL Supported
dropForeignKeyConstraint Supported rollbackOneUpdate Supported
dropFunction Supported rollbackOneUpdateSQL Supported
dropIndex Supported rollbackSQL <tag> Supported
dropNotNullConstraint Supported rollbackToDate Supported
dropPackage Supported rollbackToDateSQL Supported
dropPackageBody Supported snapshot Supported
dropPrimaryKey Supported snapshotReference Supported
dropProcedure Supported status --verbose Supported
dropSequence Supported syncHub Supported
dropSynonym Not Supported tag <tag string> Supported
dropTable Supported tagExists <tag string> Supported
dropTrigger Not Supported unexpectedChangeSets Supported
dropUniqueConstraint Supported update Supported
dropView Supported updateSQL Supported
empty Supported updateCount <value> Supported
enableCheckConstraint Not Supported updateCountSQL <value> Supported
enableTrigger Not Supported updateTestingRollback Supported
executeCommand Supported updateToTag <tag> Supported
insert Supported updateToTagSQL <tag> Supported
loadData Supported validate Supported
loadUpdateData Supported    
markUnused Not Supported    
mergeColumns Supported    
modifyDataType Supported    
output Supported    
renameColumn Supported    
renameSequence Not Supported    
renameTable Supported    
renameTrigger Not Supported    
renameView Not Supported    
setColumnRemarks Supported    
setTableRemarks Supported    
sql Supported    
sqlFile Supported    
stop Supported    
tagDatabase Supported    
update Supported    

Related Links