@@ -4,23 +4,70 @@ var path = require('path');
4
4
var util = require ( 'util' ) ;
5
5
var mkdirp = require ( 'mkdirp' ) ;
6
6
var optimist = require ( 'optimist' ) ;
7
- var index = require ( './connect' ) ;
8
- var Migration = require ( './lib/migration.js' ) ;
9
- var Seeder = require ( './lib/seeder.js' ) ;
10
- var Migrator = require ( './lib/migrator.js' ) ;
11
7
var log = require ( 'db-migrate-shared' ) . log ;
12
8
var pkginfo = require ( 'pkginfo' ) ( module , 'version' ) ; // jshint ignore:line
13
9
var dotenv = require ( 'dotenv' ) ;
14
- var Promise = require ( 'bluebird' ) ;
10
+ var Promise = Promise ;
15
11
16
- function dbmigrate ( isModule , options , callback ) {
12
+ function registerPluginLoader ( plugins ) {
13
+
14
+ return {
15
+
16
+ overwrite : function ( name ) {
17
+
18
+ if ( plugins [ name ] && plugins [ name ] . length ) {
19
+
20
+ var plugin = plugins [ name ] ;
21
+
22
+ if ( plugin . length !== 1 ) {
23
+ log . warn (
24
+ 'Attention, multiple overwrites registered for %s, we are ' +
25
+ 'only loading the first plugin %s!' ,
26
+ name ,
27
+ plugin . name
28
+ ) ;
29
+ }
30
+
31
+ plugin = plugin [ 0 ] ;
32
+ if ( typeof ( plugin . loadPlugin ) === 'function' )
33
+ plugin . loadPlugin ( ) ;
34
+
35
+ return plugin ;
36
+ }
37
+
38
+ return false ;
39
+ } ,
40
+
41
+ hook : function ( name ) {
42
+
43
+ if ( plugins [ name ] && plugins [ name ] . length ) {
44
+
45
+ var plugin = plugins [ name ] ;
46
+
47
+ plugin . map ( function ( plugin ) {
48
+
49
+ if ( typeof ( plugin . loadPlugin ) === 'function' )
50
+ plugin . loadPlugin ( ) ;
51
+ } ) ;
52
+
53
+ return plugin ;
54
+ }
55
+
56
+ return false ;
57
+ }
58
+ } ;
59
+ }
60
+
61
+ function dbmigrate ( plugins , isModule , options , callback ) {
17
62
18
63
this . internals = {
19
64
20
65
onComplete : onComplete
21
66
} ;
22
67
var internals = this . internals ;
23
68
69
+ this . internals . plugins = registerPluginLoader ( plugins ) ;
70
+
24
71
if ( typeof ( callback ) === 'function' )
25
72
this . internals . onComplete = callback ;
26
73
else if ( typeof ( options ) === 'function' )
@@ -60,8 +107,8 @@ function dbmigrate(isModule, options, callback) {
60
107
61
108
this . config = loadConfig ( require ( './lib/config.js' ) , this . internals ) ;
62
109
63
- index . exportInternals ( internals ) ;
64
-
110
+ //delayed loading of bluebird
111
+ Promise = require ( 'bluebird' ) ;
65
112
this . internals . migrationOptions = {
66
113
dbmigrate : this . internals . dbm ,
67
114
ignoreOnInit : this . internals . argv [ 'ignore-on-init' ] ,
@@ -420,7 +467,7 @@ function setDefaultArgv(internals, isModule) {
420
467
'force-exit' : false ,
421
468
'sql-file' : false ,
422
469
'non-transactional' : false ,
423
- config : internals . configFile || internals . cwd + '/ database.json',
470
+ config : internals . configFile || ' database.json',
424
471
'migrations-dir' : internals . cwd + '/migrations' ,
425
472
'vcseeder-dir' : internals . cwd + '/VCSeeder' ,
426
473
'staticseeder-dir' : internals . cwd + '/Seeder' ,
@@ -554,7 +601,7 @@ function loadConfig( config, internals ) {
554
601
} else if ( internals . configObject ) {
555
602
out = config . loadObject ( internals . configObject , currentEnv ) ;
556
603
} else {
557
- out = config . loadFile ( internals . argv . config , currentEnv ) ;
604
+ out = config . loadFile ( internals . argv . config , currentEnv , internals . plugins ) ;
558
605
}
559
606
if ( internals . verbose ) {
560
607
var current = out . getCurrent ( ) ;
@@ -588,6 +635,10 @@ function executeCreateMigration(internals, config, callback) {
588
635
}
589
636
590
637
createMigrationDir ( migrationsDir , function ( err ) {
638
+
639
+ var index = require ( './connect' ) ;
640
+ var Migration = require ( './lib/migration.js' ) ;
641
+
591
642
if ( err ) {
592
643
log . error ( 'Failed to create migration directory at ' , migrationsDir ,
593
644
err ) ;
@@ -670,6 +721,10 @@ function createSqlFiles(internals, config, callback) {
670
721
671
722
var sqlDir = migrationsDir + '/sqls' ;
672
723
createMigrationDir ( sqlDir , function ( err ) {
724
+
725
+ var index = require ( './connect' ) ;
726
+ var Migration = require ( './lib/migration.js' ) ;
727
+
673
728
if ( err ) {
674
729
log . error ( 'Failed to create migration directory at ' , sqlDir , err ) ;
675
730
@@ -727,6 +782,9 @@ function _assert(err, callback) {
727
782
728
783
function executeUp ( internals , config , callback ) {
729
784
785
+ var Migrator = require ( './lib/migrator.js' ) ;
786
+ var index = require ( './connect' ) ;
787
+
730
788
if ( ! internals . argv . count ) {
731
789
internals . argv . count = Number . MAX_VALUE ;
732
790
}
@@ -756,6 +814,9 @@ function executeUp(internals, config, callback) {
756
814
757
815
function executeDown ( internals , config , callback ) {
758
816
817
+ var Migrator = require ( './lib/migrator.js' ) ;
818
+ var index = require ( './connect' ) ;
819
+
759
820
if ( ! internals . argv . count ) {
760
821
log . info ( 'Defaulting to running 1 down migration.' ) ;
761
822
internals . argv . count = 1 ;
@@ -779,6 +840,8 @@ function executeDown(internals, config, callback) {
779
840
780
841
function executeDB ( internals , config , callback ) {
781
842
843
+ var index = require ( './connect' ) ;
844
+
782
845
if ( internals . argv . _ . length > 0 ) {
783
846
internals . argv . dbname = internals . argv . _ . shift ( ) . toString ( ) ;
784
847
} else {
@@ -828,6 +891,9 @@ function executeDB(internals, config, callback) {
828
891
829
892
function executeSeed ( internals , config , callback ) {
830
893
894
+ var index = require ( './connect' ) ;
895
+ var Seeder = require ( './lib/seeder.js' ) ;
896
+
831
897
if ( internals . argv . _ . length > 0 ) {
832
898
internals . argv . destination = internals . argv . _ . shift ( ) . toString ( ) ;
833
899
}
@@ -859,6 +925,9 @@ function executeSeed(internals, config, callback) {
859
925
860
926
function executeUndoSeed ( internals , config , callback ) {
861
927
928
+ var index = require ( './connect' ) ;
929
+ var Seeder = require ( './lib/seeder.js' ) ;
930
+
862
931
if ( ! internals . argv . count ) {
863
932
log . info ( 'Defaulting to running 1 down seed.' ) ;
864
933
internals . argv . count = 1 ;
0 commit comments