← All Exercises
Order Migrations and Detect Cycles
intermediatejavascript
Background
Migrations that run out of order leave a schema that matches neither version. A cycle in dependencies is never safe to resolve by retrying.
Problem
Topologically order migrations by their dependency declarations and report any cycle instead of emitting a half-applied plan.
Input
JSON object: { migrations: [{ id, dependsOn?: string[] }] }Output
JSON object: { order: string[], cyclic: boolean, cycle: string[] }Sample tests
Sample 1
Input: {"migrations":[{"id":"003_add_index","dependsOn":["002_add_table"]},{"id":"001_init"},{"id":"002_add_table","dependsOn":["001_init"]}]}Expected: {"order":["001_init","002_add_table","003_add_index"],"cyclic":false,"cycle":[]}Sample 2
Input: {"migrations":[{"id":"a","dependsOn":["b"]},{"id":"b","dependsOn":["a"]}]}Expected: {"order":[],"cyclic":true,"cycle":["a->b->a"]}Your solution
Learning objective
Produce a migration order that a deploy job can execute without retries.