Skip to content

Commit 2d9037f

Browse files
committed
refactor(globals): remove several globals, this might break things
There are several things that are exposed as global. For example Migrator, Seeder, Class, or the async and dbm globals. Most probably finally removing async and dbm which have been marked deprecated, will end up for some people in a breaking change. But we need to remove all those globals, to not interfere with projects using db-migrate. DB-Migrate is not just a cli module anymore though. In this step we now also finally removed internals completely from the API, and needed to adjust the onComplete callback to actually do this. This change could also lead to a breaking change and should thus be communicated All in all this is a big clean up and also two not necessary functions were removed.
1 parent 4949f2e commit 2d9037f

File tree

5 files changed

+20
-75
lines changed

5 files changed

+20
-75
lines changed

api.js

+15-24
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,22 @@ var pkginfo = require('pkginfo')(module, 'version'); // jshint ignore:line
1313
var dotenv = require('dotenv');
1414
var Promise = require('bluebird');
1515

16-
17-
//global declaration for detection like it's done in umigrate //deprecated
18-
global.dbm = require('./'); //deprecated
19-
global.async = require('async'); //deprecated
20-
21-
var internals = {};
22-
2316
function dbmigrate(isModule, options, callback) {
2417

2518
this.internals = {
2619

27-
onComplete: internals.onComplete
20+
onComplete: onComplete
2821
};
29-
internals = this.internals;
22+
var internals = this.internals;
3023

3124
if (typeof(callback) === 'function')
3225
this.internals.onComplete = callback;
3326
else if (typeof(options) === 'function')
3427
this.internals.onComplete = options;
3528

36-
this.dataType = dbm.dataType;
37-
this.version = dbm.version;
29+
this.internals.dbm = require('./');
30+
this.dataType = this.internals.dbm.dataType;
31+
this.version = this.internals.dbm.version;
3832
dotenv.load({
3933
silent: true
4034
});
@@ -67,8 +61,7 @@ function dbmigrate(isModule, options, callback) {
6761
this.config = loadConfig( require('./lib/config.js'), this.internals );
6862

6963
index.exportInternals(internals);
70-
this.internals.dbm = dbm;
71-
global.dbm = dbm; //deprecated
64+
7265
this.internals.migrationOptions = {
7366
dbmigrate: this.internals.dbm,
7467
ignoreOnInit: this.internals.argv['ignore-on-init'],
@@ -112,7 +105,7 @@ dbmigrate.prototype = {
112105
return true;
113106
},
114107

115-
_internals: internals,
108+
_internals: this.internals,
116109

117110
/**
118111
* Add a configuration option to dbmigrate.
@@ -503,7 +496,7 @@ function setDefaultArgv(internals, isModule) {
503496
.argv;
504497

505498
if (internals.argv.version) {
506-
console.log(dbm.version);
499+
console.log(internals.dbm.version);
507500
process.exit(0);
508501
}
509502

@@ -652,7 +645,7 @@ function shouldIgnoreOnInitFiles( internals, config ) {
652645
'ignore-on-init'];
653646
}
654647

655-
function shouldCreateCoffeeFile( intenrals, config ) {
648+
function shouldCreateCoffeeFile( internals, config ) {
656649
return internals.argv['coffee-file'] || config['coffee-file'];
657650
}
658651

@@ -747,7 +740,7 @@ function executeUp(internals, config, callback) {
747740
log.verbose('migration table created');
748741

749742
migrator.up(internals.argv, internals.onComplete.bind(this,
750-
migrator, callback));
743+
migrator, internals, callback));
751744
});
752745
});
753746
}
@@ -770,7 +763,7 @@ function executeDown(internals, config, callback) {
770763
migrator.driver.createMigrationsTable(function(err) {
771764
assert.ifError(err);
772765
migrator.down(internals.argv, internals.onComplete.bind(this,
773-
migrator, callback));
766+
migrator, internals, callback));
774767
});
775768
});
776769
}
@@ -842,13 +835,13 @@ function executeSeed(internals, config, callback) {
842835
if (internals.mode === 'static') {
843836

844837
seeder.seed(internals.argv, internals.onComplete.bind(this, seeder,
845-
callback));
838+
internals, callback));
846839
} else {
847840
seeder.createSeedsTable(function(err) {
848841
if (_assert(err, callback)) {
849842

850843
seeder.seed(internals.argv, internals.onComplete.bind(this,
851-
seeder, callback));
844+
seeder, internals, callback));
852845
}
853846
});
854847
}
@@ -884,16 +877,14 @@ function executeUndoSeed(internals, config, callback) {
884877
if (_assert(err, callback)) {
885878

886879
seeder.down(internals.argv, internals.onComplete.bind(this,
887-
seeder, callback));
880+
seeder, internals, callback));
888881
}
889882
});
890883
}
891884
});
892885
}
893886

894-
internals.onComplete = onComplete;
895-
896-
function onComplete(migrator, callback, originalErr) {
887+
function onComplete(migrator, internals, callback, originalErr) {
897888

898889
if (typeof(callback) !== 'function') {
899890
originalErr = originalErr || callback;

index.js

-46
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,6 @@ var pkginfo = require('pkginfo')(module, 'version'); // jshint ignore:line
22

33
exports.dataType = require('db-migrate-shared').dataType;
44

5-
6-
/**
7-
* Removes a module from the cache
8-
*/
9-
uncache = function (moduleName) {
10-
// Run over the cache looking for the files
11-
// loaded by the specified module name
12-
searchCache(moduleName, function (mod) {
13-
delete require.cache[mod.id];
14-
});
15-
16-
// Remove cached paths to the module.
17-
// Thanks to @bentael for pointing this out.
18-
Object.keys(module.constructor._pathCache).forEach(function(cacheKey) {
19-
if (cacheKey.indexOf(moduleName)>0) {
20-
delete module.constructor._pathCache[cacheKey];
21-
}
22-
});
23-
};
24-
25-
/**
26-
* Runs over the cache to search for all the cached
27-
* files
28-
*/
29-
searchCache = function (moduleName, callback) {
30-
// Resolve the module identified by the specified name
31-
var mod = require.resolve(moduleName);
32-
33-
// Check if the module has been resolved and found within
34-
// the cache
35-
if (mod && ((mod = require.cache[mod]) !== undefined)) {
36-
// Recursively go over the results
37-
(function run(mod) {
38-
// Go over each of the module's children and
39-
// run over it
40-
mod.children.forEach(function (child) {
41-
run(child);
42-
});
43-
44-
// Call the specified callback providing the
45-
// found module
46-
callback(mod);
47-
})(mod);
48-
}
49-
};
50-
515
module.exports.getInstance = function(isModule, options, callback) {
526

537
delete require.cache[require.resolve('./api.js')];

lib/class.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
77

88
// The base Class implementation (does nothing)
9-
Class = function(){};
9+
var Class = function(){};
1010

1111
// Create a new Class that inherits from this class
12-
Class.extend = function(prop) {
12+
Class.extend = function ext(prop) {
1313
var _super = this.prototype;
1414

1515
// Instantiate a base class (but only create the instance,
@@ -56,7 +56,7 @@ Class.extend = function(prop) {
5656
Class.prototype.constructor = Class;
5757

5858
// And make this class extendable
59-
Class.extend = arguments.callee;
59+
Class.extend = ext;
6060

6161
return Class;
6262
};

lib/migrator.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ SeedLink.prototype = {
5151
}
5252
};
5353

54-
Migrator = function(driver, migrationsDir, empty, intern) {
54+
var Migrator = function(driver, migrationsDir, empty, intern) {
5555
this.driver = dbmUtil.reduceToInterface( driver, MigratorInterface );
5656
this._driver = driver;
5757
this.migrationsDir = migrationsDir;

lib/seeder.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ MigrationLink.prototype = {
9494
}
9595
};
9696

97-
Seeder = function (driver, seedsDir, versionControlled, intern) {
97+
var Seeder = function (driver, seedsDir, versionControlled, intern) {
9898
SeederInterface.extending = intern.interfaces.SeederInterface;
9999
this.driver = dbmUtil.reduceToInterface( driver, SeederInterface );
100100
this._driver = driver;

0 commit comments

Comments
 (0)