icefoganalytics / elcc-data-management

Data Management application for Education's Early Learning Child Centre
Apache License 2.0
0 stars 0 forks source link

Migrate Seeds Setup to Using Sequelize #39

Closed klondikemarlen closed 1 year ago

klondikemarlen commented 1 year ago

Fixes https://github.com/icefoganalytics/elcc-data-management/issues/22

Context

The current seeding setup is using Knex, switch to Sequelize See https://sequelize.org/docs/v6/other-topics/migrations/ See https://github.com/sequelize/umzug/issues/24#issuecomment-591313032

This will likely require building factories for the various Sequelize models. See https://github.com/icefoganalytics/sfa-client/issues/52 as an example.

Implementation

Used umzug with a sequelize query instance. Migration pipeline is:

  1. When the api service boots in docker it runs api/bin/boot-app.sh
  2. The boot script runs npm run seed up with runs the seed script in api/package.json
  3. The package migrate script runs api/bin/seed.ts which loads the umzug seeder CLI.
  4. The seeder CLI script loads the umzug seeder config from api/src/db/umzug.ts
  5. The umzug seeder config loads the sequelize config from api/src/db/db-client.ts and passes the models package as the migration context.
  6. Seeds are created/migrated from the folder corresponding to the NODE_ENV. i.e in development mode seeds are created and ran from the api/src/db/seeds/development/ folder.
  7. Seeds are not stored in the database and should be idempotent.

The seed migration template is located at api/src/db/templates/sample-seed.ts

Seeds are mostly just a bunch of Model.findOrCreate({ where: { }, defaults: { record to be inserted }}) commands that are run in order.

Examples

New: https://github.com/icefoganalytics/elcc-data-management/blob/4e1e9fa13611da07592fad4578f18f93c17d696b/api/src/db/seeds/development/2023.09.07T22.47.53.fill-centres-table.ts Old: https://github.com/icefoganalytics/elcc-data-management/blob/cc7365a922e894b3d74244dfadc1660c5c6ccc72/api/src/data/seeds/001_centres.ts

Testing Instructions

  1. Drop your database via dev down -v
  2. Boot the app via dev up. This will re-create your database and run all migrations and all seeds.
  3. Check that the app loads and that you can log in at http://localhost:8080.
  4. Check that you can list and run all seeds by going to http://localhost:3000/api/migrate/seed
  5. You can run seeds vai dev seed up.
  6. You can create a new seed via dev seed create -- --name test.ts. The migration will appear in the api/src/db/seeds/development folder.