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.
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
.
Follow these steps:
- Lookup the current webapp version in
pom.xml
- Add a new file to
src/main/resources/db/migration
. E.g.2003004.sql
for version2.3.4
. - Add the SQL script that will be executed on the
TEST
/PROD
server.
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
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.