Liquibase with H2 database

Once Liquibase is properly set up and liquibase --version, is giving the correct installed version. It's time to create Liquibase first project. 

Create an empty directory, open a command prompt or terminal run the command

liquibase init project

a prompt will appear.

Set up new liquibase.properties, flowfile, and sample changelog? Enter (Y)es with defaults, yes with (C)ustomization, or (N)o [Y]:

choose Y and Liquibase will create a project with default settings.

By default, Liquibase will create an example-changelog.sql and liquibase.properties file with a working example of the in-memory H2 database.

liquibase.properties - all configurations required to connect with the in-memory H2 database.
example-changelog.sql - SQL query to create Person and Company table in-memory H2 database.

Open a new terminal or command prompt in the same directory and run

liquibase init start-h2

it will start an in-memory H2 database with 2 empty schema dev schema as the primary database and integration schema for database migration.

The database does not persist data, stopping and restarting this process will reset it back to a blank database

the prompt will provide basic information about the in-memory H2 database

Connection Information:
  Dev database:
    JDBC URL: jdbc:h2:tcp://localhost:9090/mem:dev
    Username: dbuser
    Password: letmein
  Integration database:
    JDBC URL: jdbc:h2:tcp://localhost:9090/mem:integration
    Username: dbuser
    Password: letmein

Both the dev schema and integration schema of the in-memory H2 database can be logged, and the prompt will provide the URL

  • Dev URL: http://localhost:8080/frame.jsp?jsessionid=ed4d5be024351fed4e8dda18392eafc3
  • Integration URL: http://localhost:8080/frame.jsp?jsessionid=085663e68df9e3d5cf7b8b3d229c93c6

Keep the H2 command prompt open and in the first terminal and run liquibase update command.

Liquibase will read the liquibase.properties files and tries to connect with the in-memory H2 database, if the connection is successful then Liquibase will execute example-changelog.sql file and create Person and Company tables in the connected database, which can be verified by logging in with the credentials provided at the beginning.

The liquibase init start-h2 command takes some additional optional parameters as well for customization if required.

parameter command usage default value
bind-address liquibase init start-h2 --bind-address The network address to bind 127.0.0.1
db-port liquibase init start-h2 --db-port Port to run h2 database 9090
launch-browser liquibase init start-h2 --launch-browser Whether to open a browser to the database's web interface true
password liquibase init start-h2 --password Password to use for created h2 user. letmein
username liquibase init start-h2 --username Username to create in h2. dbuser
web-port liquibase init start-h2 --web-port Port to run h2's web interface on. 8080

 

follow us on