Drop indexes correctly, closes #276

This commit is contained in:
Blake Embrey 2014-05-20 17:30:40 +10:00
parent 9580e7c7c6
commit d0d4549c21
2 changed files with 22 additions and 23 deletions

View File

@ -83,37 +83,37 @@ _.extend(SchemaBuilder.prototype, Common, {
// Indicate that the given primary key should be dropped.
dropPrimary: function(index) {
return this._dropIndexCommand('dropPrimary', index);
return this._indexCommand('dropPrimary', 'primary', index);
},
// Indicate that the given unique key should be dropped.
dropUnique: function(index) {
return this._dropIndexCommand('dropUnique', index);
return this._indexCommand('dropUnique', 'unique', index);
},
// Indicate that the given index should be dropped.
dropIndex: function(index) {
return this._dropIndexCommand('dropIndex', index);
return this._indexCommand('dropIndex', 'index', index);
},
// Indicate that the given foreign key should be dropped.
dropForeign: function(index) {
return this._dropIndexCommand('dropForeign', index);
return this._indexCommand('dropForeign', 'foreign', index);
},
// Specify the primary key(s) for the table.
primary: function(columns, name) {
return this._indexCommand('primary', columns, name);
return this._indexCommand('primary', 'primary', columns, name);
},
// Specify a unique index for the table.
unique: function(columns, name) {
return this._indexCommand('unique', columns, name);
return this._indexCommand('unique', 'unique', columns, name);
},
// Specify an index for the table.
index: function(columns, name) {
return this._indexCommand('index', columns, name);
return this._indexCommand('index', 'index', columns, name);
},
// Rename a column from one value to another value.
@ -124,7 +124,7 @@ _.extend(SchemaBuilder.prototype, Common, {
// Specify a foreign key for the table, also getting any
// relevant info from the chain during column.
foreign: function(column, name) {
var chained, chainable = this._indexCommand('foreign', column, name);
var chained, chainable = this._indexCommand('foreign', 'foreign', column, name);
if (_.isObject(column)) {
chained = _.pick(column, 'foreignColumn', 'foreignTable', 'commandOnDelete', 'commandOnUpdate');
}
@ -265,30 +265,18 @@ _.extend(SchemaBuilder.prototype, Common, {
// ----------------------------------------------------------------------
// Create a new drop index command on the blueprint.
// If the index is an array of columns, the developer means
// to drop an index merely by specifying the columns involved.
_dropIndexCommand: function(type, index) {
var columns = [];
if (_.isArray(index)) {
columns = index;
index = null;
}
return this._indexCommand(type, columns, index);
},
// Add a new index command to the blueprint.
// If no name was specified for this index, we will create one using a basic
// convention of the table name, followed by the columns, followed by an
// index type, such as primary or index, which makes the index unique.
_indexCommand: function(type, columns, index) {
_indexCommand: function(command, type, columns, index) {
index || (index = null);
if (!_.isArray(columns)) columns = columns ? [columns] : [];
if (index === null) {
var table = this.table.replace(/\.|-/g, '_');
index = (table + '_' + _.map(columns, function(col) { return col.name || col; }).join('_') + '_' + type).toLowerCase();
}
return this._addCommand(type, {index: index, columns: columns});
return this._addCommand(command, {index: index, columns: columns});
},
// Add a new column to the blueprint.

View File

@ -127,6 +127,17 @@ module.exports = function(knex) {
});
});
it('allows dropping a unique index', function() {
return knex.schema.table('composite_key_test', function(t) {
t.dropUnique(['column_a', 'column_b']);
});
});
it('allows dropping a index', function() {
return knex.schema.table('test_table_one', function(t) {
t.dropIndex('first_name');
});
});
});
@ -185,4 +196,4 @@ module.exports = function(knex) {
});
};
};