11import logger from 'lib/logger' ;
22import migration from 'cli/migrateMongo/models/migration' ;
3- import { isBoolean , isString , map } from 'lodash' ;
3+ import { isBoolean , isString , map , find } from 'lodash' ;
44import v2Migrations from 'cli/commands/v2-migrations' ;
55import { OrderedMap , List } from 'immutable' ;
6+ import { map as bmap } from 'bluebird' ;
67import migrate from './migrate' ;
78
89const getOutstandingMigrations = async ( { down, up, migrations } ) => {
9- const lastMigration = await migration . findOne ( { } ) . sort ( { updatedAt : - 1 } ) ;
10+ const lastMigration = await migration . findOne ( { } ) . sort ( { order : - 1 , updatedAt : - 1 } ) ;
1011
1112 const lastKey = lastMigration ? lastMigration . key : null ;
1213 if ( down ) {
@@ -80,8 +81,35 @@ const getOutstandingMigrations = async ({ down, up, migrations }) => {
8081 return outstandingMigrations ;
8182} ;
8283
84+ export const fixDbMigrationOrder = async ( { migrations } ) => {
85+ const dbMigrations = await migration . find ( { } ) . sort ( { order : 1 } ) ;
86+ let toSave = [ ] ;
87+
88+ let inc = 1 ;
89+ for ( const migrationItem of migrations . keySeq ( ) . toJS ( ) ) {
90+ // eslint-disable-next-line no-loop-func
91+ const dbMigration = find (
92+ dbMigrations ,
93+ dbMigrationItem => migrationItem === dbMigrationItem . key
94+ ) ;
95+ if ( ! dbMigration ) {
96+ break ;
97+ }
98+
99+ dbMigration . order = inc ;
100+ toSave = [ dbMigration , ...toSave ] ;
101+
102+ inc += 1 ;
103+ }
104+
105+ await bmap ( toSave , ( toSav ) => {
106+ const out = toSav . save ( ) ;
107+ return out ;
108+ } ) ;
109+ } ;
110+
83111const checkRunMigrations = async ( { migrations } ) => {
84- const dbMigrations = new List ( await migration . find ( { } ) ) ;
112+ const dbMigrations = new List ( await migration . find ( { } ) . sort ( { order : 1 } ) ) ;
85113 const actualMigrations = new List ( migrations ) ;
86114 const allMigrations = dbMigrations . zip ( actualMigrations ) ;
87115
@@ -138,15 +166,15 @@ const displayInfo = async ({ info, migrations }) => {
138166 const verbose = info === 'v' || info === 'verbose' ;
139167
140168 if ( ! verbose ) {
141- const lastRunMigration = await migration . findOne ( ) . sort ( { updatedAt : - 1 } ) ;
169+ const lastRunMigration = await migration . findOne ( ) . sort ( { order : - 1 , updatedAt : - 1 } ) ;
142170
143171 if ( ! lastRunMigration ) {
144172 logger . info ( 'No migrations have been run' ) ;
145173 } else {
146174 logger . info ( 'Last run migration: ' , lastRunMigration . key ) ;
147175 }
148176 } else {
149- const runMigrations = await migration . find ( ) . sort ( { updatedAt : 1 } ) ;
177+ const runMigrations = await migration . find ( ) . sort ( { order : 1 , updatedAt : 1 } ) ;
150178
151179 const output = map ( runMigrations , item => item . key ) ;
152180
@@ -170,10 +198,12 @@ const displayInfo = async ({ info, migrations }) => {
170198 }
171199} ;
172200
173- export default async function ( { down, up, info, migrations = v2Migrations , dontExit = false } , next = null ) {
201+ export default async function ( { down, up, info, migrations = v2Migrations , dontExit = false , fixOrder = false } , next = null ) {
174202 try {
175203 if ( info ) {
176204 await displayInfo ( { info, migrations } ) ;
205+ } else if ( fixOrder ) {
206+ await fixDbMigrationOrder ( { migrations } ) ;
177207 } else {
178208 await doMigrations ( { down, up, migrations } ) ;
179209 }
0 commit comments