6.0 KiB
How to update the database schema
These instructions explain how to update the schemas for the following databases used by Oodikone:
sis-dbuser-dbkone-db
The steps to add, remove, or modify columns are mostly the same for each database, but the file paths and container names differ. Always double-check that you have the correct function name, column name, data type, and other relevant details in your migration file.
Note
If you need to change the
sis-importer-dbschema, see the instructions in the sis-importer repo.
General guidelines
File names
- Name new migration files using the format
YYYYMMDD_XX_description.js. - The
XXpart is a migration number (starting from00each day). - Example:
20250122_00_add_column_to_table.js.
Model vs. column names
- Use camelCase in your model definitions.
- Use snake_case in the actual database columns.
- If a model has
underscored: true, Sequelize automatically converts camelCase to snake_case in all queries. Otherwise, specify the column name explicitly.
Important
The column name in the database (snake_case) must be used in the migration file, not the camelCase name from your model.
Verifying the migration locally
-
Migrations are run automatically whenever the server restarts.
-
After creating a migration, ensure the relevant service is running:
- For
sis-db:npm run both:realornpm run updater:real - For
user-db/kone-db:npm run both:realornpm run oodikone:real
- For
-
Connect to the database container and check:
docker exec -it <container-name> psql -U postgres <database-name>-realReplace
<container-name>and<database-name>with the actual values (e.g.,sis-db,user-db, orkone-db). -
In the psql prompt, verify your migration:
SELECT * FROM migrations;Also confirm that the new (or updated) column exists in the appropriate table and has the correct type.
Reverting a migration locally
If you need to revert a migration locally, follow these steps:
- Locate the
migrator.up()function in the code:- For
user-dbandkone-db:services/backend/src/database/connection.ts - For
sis-db:updater/sis-updater-worker/src/db/connection.js
- For
- Change
migrator.up()tomigrator.down()and save the file. The latest migration will be reverted automatically when the server reloads. - Important: After the migration is reverted, change
migrator.down()back tomigrator.up()to ensure future migrations run correctly. - For more instructions, see the umzug documentation.
Deploying changes to production
- Push changes to the
masterbranch and wait for the workflow to finish. - For
sis-dbmigrations (in the updater):- You do not need to create a new release. Once tests pass, changes to the updater go directly to production.
- For
user-dbandkone-dbmigrations (in the backend):- A new release must be made before the changes go to production.
- After deployment, check logs (for example, in OpenShift) to confirm the migration ran successfully.
- Optionally, use the
db-clitool to inspect the production database. Seehow_to_db_cli.md(in GitLab) for instructions.
sis-db
Note
These instructions assume that needed data is already in the
sis-importer-db. If not, you must add the data there first. See the sis-importer repo for more details.
-
Update the updater model:
- Go to
updater/sis-updater-worker/src/db/models/and choose the model you want to change. - Add or modify the column (with its type and any other necessary fields).
- Check the correct data type in the importer.
- Use camelCase in the model, snake_case in the database.
- Go to
-
Update the backend model:
- Go to
services/backend/src/models/. - The models are in different formats (JavaScript in the updater, TypeScript in the backend), but follow existing examples.
- Go to
-
Create a migration file:
- Go to
updater/sis-updater-worker/src/db/migrations/. - Name your file using the format described above (e.g.
20250122_00_add_column_to_table.js). - Remember: The column name in the database (snake_case) must be used in the migration file, not the camelCase name from your model.
- Go to
-
Run and verify the migration:
- Ensure that the updater is running (
npm run both:realornpm run updater:real). - Use the steps in General guidelines to verify.
- Ensure that the updater is running (
-
Push to master and deploy:
- Your migration will go to production automatically after tests pass.
- Check logs in production (OpenShift) or use
db-clito confirm success.
user-db and kone-db
-
Update the model:
- Go to
services/backend/src/models/kone(forkone-db) orservices/backend/src/models/user.ts(the only model for user-db) - Add or modify the column, type, and any other needed fields. Follow existing models for structure.
- Go to
-
Create a migration file:
- Go to
services/backend/src/database/. - Use
migrations_kone(forkone-db) ormigrations_user(foruser-db). - Name the file using the same
YYYYMMDD_XX_description.jsformat. - Remember: The column name in the database (snake_case) must be used in the migration file, not the camelCase name from your model.
- Go to
-
Run and verify the migration:
- Make sure Oodikone is running (
npm run both:realornpm run oodikone:real). - Follow the same verification steps as in General Guidelines.
- Make sure Oodikone is running (
-
Push to master and release:
- Wait for the CI workflow to pass.
- Create a new release; then check the production logs (in OpenShift) to confirm the migrationor use
db-cli.