Used umzug with a sequelize query instance.
Migration pipeline is:
When the api service boots in docker it runs api/bin/boot-app.sh
The boot script runs npm run seed up with runs the seed script in api/package.json
The package migrate script runs api/bin/seed.ts which loads the umzug seeder CLI.
The seeder CLI script loads the umzug seeder config from api/src/db/umzug.ts
The umzug seeder config loads the sequelize config from api/src/db/db-client.ts and passes the models package as the migration context.
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.
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.
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 asequelize
query instance. Migration pipeline is:api/bin/boot-app.sh
npm run seed up
with runs theseed
script inapi/package.json
api/bin/seed.ts
which loads theumzug
seeder CLI.api/src/db/umzug.ts
api/src/db/db-client.ts
and passes the models package as the migration context.api/src/db/seeds/development/
folder.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
dev down -v
dev up
. This will re-create your database and run all migrations and all seeds.http://localhost:3000/api/migrate/seed
dev seed up
.dev seed create -- --name test.ts
. The migration will appear in theapi/src/db/seeds/development
folder.