Permalink
Browse files

Merge commit '20340287c24776c67fef2becfff8cb2c409282da'

Conflicts:
	lib/driver/mysql.js - Accept theirs
  • Loading branch information...
2 parents 728aa8e + 2034028 commit 4860ff12072dd2da8bc886d60294ced63a3223c7 Jim Pravetz committed Oct 16, 2012
Showing with 66 additions and 23 deletions.
  1. +16 −0 CHANGELOG.md
  2. +7 −6 bin/db-migrate
  3. +2 −2 index.js
  4. +1 −1 lib/driver/base.js
  5. +2 −2 lib/driver/mysql.js
  6. +20 −1 lib/driver/pg.js
  7. +5 −5 lib/migration.js
  8. +5 −1 lib/migrator.js
  9. +4 −3 package.json
  10. +2 −1 test/driver/mysql_test.js
  11. +2 −1 test/driver/pg_test.js
View
16 CHANGELOG.md
@@ -58,3 +58,19 @@ Improvement:
Fix:
- Fix default value error when using Postgres #43
+
+## 0.2.3
+
+Fix:
+ - Fix error when using mysql-2.0.0-alpha3 driver #44
+
+## 0.2.4
+
+Fix:
+ - Use --migrations-dir option when specified #47
+ - Handle unique constraints in changeColumn() for postgres #46 (via @ryanmeador)
+
+## 0.2.5
+
+Fix:
+ - Fix randomly occuring migration sorting error on empty db #51
View
13 bin/db-migrate
@@ -1,13 +1,14 @@
#!/usr/bin/env node
-var optimist = require('optimist');
+var assert = require('assert');
var fs = require('fs');
+var path = require('path');
+var util = require('util');
+var mkdirp = require('mkdirp');
+var optimist = require('optimist');
var config = require('../lib/config.js');
var index = require('../index');
var log = require('../lib/log');
-var util = require('util');
-var path = require('path');
-var assert = require('assert');
process.on('uncaughtException', function(err) {
log.error(err);
@@ -56,7 +57,7 @@ global.verbose = argv.verbose;
function createMigrationDir(dir, callback) {
fs.stat(dir, function(err, stat) {
if (err) {
- fs.mkdir(dir, callback);
+ mkdirp(dir, callback);
} else {
callback();
}
@@ -85,7 +86,7 @@ function executeCreate() {
}
argv.title = argv._.shift();
- index.createMigration(argv.title, function(err, migration) {
+ index.createMigration(argv.title, argv['migrations-dir'], function(err, migration) {
assert.ifError(err);
log.info(util.format('Created migration at %s', migration.path));
});
View
4 index.js
@@ -11,8 +11,8 @@ exports.connect = function(config, callback) {
});
};
-exports.createMigration = function(title, callback) {
- var migration = new Migration(title, new Date());
+exports.createMigration = function(title, migrationsDir, callback) {
+ var migration = new Migration(title, migrationsDir, new Date());
migration.write(function(err) {
if (err) { callback(err); return; }
callback(null, migration);
View
2 lib/driver/base.js
@@ -158,7 +158,7 @@ module.exports = Base = Class.extend({
columnNames += columnNameArray[index];
if (typeof(valueArray[index]) === 'string') {
- values += "'" + valueArray[index] + "'";
+ values += "'" + valueArray[index].replace("'", "''") + "'";
} else {
values += valueArray[index];
}
View
4 lib/driver/mysql.js
@@ -107,8 +107,8 @@ var MysqlDriver = Base.extend({
exports.connect = function(config, callback) {
var db;
- if (typeof mysql.createConnection === 'undefined') {
- db = config.db || new mysql.createClient(config);
+ if (typeof(mysql.createConnection) === 'undefined') {
+ db = config.db || new mysql.createClient(config);
} else {
db = config.db || new mysql.createConnection(config);
}
View
21 lib/driver/pg.js
@@ -116,7 +116,26 @@ var PgDriver = Base.extend({
function setNotNull() {
var setOrDrop = columnSpec.notNull === true ? 'SET' : 'DROP';
var sql = util.format("ALTER TABLE %s ALTER COLUMN %s %s NOT NULL", tableName, columnName, setOrDrop);
- this.runSql(sql, setDefaultValue.bind(this));
+ this.runSql(sql, setUnique.bind(this));
+ }
+
+ function setUnique(err) {
+ if (err) {
+ callback(err);
+ }
+
+ var sql;
+ var constraintName = tableName + '_' + columnName + '_unique';
+
+ if (columnSpec.unique === true) {
+ sql = util.format("ALTER TABLE %s ADD CONSTRAINT %s UNIQUE (%s)", tableName, constraintName, columnName);
+ this.runSql(sql, setDefaultValue.bind(this));
+ } else if (columnSpec.unique === false) {
+ sql = util.format("ALTER TABLE %s DROP CONSTRAINT %s", tableName, constraintName);
+ this.runSql(sql, setDefaultValue.bind(this));
+ } else {
+ setDefaultValue.call(this);
+ }
}
function setDefaultValue(err) {
View
10 lib/migration.js
@@ -4,8 +4,8 @@ var inflection = require('./inflection');
var lpad = require('./util').lpad;
var config = require('./config');
-function formatPath(name) {
- return process.cwd() + '/migrations/' + name + '.js';
+function formatPath(dir, name) {
+ return path.join(dir, name + '.js');
}
function formatName(title, date) {
@@ -64,11 +64,11 @@ var migrationTemplate = [
].join("\n");
Migration = function() {
- if (arguments.length == 2) {
+ if (arguments.length == 3) {
this.title = arguments[0];
- this.date = arguments[1];
+ this.date = arguments[2];
this.name = formatName(this.title, this.date);
- this.path = formatPath(this.name);
+ this.path = formatPath(arguments[1], this.name);
} else if (arguments.length == 1) {
this.path = arguments[0];
this.name = Migration.parseName(this.path);
View
6 lib/migrator.js
@@ -22,7 +22,11 @@ function isIncludedInDown(migration, destination) {
}
function filterUp(allMigrations, completedMigrations, destination, count) {
- return allMigrations.sort()
+ var sortFn = function(a, b) {
+ return a.name.slice(0, a.name.indexOf('-')) - b.name.slice(0, b.name.indexOf('-'));
+ };
+
+ return allMigrations.sort(sortFn)
.filter(function(migration) {
var hasRun = completedMigrations.some(function(completedMigration) {
return completedMigration.name === migration.name;
View
7 package.json
@@ -11,7 +11,7 @@
"sqlite",
"mysql"
],
- "version": "0.2.2",
+ "version": "0.2.5",
"engines": {
"node": ">=0.6.0"
},
@@ -36,13 +36,14 @@
"dependencies": {
"optimist": "~0.3.0",
"async": "~0.1.15",
- "semver": "~1.0.14"
+ "semver": "~1.0.14",
+ "mkdirp": "~0.3.4"
},
"devDependencies": {
"vows": "~0.6.2",
"db-meta": "~0.4.0"
},
"scripts": {
- "test": "vows"
+ "test": "node_modules/.bin/vows"
}
}
View
3 test/driver/mysql_test.js
@@ -260,7 +260,7 @@ driver.connect({ driver: 'mysql', database: 'db_migrate_test', user:'root' }, fu
topic: function() {
db.createTable('event', {
id: { type: dataType.INTEGER, primaryKey: true, autoIncrement: true },
- txt: { type: dataType.STRING, notNull: true, defaultValue: "foo" }
+ txt: { type: dataType.STRING, notNull: true, defaultValue: "foo", unique: true }
}, function(err) {
if (err) {
return this.callback(err);
@@ -291,6 +291,7 @@ driver.connect({ driver: 'mysql', database: 'db_migrate_test', user:'root' }, fu
assert.equal(column.getName(), 'txt');
assert.equal(column.isNullable(), true);
assert.equal(column.getDefaultValue(), "foo2");
+ assert.equal(column.isUnique(), true);
}
}
}
View
3 test/driver/pg_test.js
@@ -260,7 +260,7 @@ driver.connect({ driver: 'pg', database: 'db_migrate_test' }, function(err, db)
id: { type: dataType.INTEGER, primaryKey: true, autoIncrement: true },
txt: { type: dataType.TEXT, notNull: true, defaultValue: "foo" }
}, function() {
- var spec = { notNull: false, defaultValue: "foo2" };
+ var spec = { notNull: false, defaultValue: "foo2", unique: true };
db.changeColumn('event', 'txt', spec, this.callback.bind(this, null));
}.bind(this));
},
@@ -282,6 +282,7 @@ driver.connect({ driver: 'pg', database: 'db_migrate_test' }, function(err, db)
assert.equal(column.getName(), 'txt');
assert.equal(column.isNullable(), true);
assert.equal(column.getDefaultValue(), "'foo2'::text");
+ assert.equal(column.isUnique(), true);
}
},

0 comments on commit 4860ff1

Please sign in to comment.