Using Liquibase and Docker with Titan

Titan solves the problem of database delivery and speeds up development workflows. It’s as simple as cloning an existing database locally, making changes, and saving those changes for others to use. If you use Titan, you can run your database in a docker container on your laptop or in Kubernetes, versioning the underlying data.

To get the structural changes such as DDL for new tables and columns to your later environments, you must evolve your database structure so you do not lose production data. By using Liquibase and Titan, you can accelerate your software delivery to test, production, and all stages in between, decrease the time setting up databases for development (Titan), and store your database changes in source code control (Liquibase).

To use Liquibase and Docker with Titan, follow these steps:

  1. Install Docker and Titan by referring to Installation and Configuration. Here is an example of installing Titan and running the data found in the hello-world postgres repository:
  2. titan install
    titan clone s3://titan-data-demo/hello-world/postgres hello-world
    titan ls
  1. Create directories for the PostgreSQL JDBC jar and changelog locally. For example: /home/app/jdbc and /home/app/changelog.
  2. Run docker network inspect bridge to find the IP address of your Titan PostgreSQL container. For example:
  3. Create an alias to avoid lengthy arguments. Ensure to set update ${JAR}.
  4. alias liquibase="docker run -v /home/app/jdbc:/liquibase/jdbc -v 
    /home/app/changelog:/liquibase/changelog liquibase/liquibase 
    --driver=org.postgresql.Driver --classpath=/liquibase/jdbc/${JAR} 
    --username=postgres --password=postgres"
  1. Generate the changelog file for your Titan-provided PostgreSQL database by running:
  2. liquibase --changelog-file=changelog.xml generate-changelog

    The command will create a changelog.xml file that is the baseline of your database. You can find it on your local system in the changelog folder.

    Note: If you want to create an SQL changelog file, add your database type name when specifying the changelog file: liquibase generate-changelog. Replace .oracle.sql with your database type. Liquibase also supports JSON and YAML formats. When in doubt about your database type name, check Supported Databases.

  1. Run the following command to synchronize the changelog file with a schema.
  2. liquibase changelog-sync
  1. Examine the DATABASECHANGELOG table, which is created by Liquibase:
  2. psql postgres://postgres:postgres@localhost/postgres -t -c 'SELECT * FROM DATABASECHANGELOG;'
  1. Commit the updated database to the Titan server and check in the changelog.xml file into the same source code repository as your application.
  2. titan commit -m "baselined with Liquibase" hello-world
  1. Add a new changeset to the changelog file:
  2. <changeSet author="liquibase_docker" id="1">
    	<createTable tableName="newtable">
    		<column name="colname" type="VARCHAR(255)"/>
  1. Save your file and run the following command to make sure you have updated the file correctly:
  2. liquibase validate
  1. Check the SQL that will be run on the server:
  2. liquibase update-sql
  1. Deploy your new table to the database:
  2. liquibase update

    Note: You can verify that the table was created by running
    psql postgres://postgres:postgres@localhost/postgres -t -c ‘\d newtable;’.

  1. Commit your data changes to Titan and your updated changelog to source code control.
  2. titan commit -m "Created table newtable with Liquibase." hello-world

Now, you can use Liquibase to update database schema change in non-development environments.

Related Links