Database Migrations¶
Introduction¶
In contrast to Dependency-Track v4 and earlier, Dependency-Track v5 manages database migrations with Liquibase.
The database schema is still owned by the API server though. It will execute migrations upon startup,
unless explicitly disabled via database.run.migrations.
Liquibase operates with the concept of changelogs.
For the sake of better visibility, Dependency-Track uses separate changelogs for each release version.
Individual changelogs are referenced by changelog-main.xml.
Stored procedures and custom SQL functions are treated differently: They are re-created whenever their
content changes. Their sources are located in the procedures directory.
Adding Migrations¶
- If it doesn't exist already, create a
changelog-vX.Y.Z.xmlfileX,Y, andZmust correspond to the current release version
- Ensure the
changelog-vX.Y.Z.xmlfile is referenced viaincludeinchangelog-main.xml - Add your changeset to
changelog-vX.Y.Z.xml
When adding a new changeset, consider the following guidelines:
- The changeset ID must follow the
vX.Y.Z-<NUM>format, where:X,Y, andZmatch the changelog's versionNUMis an incrementing number, starting at1for the firstchangesetof the release
- The
authormust correspond to your GitHub username - Prefer built-in change types
- Use the
sqlchange type if no fitting built-in exists - Use a custom change in edge cases, when additional computation is required
- Use the
- When using custom changes:
- Use the
org.dependencytrack.persistence.migration.changepackage - Changes must not depend on domain logic
- Use the
- You must not modify
changesets that were already committed tomain
Making Schema Changes Available to Hyades Services¶
Because the schema is owned by the API server, and the API server is also responsible for executing migrations, other services that access the database must replicate the current schema, in order to run tests against it.
Currently, this is achieved by:
- Having Liquibase generate the schema SQL based on the changelog
- Adding the
schema.sqlfile as resource to thecommons-persistencemodule - Having all services that require database access depend on
commons-persistence - Configuring Quarkus Dev Services to initialize new database containers with
schema.sql
The schema can be generated using the dbschema-generate.sh
script in the hyades-apiserver repository:
Note
- You may need to build the API server project once before running the script
- Because Liquibase requires database to run against, the script will launch a temporary PostgreSQL container
The output is written to target/liquibase/migrate.sql.