Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

DATABASE_URL can be used to configure any database.

  • Loading branch information...
commit 250823fd85f48e0bbc7e0cf3d1ba67d44eee3b29 1 parent f1a8025
@pwnall pwnall authored
View
19 README.md
@@ -98,7 +98,7 @@ exports.up = function (db, callback) {
exports.down = function (db, callback) {
db.dropTable('pets', function(err) {
if (err) { callback(err); return; }
- db.dropTable('owners', callback);
+ db.dropTable('owners', callback);
});
};
```
@@ -204,7 +204,7 @@ You can pass the -e or --env option to db-migrate to select the environment you
The above will run all migrations that haven't yet been run in the prod environment, grabbing the settings from config/database.json.
-Alternatively, for PostgreSQL, you can specify a DATABASE_URL
+Alternatively, you can specify a DATABASE_URL
environment variable that will be used in place of the configuration
file settings. This is helpful for use with Heroku.
@@ -378,6 +378,21 @@ __Arguments__
* params - zero or more ? replacement parameters
* callback(err, results) - callback that will be invoked after executing the SQL
+## Development
+
+The following command runs the vows tests.
+
+```bash
+npm test
+```
+
+Running the tests requires a one-time setup of the MySQL and Postgres databases.
+
+```bash
+mysql -u root -e "CREATE DATABASE db_migrate_test;"
+createdb db_migrate_test
+```
+
## License
(The MIT License)
View
6 bin/db-migrate
@@ -82,7 +82,11 @@ function createMigrationDir(dir, callback) {
}
function loadConfig() {
- config.load(argv.config, argv.env);
+ if (process.env["DATABASE_URL"]) {
+ config.loadUrl(process.env["DATABASE_URL"], argv.env);
+ } else {
+ config.load(argv.config, argv.env);
+ }
if(verbose) {
var current = config.getCurrent();
log.info("Using", current.env, "settings:", current.settings);
View
16 lib/config.js
@@ -1,5 +1,6 @@
var fs = require('fs');
var path = require('path');
+var parseDatabaseUrl = require('parse-database-url');
var dbmUtil = require('./util');
exports.load = function(fileName, currentEnv) {
@@ -29,8 +30,23 @@ exports.load = function(fileName, currentEnv) {
}
delete exports.load;
+ delete exports.loadUrl;
};
+exports.loadUrl = function(databaseUrl, currentEnv) {
+ var config = parseDatabaseUrl(databaseUrl);
+ if (currentEnv) {
+ exports[currentEnv] = config;
+ setCurrent(currentEnv);
+ } else {
+ exports.urlConfig = config;
+ setCurrent('urlConfig');
+ }
+
+ delete exports.load;
+ delete exports.loadUrl;
+}
+
var setCurrent = exports.setCurrent = function (env) {
env = dbmUtil.isArray(env) ? env : [env];
View
2  lib/driver/pg.js
@@ -198,6 +198,6 @@ var PgDriver = Base.extend({
});
exports.connect = function(config, callback) {
- var db = config.db || new pg.Client(process.env.DATABASE_URL || config);
+ var db = config.db || new pg.Client(config);
callback(null, new PgDriver(db));
};
View
1  lib/driver/postgres.js
@@ -0,0 +1 @@
+module.exports = require('./pg');
View
8 package.json
@@ -43,11 +43,15 @@
"semver": "~1.0.14",
"mkdirp": "~0.3.4",
"moment": "~1.7.2",
- "pkginfo": "~0.3.0"
+ "pkginfo": "~0.3.0",
+ "parse-database-url": "~0.1.0"
},
"devDependencies": {
"vows": "~0.7.0",
- "db-meta": "~0.4.1"
+ "db-meta": "~0.4.1",
+ "mysql": "~2.0.0",
+ "pg": "~1.0.0",
+ "sqlite3": "~2.1.0"
},
"scripts": {
"test": "node_modules/.bin/vows"
View
44 test/config_test.js
@@ -3,10 +3,15 @@ var assert = require('assert');
var config = require('../lib/config');
var path = require('path');
+var _configLoad = config.load;
+var _configLoadUrl = config.loadUrl;
+
vows.describe('config').addBatch({
- 'library': {
+ 'loading from a file': {
topic: function() {
var configPath = path.join(__dirname, 'database.json');
+ config.load = _configLoad;
+ config.loadUrl = _configLoadUrl;
config.load(configPath, 'dev');
return config;
},
@@ -15,6 +20,10 @@ vows.describe('config').addBatch({
assert.isUndefined(config.load);
},
+ 'should remove the loadUrl function': function (config) {
+ assert.isUndefined(config.loadUrl);
+ },
+
'should export all environment settings': function (config) {
assert.isDefined(config.dev);
assert.isDefined(config.test);
@@ -29,5 +38,38 @@ vows.describe('config').addBatch({
assert.equal(current.settings.filename, ':memory:');
}
}
+}).addBatch({
+ 'loading from an URL': {
+ topic: function() {
+ var databaseUrl = 'postgres://uname:pw@server.com/dbname';
+ config.load = _configLoad;
+ config.loadUrl = _configLoadUrl;
+ config.loadUrl(databaseUrl, 'dev');
+ return config;
+ },
+
+ 'should remove the load function': function (config) {
+ assert.isUndefined(config.load);
+ },
+
+ 'should remove the loadUrl function': function (config) {
+ assert.isUndefined(config.loadUrl);
+ },
+
+ 'should export the settings as the current environment': function (config) {
+ assert.isDefined(config.dev);
+ },
+
+ 'should export a getCurrent function with all current environment settings': function (config) {
+ assert.isDefined(config.getCurrent);
+ var current = config.getCurrent();
+ assert.equal(current.env, 'dev');
+ assert.equal(current.settings.driver, 'postgres');
+ assert.equal(current.settings.user, 'uname');
+ assert.equal(current.settings.password, 'pw');
+ assert.equal(current.settings.host, 'server.com');
+ assert.equal(current.settings.database, 'dbname');
+ }
+ }
}).export(module);
Please sign in to comment.
Something went wrong with that request. Please try again.