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 H2.
To create a Liquibase project with H2 database on your Windows machine, begin with the following steps:
Create a new project folder and name it LiquibaseH2.
Place the jdbc H2 jar driver file in the LiquibaseH2 project directory.
In your LiquibaseH2 folder, Right-click then select New>Text Document to create an empty text file.
Rename the text file to myChangeLog.xml.
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. In this tutorial, you will manually add a single change. To add this change:
Open the myChangeLog.xml file and update the changelog file with the following code snippet:
In your LiquibaseH2 folder Right-click and select New>Text Document to create a new text file.
Rename the text file to liquibase.properties.
Edit the liquibase.properties file to add the following properties:
Note: In this properties file example, the driver jar file is “h2-1.4.200.jar” under the classpath: property. Please rename it to match the one that you have previously downloaded accordingly.
Adding a changeset to the changelog – Change Sets are uniquely identified by “author” and ”id” attributes. Liquibase attempts to execute each changeset in a transaction that is committed at the end.
In the myChangeLog.xml file line 9 to 20 add a new “department” create table change set as follows:
Note: This create table change set is XML format. The corresponding
SQL statement should look like the following:
Open the command prompt. Navigate to the LiquibaseH2 directory.
Run the following command:
“liquibase update”
From a database UI Tool, for example: “DBeaver” check your database changes.
You should see a new “department**” table added to the database. For example:
ID
NAME
ACTIVE
NULL
NULL
NULL
Also, you should 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.
For this example:
ID
AUTHOR
FILENAME
DATEEXECUTED
ORDEREXECUTED
EXECTYPE
MDSUM
…
1
bob
dbchangelog.xml
date&time
1
EXECUTED
checksumvalue
…
DATABASECHANGELOGLOCK – This table is used internally by Liquibase to manage access to the changelog table during deployment.