File tree Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change 2
2
DB_PORT =
3
3
DB_USER =
4
4
DB_PASSWORD =
5
- DB_DATABASE =
5
+ DB_DATABASE =
6
+
7
+
8
+ # Change to 'prod' to prevent not loaded migrations with lower timestamp to be run after loaded migrations with higher timestamp
9
+ MODE = dev
Original file line number Diff line number Diff line change @@ -3,6 +3,9 @@ import * as path from 'path';
3
3
import { promisify } from 'util' ;
4
4
import { QueryFunction } from 'mysql' ;
5
5
6
+ require ( 'dotenv' ) . config ( ) ;
7
+
8
+
6
9
/**
7
10
* Database connection interface
8
11
*/
@@ -207,6 +210,17 @@ export class Migration {
207
210
const notLoadedScripts = files . filter ( ( file ) => ! loadedScripts . find ( ( loadedScripts ) => loadedScripts . fileName === file ) ) ;
208
211
209
212
const sortedNotLoadedScripts = notLoadedScripts . sort ( this . sortFiles ) ;
213
+
214
+ if ( loadedScripts . length && sortedNotLoadedScripts . length ) {
215
+ const lastLoadedScript = loadedScripts [ loadedScripts . length - 1 ] ;
216
+ // Get timestamp from filename of both scripts
217
+ const lastLoadedScriptTimestamp = lastLoadedScript . fileName . match ( / ^ ( \d * ) - / ) [ 1 ] ;
218
+ const firstNotLoadedScriptTimestamp = sortedNotLoadedScripts [ 0 ] . match ( / ^ ( \d * ) - / ) [ 1 ] ;
219
+ if ( process . env . MODE === 'prod' && firstNotLoadedScriptTimestamp < lastLoadedScriptTimestamp ) {
220
+ throw new Error ( 'Check your migration scripts! You are trying to load a script with a lower timestamp than the last loaded script!' ) ;
221
+ }
222
+ }
223
+
210
224
const fileArr = [ ...loadedScripts . map ( ( script ) => script . fileName ) , ...sortedNotLoadedScripts ] ;
211
225
212
226
this . writeLog ( `Found migration scripts: ${ fileArr . join ( ', ' ) } ` ) ;
You can’t perform that action at this time.
0 commit comments