seppevs / migrate-mongo

A database migration tool for MongoDB in Node
MIT License
931 stars 166 forks source link

Order of migrations is not guaranteed #260

Closed DavidBruant closed 4 years ago

DavidBruant commented 4 years ago

Describe the bug migrate-mongo up does not guarantee that the various migration files will be read "in order"

This would lead to migrations happening out-of-order and, in the worst case lead to data loss, etc.

To Reproduce I haven't seen this bug in practice yet, so i cannot provide steps to reproduce,\ ...but i really really hope i won't see it, because it'd be nasty :-p

At least, this can only happen if there are at least 2 updates to apply by migrate-mongo up

Expected behavior migrate-mongo up should guarantee the migration order

Details migrate-mongo up reads the files in the order provided by status

https://github.com/seppevs/migrate-mongo/blob/f5e4c2fd71f2b294e59479b6f436a1c559cbd13d/lib/actions/up.js#L6-L12

status returns the files in the order provided by migrationsDir.getFileNames()

https://github.com/seppevs/migrate-mongo/blob/f5e4c2fd71f2b294e59479b6f436a1c559cbd13d/lib/actions/status.js#L2-L8

migrationsDir.getFileNames() return the file list as provided by fs.readdir

https://github.com/seppevs/migrate-mongo/blob/f5e4c2fd71f2b294e59479b6f436a1c559cbd13d/lib/env/migrationsDir.js#L80-L84

fs.readdir is "Asynchronous readdir(3)" (Linux)

and readdir (3) says:

The order in which filenames are read by successive calls to readdir() depends on the filesystem implementation; it is unlikely that the names will be sorted in any fashion.

seppevs commented 4 years ago

Thank you for reporting and providing a PR. The fix is in migrate-mongo@8.1.3.

DavidBruant commented 4 years ago

My pleasure for the report! and i'm grateful to @daveboulard for creating the PR!

daveboulard commented 4 years ago

My pleasure as well ! @DavidBruant you did most of the work actually so, thank you ;)