Skip to content

Latest commit

 

History

History
54 lines (43 loc) · 2.33 KB

db-migration.md

File metadata and controls

54 lines (43 loc) · 2.33 KB

DB migration

Development

In development, we use hibernate.ddl-auto=update in order to evolve the database while changing entities.

Production

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:

  1. Start a base postgresql database:
docker run --name ahoy-postgres -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -d postgres:10.16
  1. Exec in to the container and connect to the DB:
docker exec -it ahoy-postgres bin/bash
psql -U postgres
  1. Create the ahoy database:
create database ahoy;
  1. 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
  2. This will create the baseline/master database from the previous version.
  3. Stop the ahoy-server
  4. Build and install ahoy, from the root of the project, run:
mvn clean install -Dmaven.test.skip=true
  1. Navigate into the ahoy-server project and run a diff between the entities and the current database:
mvn liquibase:diff
  1. This will produce a diff changelog file named db.changelog-migrate-<version>.yaml in ahoy-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.

  1. 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.