Skip to content

Latest commit

 

History

History
30 lines (18 loc) · 1.74 KB

DB_MIGRATION.md

File metadata and controls

30 lines (18 loc) · 1.74 KB

DB Migration 🔀

By using the Java Persistence API (JPA), classed annotated by @Entity (in the model package) get mapped automatically to database tables/columns: src/main/resources/META-INF/jpa-persistence.xml

However, when deleting, modifying or renaming an entity class/attribute, we have to manually update the underlying database structure.

Migration Scripts

Every time the webapp completes initialization, the DbMigrationHelper checks if there are any database migration scripts at src/main/resources/db/migration matching the current webapp version. You can find the version in pom.xml.

How to Add a New Migration Script

Follow these steps:

  1. Lookup the current webapp version in pom.xml
  2. Add a new file to src/main/resources/db/migration. E.g. 2003004.sql for version 2.3.4.
  3. Add the SQL script that will be executed on the TEST/PROD server.

Sample

Let's assume that you want to delete the text property from the Word entity. It would look like this:

ALTER TABLE `Word` DROP COLUMN `text`;

For an example of a previous database migration script, see https://github.com/elimu-ai/webapp/commit/9908537ced3b6d64849f7f9967399921dba3d3fc

Caveats 😅

Note that DB migration performed automatically by the ORM provider (Hibernate), e.g. when adding a new property to an @Entity, is executed before our custom migration scripts.