mongo-migrate-ts icon indicating copy to clipboard operation
mongo-migrate-ts copied to clipboard

feat: sort migrations before applying

Open sf0rman opened this issue 9 months ago • 0 comments

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.

sf0rman avatar May 22 '24 13:05 sf0rman