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:
- 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: - Create directories for the PostgreSQL JDBC jar and changelog locally. For example:
/home/app/jdbc
and/home/app/changelog
. - Run docker network inspect bridge to find the IP address of your Titan PostgreSQL container. For example:
172.17.0.3
. - Create an alias to avoid lengthy arguments. Ensure to set
update ${JAR}
. - Generate the changelog file for your Titan-provided PostgreSQL database by running:
- Run the following command to synchronize the changelog file with a schema.
- Examine the DATABASECHANGELOG table, which is created by Liquibase:
- Commit the updated database to the Titan server and check in the
changelog.xml
file into the same source code repository as your application. - Add a new changeset to the changelog file:
- Save your file and run the following command to make sure you have updated the file correctly:
- Check the SQL that will be run on the server:
- Deploy your new table to the database:
- Commit your data changes to Titan and your updated changelog to source code control.
titan install
titan clone s3://titan-data-demo/hello-world/postgres hello-world
titan ls
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"
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.
liquibase changelog-sync
psql postgres://postgres:postgres@localhost/postgres -t -c 'SELECT * FROM DATABASECHANGELOG;'
titan commit -m "baselined with Liquibase" hello-world
<changeSet author="liquibase_docker" id="1">
<createTable tableName="newtable">
<column name="colname" type="VARCHAR(255)"/>
</createTable>
</changeSet>
liquibase validate
liquibase update-sql
liquibase update
Note: You can verify that the table was created by runningpsql postgres://postgres:postgres@localhost/postgres -t -c ‘\d newtable;’
.
titan commit -m "Created table newtable with Liquibase." hello-world
Now, you can use Liquibase to update database schema change in non-development environments.