1
1
// Migrator
2
2
// -------
3
- import fs from 'fs' ;
4
- import path from 'path' ;
5
- import mkdirp from 'mkdirp' ;
6
3
import Promise from 'bluebird' ;
7
4
import {
8
5
assign ,
@@ -14,7 +11,6 @@ import {
14
11
isEmpty ,
15
12
isUndefined ,
16
13
max ,
17
- template ,
18
14
} from 'lodash' ;
19
15
import inherits from 'inherits' ;
20
16
import {
@@ -26,6 +22,7 @@ import {
26
22
import { getSchemaBuilder } from './table-creator' ;
27
23
import * as migrationListResolver from './migration-list-resolver' ;
28
24
import FsMigrations , { DEFAULT_LOAD_EXTENSIONS } from './sources/fs-migrations' ;
25
+ import MigrationGenerator from './MigrationGenerator' ;
29
26
30
27
function LockError ( msg ) {
31
28
this . name = 'MigrationLocked' ;
@@ -51,6 +48,7 @@ export default class Migrator {
51
48
constructor ( knex ) {
52
49
this . knex = knex ;
53
50
this . config = getMergedConfig ( knex . client . config . migrations ) ;
51
+ this . generator = new MigrationGenerator ( knex . client . config . migrations ) ;
54
52
55
53
this . _activeMigration = {
56
54
fileName : null ,
@@ -145,30 +143,7 @@ export default class Migrator {
145
143
// Creates a new migration, with a given name.
146
144
make ( name , config ) {
147
145
this . config = getMergedConfig ( config , this . config ) ;
148
-
149
- if ( ! name ) {
150
- return Promise . reject (
151
- new Error ( 'A name must be specified for the generated migration' )
152
- ) ;
153
- }
154
-
155
- return this . _ensureFolder ( config )
156
- . then ( ( val ) => this . _generateStubTemplate ( val ) )
157
- . then ( ( val ) => this . _writeNewMigration ( name , val ) ) ;
158
- }
159
-
160
- // Ensures a folder for the migrations exist, dependent on the migration
161
- // config settings.
162
- _ensureFolder ( ) {
163
- const dirs = this . _absoluteConfigDirs ( ) ;
164
-
165
- const promises = dirs . map ( ( dir ) => {
166
- return Promise . promisify ( fs . stat , { context : fs } ) ( dir ) . catch ( ( ) =>
167
- Promise . promisify ( mkdirp ) ( dir )
168
- ) ;
169
- } ) ;
170
-
171
- return Promise . all ( promises ) ;
146
+ return this . generator . make ( name , this . config ) ;
172
147
}
173
148
174
149
_isLocked ( trx ) {
@@ -305,33 +280,6 @@ export default class Migrator {
305
280
return migration ;
306
281
}
307
282
308
- // Generates the stub template for the current migration, returning a compiled
309
- // template.
310
- _generateStubTemplate ( ) {
311
- const stubPath =
312
- this . config . stub ||
313
- path . join ( __dirname , 'stub' , this . config . extension + '.stub' ) ;
314
- return Promise . promisify ( fs . readFile , { context : fs } ) ( stubPath ) . then (
315
- ( stub ) => template ( stub . toString ( ) , { variable : 'd' } )
316
- ) ;
317
- }
318
-
319
- // Write a new migration to disk, using the config and generated filename,
320
- // passing any `variables` given in the config to the template.
321
- _writeNewMigration ( name , tmpl ) {
322
- const { config } = this ;
323
- const dirs = this . _absoluteConfigDirs ( ) ;
324
- const dir = dirs . slice ( - 1 ) [ 0 ] ; // Get last specified directory
325
-
326
- if ( name [ 0 ] === '-' ) name = name . slice ( 1 ) ;
327
- const filename = yyyymmddhhmmss ( ) + '_' + name + '.' + config . extension ;
328
-
329
- return Promise . promisify ( fs . writeFile , { context : fs } ) (
330
- path . join ( dir , filename ) ,
331
- tmpl ( config . variables || { } )
332
- ) . return ( path . join ( dir , filename ) ) ;
333
- }
334
-
335
283
// Get the last batch of migrations, by name, ordered by insert id in reverse
336
284
// order.
337
285
_getLastBatch ( [ allMigrations ] ) {
@@ -433,16 +381,6 @@ export default class Migrator {
433
381
) ;
434
382
} ) ;
435
383
}
436
-
437
- /** Returns */
438
- _absoluteConfigDirs ( ) {
439
- const directories = Array . isArray ( this . config . directory )
440
- ? this . config . directory
441
- : [ this . config . directory ] ;
442
- return directories . map ( ( directory ) => {
443
- return path . resolve ( process . cwd ( ) , directory ) ;
444
- } ) ;
445
- }
446
384
}
447
385
448
386
export function getMergedConfig ( config , currentConfig ) {
@@ -510,23 +448,3 @@ function warnPromise(knex, value, name, fn) {
510
448
}
511
449
return value ;
512
450
}
513
-
514
- // Ensure that we have 2 places for each of the date segments.
515
- function padDate ( segment ) {
516
- segment = segment . toString ( ) ;
517
- return segment [ 1 ] ? segment : `0${ segment } ` ;
518
- }
519
-
520
- // Get a date object in the correct format, without requiring a full out library
521
- // like "moment.js".
522
- function yyyymmddhhmmss ( ) {
523
- const d = new Date ( ) ;
524
- return (
525
- d . getFullYear ( ) . toString ( ) +
526
- padDate ( d . getMonth ( ) + 1 ) +
527
- padDate ( d . getDate ( ) ) +
528
- padDate ( d . getHours ( ) ) +
529
- padDate ( d . getMinutes ( ) ) +
530
- padDate ( d . getSeconds ( ) )
531
- ) ;
532
- }
0 commit comments