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
Important
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
Warning
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.
Warning
When making a change to an @Entity
, keep the number of changes as small as possible. Do not make changes to multiple entities at once. Instead, keep things simple to avoid failures during the execution of the DB migration script.
Warning
Whenever an @Entity
is modified, the underlying database schema in jpa-schema-export.sql
will also be modified. Note that the database schema will not be automatically re-generated when you compile the source with mvn compile
; Instead, you will have to run the webapp locally with mvn jetty:run
for this file to be re-generated.
elimu.ai - Free open-source learning software for out-of-school children ✨🚀
Website 🌐 • Wiki 📃 • Projects 👩🏽💻 • Milestones 🎯 • Community 👋🏽 • Support 💜