In development, we use hibernate.ddl-auto=update
in order to evolve the database while changing entities.
In production, we make use of Liquibase in order to facilitate the database migrations.
An initial changelog file to set up the base database exists in:
ahoy-server/src/main/resources/db/changelog/db.changelog-master.yaml
When making changes to entities, you will be required to create changelogs to facilitate upgrading the database with the new changes.
A useful way to generate this changelog is to DIFF the current entities with the previous state of the database. Here is an example on how to do this:
- Start a base postgresql database:
docker run --name ahoy-postgres -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -d postgres:10.16
- Exec in to the container and connect to the DB:
docker exec -it ahoy-postgres bin/bash
psql -U postgres
- Create the ahoy database:
create database ahoy;
- Start up ahoy-server pointing to this database; we've provided a profile to connect to a local postgresql instance by running with spring profile
postgres
- This will create the baseline/master database from the previous version.
- Stop the ahoy-server
- Build and install ahoy, from the root of the project, run:
mvn clean install -Dmaven.test.skip=true
- Navigate into the ahoy-server project and run a diff between the entities and the current database:
mvn liquibase:diff
- This will produce a diff changelog file named
db.changelog-migrate-<version>.yaml
inahoy-server/src/main/resources/db/changelog/
.
BEST PRACTICE: The changelog generated by diffChangeLog/generateChangeLog should be inspected for correctness and completeness before being deployed. Some database objects and their dependencies cannot be represented automatically, and they may need to be manually updated before being deployed.
- In order to affect this migration on startup of the server you'll need to include it in
db.changelog-master.yaml
. Add the following to the bottom of the master changelog:
- include:
file: db/changelog/db.changelog-<version>.yaml
These changelogs will then be packaged and will run on startup migrating any change sets that have not previously been applied to the database.