Database Migration Workflow
Liquibase manages schema changes in the application server. Follow this workflow to keep the database, ERD docs, and generated SQLAlchemy models in sync.
Quick start
-
Modify JPA entities with the desired changes.
-
Generate a draft changelog:
npm run db:draft-changelog -
Review
changelog_new.xmlcarefully—check for destructive operations. -
Rename and move the file to
server/application-server/src/main/resources/db/changelog/{id}_changelog.xml. -
Regenerate docs and models:
npm run db:generate-erd-docs
npm run db:generate-models:intelligence-service -
Commit the migration, documentation (
docs/contributor/erd/schema.mmd), and updated models.
Generated migrations can drop or rename columns unexpectedly. Double-check each changeset before committing.
Validation checklist
- Replace the autogenerated
user (generated)author with your GitHub username. - Prefer
renameColumnover drop/add sequences when renaming fields to avoid data loss. - Ensure new sequences start at
1unless you have a data migration plan. - Confirm destructive statements (
dropTable,dropColumn) are intentional and safe. - Run
npm run db:generate-erd-docsand inspect the diff before committing.
Safe rename example
<!-- ❌ Avoid drop + add when renaming -->
<dropColumn tableName="user" columnName="first_name"/>
<addColumn tableName="user">
<column name="firstName" type="VARCHAR(255)"/>
</addColumn>
<!-- ✅ Use renameColumn to preserve data -->
<renameColumn tableName="user" oldColumnName="first_name" newColumnName="firstName"/>
CI validation
Three GitHub Actions guard against drift:
- Database schema validation – Applies migrations to a fresh DB and compares it with current entities. Failure produces a new
changelog_new.xml. - Database documentation validation – Generates an ERD from migrations and compares it with the committed diagram.
- Intelligence service model validation – Regenerates SQLAlchemy models and compares them with
models_gen.py.
Entity change tips
- Prefer
renameColumnover drop/add pairs to avoid data loss. - Replace
user (generated)in Liquibase files with your GitHub username. - Sequences should start at
1unless explicitly required otherwise.
Example
@Entity
public class User {
@NonNull
private String email; // Newly added field
}
Generates:
<changeSet author="yourusername" id="1749286026779-1">
<addColumn tableName="user">
<column name="email" type="VARCHAR(255)">
<constraints nullable="false"/>
</column>
</addColumn>
</changeSet>
CI flow
Intelligence service sync
Every migration may affect the intelligence service. Regenerate models with:
npm run db:generate-models:intelligence-service
The command applies migrations, runs sqlacodegen, and updates server/intelligence-service/app/db/models_gen.py. Treat the generated file as disposable—never edit it manually.
Resources
- Liquibase documentation for change set syntax and best practices.
- Spring Data JPA reference for repository naming rules.