Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added optional parameter 'tableName' to 'removeIndex' to make the api…

… compatible with mysql driver
  • Loading branch information...
commit 22bae598f32751524ff7331f94fd2315dd37367d 1 parent 2034028
@aprobus aprobus authored
View
3  README.md
@@ -343,12 +343,13 @@ __Arguments__
* valueArray - the array of values to be inserted into the associated column
* callback(err) - callback that will be invoked once the insert has been completed.
-### removeIndex(indexName, callback)
+### removeIndex([tableName], indexName, callback)
Remove an index
__Arguments__
+* tableName - name of the table that has the index (Required for mySql)
* indexName - the name of the index
* callback(err) - callback that will be invoked after removing the index
View
9 lib/driver/base.js
@@ -173,7 +173,14 @@ module.exports = Base = Class.extend({
this.runSql(sql, callback);
},
- removeIndex: function(indexName, callback) {
+ removeIndex: function(tableName, indexName, callback) {
+ if (arguments.length === 2 && typeof(indexName) === 'function') {
+ callback = indexName;
+ indexName = tableName;
+ } else if (arguments.length === 1 && typeof(tableName) === 'string') {
+ indexName = tableName;
+ }
+
var sql = util.format('DROP INDEX %s', indexName);
this.runSql(sql, callback);
},
View
10 lib/driver/mysql.js
@@ -79,6 +79,16 @@ var MysqlDriver = Base.extend({
},
removeIndex: function(tableName, indexName, callback) {
+ // tableName is optional for other drivers, but required for mySql. So, check the args to ensure they are valid
+ if (arguments.length === 2 && typeof(indexName) === 'function') {
+ callback = indexName;
+ process.nextTick(function () {
+ callback(new Error('Illegal arguments, must provide "tableName" and "indexName"'));
+ });
+
+ return;
+ }
+
var sql = util.format('DROP INDEX %s ON %s', indexName, tableName);
this.runSql(sql, callback);
},
View
22 test/driver/mysql_test.js
@@ -385,6 +385,28 @@ driver.connect({ driver: 'mysql', database: 'db_migrate_test', user:'root' }, fu
}
}
}).addBatch({
+ 'removeIndexInvalidArgs': {
+ topic: function() {
+ db.createTable('event', {
+ id: { type: dataType.INTEGER, primaryKey: true, autoIncrement: true },
+ title: { type: dataType.STRING }
+ }, function() {
+ db.addIndex('event', 'event_title', 'title', function(err) {
+ db.removeIndex('event_title', this.callback.bind(this, null));
+ }.bind(this));
+ }.bind(this));
+ },
+
+ 'removeIndex has errored': function (err) {
+ assert.isNotNull(err);
+ assert.equal(err.message, 'Illegal arguments, must provide "tableName" and "indexName"');
+ },
+
+ teardown: function() {
+ db.dropTable('event', this.callback);
+ }
+ }
+ }).addBatch({
'createMigrationsTable': {
topic: function() {
db.createMigrationsTable(this.callback.bind(this, null));
View
36 test/driver/sqlite3_test.js
@@ -306,6 +306,42 @@ vows.describe('sqlite3').addBatch({
}
}
}).addBatch({
+ 'removeIndexWithTableName': {
+ topic: function() {
+ driver.connect({ driver: 'sqlite3', filename: 'test.db' }, function(err, db) {
+ db.createTable('event', {
+ id: { type: dataType.INTEGER, primaryKey: true, autoIncrement: true },
+ title: { type: dataType.STRING }
+ }, function(err) {
+ db.addIndex('event', 'event_title', 'title', function(err) {
+ db.removeIndex('event', 'event_title', this.callback.bind(this, null, db));
+ }.bind(this));
+ }.bind(this));
+ }.bind(this));
+ },
+
+ teardown: function(db) {
+ db.close();
+ fs.unlink('test.db', this.callback);
+ },
+
+ 'has resulting index metadata': {
+ topic: function(db) {
+ dbmeta('sqlite3', {connection:db.connection} , function (err, meta) {
+ if (err) {
+ return this.callback(err);
+ }
+ meta.getIndexes('event', this.callback);
+ }.bind(this));
+ },
+
+ 'without index': function(err, indexes) {
+ assert.isNotNull(indexes);
+ assert.equal(indexes.length, 0);
+ }
+ }
+ }
+}).addBatch({
'createMigrationsTable': {
topic: function() {
driver.connect({ driver: 'sqlite3', filename: 'test.db' }, function(err,db) {
Please sign in to comment.
Something went wrong with that request. Please try again.