mongo-migrate-ts
mongo-migrate-ts copied to clipboard
feat: sort migrations before applying
Problem Description
Migration files are not ordered by timestamp when running up
-command which can cause ordering issues.
For example Migration1: createCollection Migration2: change stuff within collection
If not ordered, migration-stuff attempts to run first and fails due to dependency on Migration1 (or collection existing)
ExecuteMigrationError: MongoServerError: ns does not exist: db.collection
Solution
When creating a migration file, we know that all class names end with a 13-digit timestamp (./lib/commands/new.ts:57
) regardless of using a custom template or the default one.
We can therefore extract timestamp from className (last 13 digits of className) and sort migrations based on this before running migrations.