@@ -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' ] ,
@@ -411,7 +458,7 @@ function setDefaultArgv(internals, isModule) {
411
458
'force-exit' : false ,
412
459
'sql-file' : false ,
413
460
'non-transactional' : false ,
414
- config : internals . configFile || internals . cwd + '/ database.json',
461
+ config : internals . configFile || ' database.json',
415
462
'migrations-dir' : internals . cwd + '/migrations' ,
416
463
'vcseeder-dir' : internals . cwd + '/VCSeeder' ,
417
464
'staticseeder-dir' : internals . cwd + '/Seeder' ,
@@ -545,7 +592,7 @@ function loadConfig( config, internals ) {
545
592
} else if ( internals . configObject ) {
546
593
out = config . loadObject ( internals . configObject , currentEnv ) ;
547
594
} else {
548
- out = config . loadFile ( internals . argv . config , currentEnv ) ;
595
+ out = config . loadFile ( internals . argv . config , currentEnv , internals . plugins ) ;
549
596
}
550
597
if ( internals . verbose ) {
551
598
var current = out . getCurrent ( ) ;
@@ -579,6 +626,10 @@ function executeCreateMigration(internals, config, callback) {
579
626
}
580
627
581
628
createMigrationDir ( migrationsDir , function ( err ) {
629
+
630
+ var index = require ( './connect' ) ;
631
+ var Migration = require ( './lib/migration.js' ) ;
632
+
582
633
if ( err ) {
583
634
log . error ( 'Failed to create migration directory at ' , migrationsDir ,
584
635
err ) ;
@@ -661,6 +712,10 @@ function createSqlFiles(internals, config, callback) {
661
712
662
713
var sqlDir = migrationsDir + '/sqls' ;
663
714
createMigrationDir ( sqlDir , function ( err ) {
715
+
716
+ var index = require ( './connect' ) ;
717
+ var Migration = require ( './lib/migration.js' ) ;
718
+
664
719
if ( err ) {
665
720
log . error ( 'Failed to create migration directory at ' , sqlDir , err ) ;
666
721
@@ -718,6 +773,9 @@ function _assert(err, callback) {
718
773
719
774
function executeUp ( internals , config , callback ) {
720
775
776
+ var Migrator = require ( './lib/migrator.js' ) ;
777
+ var index = require ( './connect' ) ;
778
+
721
779
if ( ! internals . argv . count ) {
722
780
internals . argv . count = Number . MAX_VALUE ;
723
781
}
@@ -747,6 +805,9 @@ function executeUp(internals, config, callback) {
747
805
748
806
function executeDown ( internals , config , callback ) {
749
807
808
+ var Migrator = require ( './lib/migrator.js' ) ;
809
+ var index = require ( './connect' ) ;
810
+
750
811
if ( ! internals . argv . count ) {
751
812
log . info ( 'Defaulting to running 1 down migration.' ) ;
752
813
internals . argv . count = 1 ;
@@ -770,6 +831,8 @@ function executeDown(internals, config, callback) {
770
831
771
832
function executeDB ( internals , config , callback ) {
772
833
834
+ var index = require ( './connect' ) ;
835
+
773
836
if ( internals . argv . _ . length > 0 ) {
774
837
internals . argv . dbname = internals . argv . _ . shift ( ) . toString ( ) ;
775
838
} else {
@@ -819,6 +882,9 @@ function executeDB(internals, config, callback) {
819
882
820
883
function executeSeed ( internals , config , callback ) {
821
884
885
+ var index = require ( './connect' ) ;
886
+ var Seeder = require ( './lib/seeder.js' ) ;
887
+
822
888
if ( internals . argv . _ . length > 0 ) {
823
889
internals . argv . destination = internals . argv . _ . shift ( ) . toString ( ) ;
824
890
}
@@ -850,6 +916,9 @@ function executeSeed(internals, config, callback) {
850
916
851
917
function executeUndoSeed ( internals , config , callback ) {
852
918
919
+ var index = require ( './connect' ) ;
920
+ var Seeder = require ( './lib/seeder.js' ) ;
921
+
853
922
if ( ! internals . argv . count ) {
854
923
log . info ( 'Defaulting to running 1 down seed.' ) ;
855
924
internals . argv . count = 1 ;
0 commit comments