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
  3. Create directories for the PostgreSQL JDBC jar and changelog locally. For example: /home/app/jdbc and /home/app/changelog.
  4. Run docker network inspect bridge to find the IP address of your Titan PostgreSQL container. For example: 172.17.0.3.
  5. Create an alias to avoid lengthy arguments. Ensure to set update ${JAR}.
  6. 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} 
    --url="jdbc:postgresql://172.17.0.3:5432/postgres" 
    --changelog-file=/liquibase/changelog/changelog.xml 
    --username=postgres --password=postgres"
  7. Generate the changelog file for your Titan-provided PostgreSQL database by running:
  8. liquibase generate-changelog --changelog-file=changelog.xml

    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 --changelog-file=changelog.oracle.sql. 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.

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

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

  23. Commit your data changes to Titan and your updated changelog to source code control.
  24. 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