H2 Database Tutorial for Windows
The purpose of this document is to guide you through the process of creating a new Liquibase project with H2 on a Windows machine.
In this tutorial, you will generate an example project and follow the instructions to apply and learn concepts associated with creating new Liquibase projects with an H2 database.
- If you have not installed the latest version of Liquibase, navigate to https://download.liquibase.org/download to install the software application.
- Ensure the
liquibase.batfile’s path is set to a location in the
- Navigate to http://www.h2database.com/html/download.html and download the jdbc driver jar file for H2 database.
To create a Liquibase project with an H2 database on your Windows machine:
- Create a new project folder and name it
- Place the jdbc H2 jar driver file in the
- In your
LiquibaseH2folder, Right-click then select New > Text Document to create an empty text file.
- Rename the text file to
Note: changelog files contain a sequence of changesets, each of which make small changes to the structure of your database. Instead of creating an empty changelog file in step 2, you can also use an existing database to generate a changelog. For more information see the changelog topic.
Step One: Update your changelog
In this tutorial, you will need to modify your changelog by adding a template.
To add this changelog template:
- Open the
- Update the changelog file with the following code snippet, then save your file:
Step Two: Create a
Next we will create a
liquibase.properties file. To create the file:
- In your
LiquibaseH2folder, Right-click and select New > Text Document to create a new text file.
- Rename the text file to liquibase.properties.
- Edit theliquibase.properties file to add the following properties:
Note: In this liquibase.properties file example, the driver jar file is
h2-1.4.200.jarunder the classpath: property. Please rename it to match the one that you have previously downloaded accordingly.
Step Three: Add your changesets
Now we will manually add a changeset to the changelog file. Remember, when adding changesets, they are uniquely identified by author and id attributes. Liquibase attempts to execute each changeset in a transaction that is committed at the end.
To add additional changesets:
- In the
myChangeLog.xmlfile line 9 to 20 add a new department create table changeset as follows:
Note: This create table changeset is XML format. The corresponding SQL statement should look like the following:
- Open the command prompt. Navigate to the Liquibase H2 directory.
- Run the following command:
From a database UI Tool, for example: DBeaver check your database changes.
You should see a new “department” table added to the database.
You should also see two more tables:
- DATABASECHANGELOG tracking table – This table keeps a record of all the changesets that were deployed. This way, next time when you deploy again, the changesets in the changelog will be compared with the DATABASECHANGELOG tracking table and only the new changesets that were not found in the DATABASECHANGELOG will be deployed. You will notice that a new row was created in that table with the changeset information we have just deployed.
- DATABASECHANGELOGLOCK – This table is used internally by Liquibase to manage access to the changelog table during deployment.