mirror of
https://github.com/knex/knex.git
synced 2025-12-29 07:59:31 +00:00
changed mssql to compact create format. changed unique index to unique constraint
This commit is contained in:
parent
aa8df07711
commit
f99d3952a1
@ -19,6 +19,7 @@ inherits(TableCompiler_MSSQL, TableCompiler);
|
||||
|
||||
assign(TableCompiler_MSSQL.prototype, {
|
||||
|
||||
createAlterTableMethods: ['foreign', 'primary', 'unique'],
|
||||
createQuery: function createQuery(columns, ifNot) {
|
||||
var createStatement = ifNot ? 'if object_id(\'' + this.tableName() + '\', \'U\') is not null CREATE TABLE ' : 'CREATE TABLE ';
|
||||
var sql = createStatement + this.tableName() + (this._formatting ? ' (\n ' : ' (') + columns.sql.join(this._formatting ? ',\n ' : ', ') + ')';
|
||||
@ -74,6 +75,7 @@ assign(TableCompiler_MSSQL.prototype, {
|
||||
});
|
||||
}));
|
||||
},
|
||||
|
||||
index: function index(columns, indexName) {
|
||||
indexName = indexName || this._indexCommand('index', this.tableNameRaw, columns);
|
||||
this.pushQuery('CREATE INDEX ' + indexName + ' ON ' + this.tableName() + ' (' + this.formatter.columnize(columns) + ')');
|
||||
@ -81,12 +83,20 @@ assign(TableCompiler_MSSQL.prototype, {
|
||||
|
||||
primary: function primary(columns, indexName) {
|
||||
indexName = indexName || this._indexCommand('primary', this.tableNameRaw, columns);
|
||||
this.pushQuery('ALTER TABLE ' + this.tableName() + ' ADD PRIMARY KEY (' + this.formatter.columnize(columns) + ')');
|
||||
if (!this.forCreate) {
|
||||
this.pushQuery('ALTER TABLE ' + this.tableName() + ' ADD PRIMARY KEY (' + this.formatter.columnize(columns) + ')');
|
||||
} else {
|
||||
this.pushQuery('CONSTRAINT ' + indexName + ' PRIMARY KEY (' + this.formatter.columnize(columns) + ')');
|
||||
}
|
||||
},
|
||||
|
||||
unique: function unique(columns, indexName) {
|
||||
indexName = indexName || this._indexCommand('unique', this.tableNameRaw, columns);
|
||||
this.pushQuery('CREATE UNIQUE INDEX ' + indexName + ' ON ' + this.tableName() + ' (' + this.formatter.columnize(columns) + ')');
|
||||
if (!this.forCreate) {
|
||||
this.pushQuery('CREATE UNIQUE INDEX ' + indexName + ' ON ' + this.tableName() + ' (' + this.formatter.columnize(columns) + ')');
|
||||
} else {
|
||||
this.pushQuery('CONSTRAINT ' + indexName + ' UNIQUE (' + this.formatter.columnize(columns) + ')');
|
||||
}
|
||||
},
|
||||
|
||||
// Compile a drop index command.
|
||||
@ -109,7 +119,7 @@ assign(TableCompiler_MSSQL.prototype, {
|
||||
// Compile a drop unique key command.
|
||||
dropUnique: function dropUnique(column, indexName) {
|
||||
indexName = indexName || this._indexCommand('unique', this.tableNameRaw, column);
|
||||
this.pushQuery('DROP INDEX ' + indexName + ' ON ' + this.tableName());
|
||||
this.pushQuery('ALTER TABLE ' + this.tableName() + ' DROP CONSTRAINT ' + indexName);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
@ -37,9 +37,13 @@ TableCompiler.prototype.lowerCase = true;
|
||||
// If this is a table "creation", we need to first run through all
|
||||
// of the columns to build them into a single string,
|
||||
// and then run through anything else and push it to the query sequence.
|
||||
TableCompiler.prototype.createAlterTableMethods = null;
|
||||
TableCompiler.prototype.create = function (ifNot) {
|
||||
var columns = this.getColumns();
|
||||
var columnTypes = this.getColumnTypes(columns);
|
||||
if (this.createAlterTableMethods) {
|
||||
this.alterTableForCreate(columnTypes);
|
||||
}
|
||||
this.createQuery(columnTypes, ifNot);
|
||||
this.columnQueries(columns);
|
||||
delete this.single.comment;
|
||||
@ -71,9 +75,9 @@ TableCompiler.prototype.foreign = function (foreignData) {
|
||||
var onUpdate = foreignData.onUpdate ? (this.lowerCase ? ' on update ' : ' ON UPDATE ') + foreignData.onUpdate : '';
|
||||
var onDelete = foreignData.onDelete ? (this.lowerCase ? ' on delete ' : ' ON DELETE ') + foreignData.onDelete : '';
|
||||
if (this.lowerCase) {
|
||||
this.pushQuery('alter table ' + this.tableName() + ' add constraint ' + keyName + ' ' + 'foreign key (' + column + ') references ' + inTable + ' (' + references + ')' + onUpdate + onDelete);
|
||||
this.pushQuery((!this.forCreate ? 'alter table ' + this.tableName() + ' add ' : '') + 'constraint ' + keyName + ' ' + 'foreign key (' + column + ') references ' + inTable + ' (' + references + ')' + onUpdate + onDelete);
|
||||
} else {
|
||||
this.pushQuery('ALTER TABLE ' + this.tableName() + ' ADD CONSTRAINT ' + keyName + ' ' + 'FOREIGN KEY (' + column + ') REFERENCES ' + inTable + ' (' + references + ')' + onUpdate + onDelete);
|
||||
this.pushQuery((!this.forCreate ? 'ALTER TABLE ' + this.tableName() + ' ADD ' : '') + 'CONSTRAINT ' + keyName + ' ' + 'FOREIGN KEY (' + column + ') REFERENCES ' + inTable + ' (' + references + ')' + onUpdate + onDelete);
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -147,6 +151,29 @@ TableCompiler.prototype.alterTable = function () {
|
||||
}
|
||||
};
|
||||
|
||||
TableCompiler.prototype.alterTableForCreate = function (columnTypes) {
|
||||
this.forCreate = true;
|
||||
var savedSequence = this.sequence;
|
||||
var alterTable = this.grouped.alterTable || [];
|
||||
this.grouped.alterTable = [];
|
||||
for (var i = 0, l = alterTable.length; i < l; i++) {
|
||||
var statement = alterTable[i];
|
||||
if (_.indexOf(this.createAlterTableMethods, statement.method) < 0) {
|
||||
this.grouped.alterTable.push(statement);
|
||||
continue;
|
||||
}
|
||||
if (this[statement.method]) {
|
||||
this.sequence = [];
|
||||
this[statement.method].apply(this, statement.args);
|
||||
columnTypes.sql.push(this.sequence[0].sql);
|
||||
} else {
|
||||
console.error('Debug: ' + statement.method + ' does not exist');
|
||||
}
|
||||
}
|
||||
this.sequence = savedSequence;
|
||||
this.forCreate = false;
|
||||
};
|
||||
|
||||
// Drop the index on the current table.
|
||||
TableCompiler.prototype.dropIndex = function (value) {
|
||||
this.pushQuery('drop index' + value);
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
|
||||
// MSSQL Table Builder & Compiler
|
||||
// -------
|
||||
var inherits = require('inherits');
|
||||
var inherits = require('inherits');
|
||||
var TableCompiler = require('../../../schema/tablecompiler');
|
||||
var helpers = require('../../../helpers');
|
||||
var Promise = require('../../../promise');
|
||||
var assign = require('lodash/object/assign');
|
||||
var helpers = require('../../../helpers');
|
||||
var Promise = require('../../../promise');
|
||||
var assign = require('lodash/object/assign');
|
||||
|
||||
// Table Compiler
|
||||
// ------
|
||||
@ -16,9 +16,10 @@ function TableCompiler_MSSQL() {
|
||||
inherits(TableCompiler_MSSQL, TableCompiler);
|
||||
|
||||
assign(TableCompiler_MSSQL.prototype, {
|
||||
|
||||
createQuery: function(columns, ifNot) {
|
||||
var createStatement = ifNot ? 'if object_id(\'' + this.tableName() +'\', \'U\') is not null CREATE TABLE ' : 'CREATE TABLE ';
|
||||
|
||||
createAlterTableMethods: ['foreign', 'primary', 'unique'],
|
||||
createQuery: function (columns, ifNot) {
|
||||
var createStatement = ifNot ? 'if object_id(\'' + this.tableName() + '\', \'U\') is not null CREATE TABLE ' : 'CREATE TABLE ';
|
||||
var sql = createStatement + this.tableName() + (this._formatting ? ' (\n ' : ' (') + columns.sql.join(this._formatting ? ',\n ' : ', ') + ')';
|
||||
|
||||
if (this.single.comment) {
|
||||
@ -30,21 +31,21 @@ assign(TableCompiler_MSSQL.prototype, {
|
||||
},
|
||||
|
||||
lowerCase: false,
|
||||
|
||||
|
||||
addColumnsPrefix: 'ADD ',
|
||||
|
||||
|
||||
dropColumnPrefix: 'DROP COLUMN ',
|
||||
|
||||
// Compiles the comment on the table.
|
||||
comment: function() {
|
||||
comment: function () {
|
||||
},
|
||||
|
||||
changeType: function() {
|
||||
changeType: function () {
|
||||
},
|
||||
|
||||
// Renames a column on the table.
|
||||
renameColumn: function(from, to) {
|
||||
this.pushQuery('exec sp_rename ' + this.formatter.parameter(this.tableName() + '.' + from) + ', ' + this.formatter.parameter(to) + ', \'COLUMN\'');
|
||||
renameColumn: function (from, to) {
|
||||
this.pushQuery('exec sp_rename ' + this.formatter.parameter(this.tableName() + '.' + from) + ', ' + this.formatter.parameter(to) + ', \'COLUMN\'');
|
||||
},
|
||||
|
||||
dropFKRefs: function (runner, refs) {
|
||||
@ -59,58 +60,67 @@ assign(TableCompiler_MSSQL.prototype, {
|
||||
},
|
||||
createFKRefs: function (runner, refs) {
|
||||
var formatter = this.client.formatter();
|
||||
|
||||
|
||||
return Promise.all(refs.map(function (ref) {
|
||||
var tableName = formatter.wrap(ref.TABLE_NAME);
|
||||
var keyName = formatter.wrap(ref.CONSTRAINT_NAME);
|
||||
var column = formatter.columnize(ref.COLUMN_NAME);
|
||||
var tableName = formatter.wrap(ref.TABLE_NAME);
|
||||
var keyName = formatter.wrap(ref.CONSTRAINT_NAME);
|
||||
var column = formatter.columnize(ref.COLUMN_NAME);
|
||||
var references = formatter.columnize(ref.REFERENCED_COLUMN_NAME);
|
||||
var inTable = formatter.wrap(ref.REFERENCED_TABLE_NAME);
|
||||
var onUpdate = ' ON UPDATE ' + ref.UPDATE_RULE;
|
||||
var onDelete = ' ON DELETE ' + ref.DELETE_RULE;
|
||||
|
||||
var inTable = formatter.wrap(ref.REFERENCED_TABLE_NAME);
|
||||
var onUpdate = ' ON UPDATE ' + ref.UPDATE_RULE;
|
||||
var onDelete = ' ON DELETE ' + ref.DELETE_RULE;
|
||||
|
||||
return runner.query({
|
||||
sql: 'ALTER TABLE ' + tableName + ' ADD CONSTRAINT ' + keyName +
|
||||
' FOREIGN KEY (' + column + ') REFERENCES ' + inTable + ' (' + references + ')' + onUpdate + onDelete
|
||||
sql: 'ALTER TABLE ' + tableName + ' ADD CONSTRAINT ' + keyName +
|
||||
' FOREIGN KEY (' + column + ') REFERENCES ' + inTable + ' (' + references + ')' + onUpdate + onDelete
|
||||
});
|
||||
}));
|
||||
},
|
||||
index: function(columns, indexName) {
|
||||
|
||||
index: function (columns, indexName) {
|
||||
indexName = indexName || this._indexCommand('index', this.tableNameRaw, columns);
|
||||
this.pushQuery('CREATE INDEX ' + indexName + ' ON ' + this.tableName() + ' (' + this.formatter.columnize(columns) + ')');
|
||||
},
|
||||
|
||||
primary: function(columns, indexName) {
|
||||
primary: function (columns, indexName) {
|
||||
indexName = indexName || this._indexCommand('primary', this.tableNameRaw, columns);
|
||||
this.pushQuery('ALTER TABLE ' + this.tableName() + ' ADD PRIMARY KEY (' + this.formatter.columnize(columns) + ')');
|
||||
if (!this.forCreate) {
|
||||
this.pushQuery('ALTER TABLE ' + this.tableName() + ' ADD PRIMARY KEY (' + this.formatter.columnize(columns) + ')');
|
||||
} else {
|
||||
this.pushQuery('CONSTRAINT ' + indexName + ' PRIMARY KEY (' + this.formatter.columnize(columns) + ')');
|
||||
}
|
||||
},
|
||||
|
||||
unique: function(columns, indexName) {
|
||||
unique: function (columns, indexName) {
|
||||
indexName = indexName || this._indexCommand('unique', this.tableNameRaw, columns);
|
||||
this.pushQuery('CREATE UNIQUE INDEX ' + indexName + ' ON ' + this.tableName() + ' (' + this.formatter.columnize(columns) + ')');
|
||||
if (!this.forCreate) {
|
||||
this.pushQuery('CREATE UNIQUE INDEX ' + indexName + ' ON ' + this.tableName() + ' (' + this.formatter.columnize(columns) + ')');
|
||||
} else {
|
||||
this.pushQuery('CONSTRAINT ' + indexName + ' UNIQUE (' + this.formatter.columnize(columns) + ')');
|
||||
}
|
||||
},
|
||||
|
||||
// Compile a drop index command.
|
||||
dropIndex: function(columns, indexName) {
|
||||
dropIndex: function (columns, indexName) {
|
||||
indexName = indexName || this._indexCommand('index', this.tableNameRaw, columns);
|
||||
this.pushQuery('DROP INDEX ' + indexName + ' ON ' + this.tableName());
|
||||
},
|
||||
|
||||
// Compile a drop foreign key command.
|
||||
dropForeign: function(columns, indexName) {
|
||||
dropForeign: function (columns, indexName) {
|
||||
indexName = indexName || this._indexCommand('foreign', this.tableNameRaw, columns);
|
||||
this.pushQuery('ALTER TABLE ' + this.tableName() + ' DROP CONSTRAINT ' + indexName);
|
||||
},
|
||||
|
||||
// Compile a drop primary key command.
|
||||
dropPrimary: function() {
|
||||
dropPrimary: function () {
|
||||
this.pushQuery('ALTER TABLE ' + this.tableName() + ' DROP PRIMARY KEY');
|
||||
},
|
||||
|
||||
// Compile a drop unique key command.
|
||||
dropUnique: function(column, indexName) {
|
||||
dropUnique: function (column, indexName) {
|
||||
indexName = indexName || this._indexCommand('unique', this.tableNameRaw, column);
|
||||
this.pushQuery('DROP INDEX ' + indexName + ' ON ' + this.tableName());
|
||||
this.pushQuery('ALTER TABLE ' + this.tableName() + ' DROP CONSTRAINT ' + indexName);
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
@ -6,15 +6,15 @@ var helpers = require('./helpers');
|
||||
var normalizeArr = require('../helpers').normalizeArr
|
||||
|
||||
function TableCompiler(client, tableBuilder) {
|
||||
this.client = client
|
||||
this.method = tableBuilder._method;
|
||||
this.schemaNameRaw = tableBuilder._schemaName;
|
||||
this.tableNameRaw = tableBuilder._tableName;
|
||||
this.single = tableBuilder._single;
|
||||
this.grouped = _.groupBy(tableBuilder._statements, 'grouping');
|
||||
this.formatter = client.formatter();
|
||||
this.sequence = [];
|
||||
this._formatting = client.config && client.config.formatting
|
||||
this.client = client
|
||||
this.method = tableBuilder._method;
|
||||
this.schemaNameRaw = tableBuilder._schemaName;
|
||||
this.tableNameRaw = tableBuilder._tableName;
|
||||
this.single = tableBuilder._single;
|
||||
this.grouped = _.groupBy(tableBuilder._statements, 'grouping');
|
||||
this.formatter = client.formatter();
|
||||
this.sequence = [];
|
||||
this._formatting = client.config && client.config.formatting
|
||||
}
|
||||
|
||||
TableCompiler.prototype.pushQuery = helpers.pushQuery
|
||||
@ -22,7 +22,7 @@ TableCompiler.prototype.pushQuery = helpers.pushQuery
|
||||
TableCompiler.prototype.pushAdditional = helpers.pushAdditional
|
||||
|
||||
// Convert the tableCompiler toSQL
|
||||
TableCompiler.prototype.toSQL = function() {
|
||||
TableCompiler.prototype.toSQL = function () {
|
||||
this[this.method]();
|
||||
return this.sequence;
|
||||
};
|
||||
@ -35,9 +35,13 @@ TableCompiler.prototype.lowerCase = true;
|
||||
// If this is a table "creation", we need to first run through all
|
||||
// of the columns to build them into a single string,
|
||||
// and then run through anything else and push it to the query sequence.
|
||||
TableCompiler.prototype.create = function(ifNot) {
|
||||
TableCompiler.prototype.createAlterTableMethods = null;
|
||||
TableCompiler.prototype.create = function (ifNot) {
|
||||
var columns = this.getColumns();
|
||||
var columnTypes = this.getColumnTypes(columns);
|
||||
if (this.createAlterTableMethods) {
|
||||
this.alterTableForCreate(columnTypes);
|
||||
}
|
||||
this.createQuery(columnTypes, ifNot);
|
||||
this.columnQueries(columns);
|
||||
delete this.single.comment;
|
||||
@ -45,14 +49,14 @@ TableCompiler.prototype.create = function(ifNot) {
|
||||
};
|
||||
|
||||
// Only create the table if it doesn't exist.
|
||||
TableCompiler.prototype.createIfNot = function() {
|
||||
TableCompiler.prototype.createIfNot = function () {
|
||||
this.create(true);
|
||||
};
|
||||
|
||||
// If we're altering the table, we need to one-by-one
|
||||
// go through and handle each of the queries associated
|
||||
// with altering the table's schema.
|
||||
TableCompiler.prototype.alter = function() {
|
||||
TableCompiler.prototype.alter = function () {
|
||||
var columns = this.getColumns();
|
||||
var columnTypes = this.getColumnTypes(columns);
|
||||
this.addColumns(columnTypes);
|
||||
@ -60,36 +64,36 @@ TableCompiler.prototype.alter = function() {
|
||||
this.alterTable();
|
||||
};
|
||||
|
||||
TableCompiler.prototype.foreign = function(foreignData) {
|
||||
TableCompiler.prototype.foreign = function (foreignData) {
|
||||
if (foreignData.inTable && foreignData.references) {
|
||||
var keyName = this._indexCommand('foreign', this.tableNameRaw, foreignData.column);
|
||||
var column = this.formatter.columnize(foreignData.column);
|
||||
var keyName = this._indexCommand('foreign', this.tableNameRaw, foreignData.column);
|
||||
var column = this.formatter.columnize(foreignData.column);
|
||||
var references = this.formatter.columnize(foreignData.references);
|
||||
var inTable = this.formatter.wrap(foreignData.inTable);
|
||||
var onUpdate = foreignData.onUpdate ? (this.lowerCase ? ' on update ' : ' ON UPDATE ') + foreignData.onUpdate : '';
|
||||
var onDelete = foreignData.onDelete ? (this.lowerCase ? ' on delete ' : ' ON DELETE ') + foreignData.onDelete : '';
|
||||
var inTable = this.formatter.wrap(foreignData.inTable);
|
||||
var onUpdate = foreignData.onUpdate ? (this.lowerCase ? ' on update ' : ' ON UPDATE ') + foreignData.onUpdate : '';
|
||||
var onDelete = foreignData.onDelete ? (this.lowerCase ? ' on delete ' : ' ON DELETE ') + foreignData.onDelete : '';
|
||||
if (this.lowerCase) {
|
||||
this.pushQuery('alter table ' + this.tableName() + ' add constraint ' + keyName + ' ' +
|
||||
this.pushQuery((!this.forCreate ? 'alter table ' + this.tableName() + ' add ' : '') + 'constraint ' + keyName + ' ' +
|
||||
'foreign key (' + column + ') references ' + inTable + ' (' + references + ')' + onUpdate + onDelete);
|
||||
} else {
|
||||
this.pushQuery('ALTER TABLE ' + this.tableName() + ' ADD CONSTRAINT ' + keyName + ' ' +
|
||||
this.pushQuery((!this.forCreate ? 'ALTER TABLE ' + this.tableName() + ' ADD ' : '') + 'CONSTRAINT ' + keyName + ' ' +
|
||||
'FOREIGN KEY (' + column + ') REFERENCES ' + inTable + ' (' + references + ')' + onUpdate + onDelete);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Get all of the column sql & bindings individually for building the table queries.
|
||||
TableCompiler.prototype.getColumnTypes = function(columns) {
|
||||
return _.reduce(_.map(columns, _.first), function(memo, column) {
|
||||
TableCompiler.prototype.getColumnTypes = function (columns) {
|
||||
return _.reduce(_.map(columns, _.first), function (memo, column) {
|
||||
memo.sql.push(column.sql);
|
||||
memo.bindings.concat(column.bindings);
|
||||
return memo;
|
||||
}, {sql: [], bindings: []});
|
||||
}, { sql: [], bindings: [] });
|
||||
};
|
||||
|
||||
// Adds all of the additional queries from the "column"
|
||||
TableCompiler.prototype.columnQueries = function(columns) {
|
||||
var queries = _.reduce(_.map(columns, _.rest), function(memo, column) {
|
||||
TableCompiler.prototype.columnQueries = function (columns) {
|
||||
var queries = _.reduce(_.map(columns, _.rest), function (memo, column) {
|
||||
if (!_.isEmpty(column)) return memo.concat(column);
|
||||
return memo;
|
||||
}, []);
|
||||
@ -102,9 +106,9 @@ TableCompiler.prototype.columnQueries = function(columns) {
|
||||
TableCompiler.prototype.addColumnsPrefix = 'add column ';
|
||||
|
||||
// All of the columns to "add" for the query
|
||||
TableCompiler.prototype.addColumns = function(columns) {
|
||||
TableCompiler.prototype.addColumns = function (columns) {
|
||||
if (columns.sql.length > 0) {
|
||||
var columnSql = _.map(columns.sql, function(column) {
|
||||
var columnSql = _.map(columns.sql, function (column) {
|
||||
return this.addColumnsPrefix + column;
|
||||
}, this);
|
||||
this.pushQuery({
|
||||
@ -115,15 +119,15 @@ TableCompiler.prototype.addColumns = function(columns) {
|
||||
};
|
||||
|
||||
// Compile the columns as needed for the current create or alter table
|
||||
TableCompiler.prototype.getColumns = function() {
|
||||
TableCompiler.prototype.getColumns = function () {
|
||||
var i = -1, compiledColumns = [], columns = this.grouped.columns || [];
|
||||
while(++i < columns.length) {
|
||||
while (++i < columns.length) {
|
||||
compiledColumns.push(this.client.columnCompiler(this, columns[i].builder).toSQL())
|
||||
}
|
||||
return compiledColumns;
|
||||
};
|
||||
|
||||
TableCompiler.prototype.tableName = function() {
|
||||
TableCompiler.prototype.tableName = function () {
|
||||
var name = this.schemaNameRaw ?
|
||||
`${this.schemaNameRaw}.${this.tableNameRaw}`
|
||||
: this.tableNameRaw;
|
||||
@ -132,7 +136,7 @@ TableCompiler.prototype.tableName = function() {
|
||||
};
|
||||
|
||||
// Generate all of the alter column statements necessary for the query.
|
||||
TableCompiler.prototype.alterTable = function() {
|
||||
TableCompiler.prototype.alterTable = function () {
|
||||
var alterTable = this.grouped.alterTable || [];
|
||||
for (var i = 0, l = alterTable.length; i < l; i++) {
|
||||
var statement = alterTable[i];
|
||||
@ -147,21 +151,45 @@ TableCompiler.prototype.alterTable = function() {
|
||||
}
|
||||
};
|
||||
|
||||
TableCompiler.prototype.alterTableForCreate = function (columnTypes) {
|
||||
this.forCreate = true;
|
||||
var savedSequence = this.sequence;
|
||||
var alterTable = this.grouped.alterTable || [];
|
||||
this.grouped.alterTable = [];
|
||||
for (var i = 0, l = alterTable.length; i < l; i++) {
|
||||
var statement = alterTable[i];
|
||||
if (_.indexOf(this.createAlterTableMethods, statement.method) < 0) {
|
||||
this.grouped.alterTable.push(statement);
|
||||
continue;
|
||||
}
|
||||
if (this[statement.method]) {
|
||||
this.sequence = [];
|
||||
this[statement.method].apply(this, statement.args);
|
||||
columnTypes.sql.push(this.sequence[0].sql);
|
||||
} else {
|
||||
console.error('Debug: ' + statement.method + ' does not exist');
|
||||
}
|
||||
}
|
||||
this.sequence = savedSequence;
|
||||
this.forCreate = false;
|
||||
};
|
||||
|
||||
|
||||
// Drop the index on the current table.
|
||||
TableCompiler.prototype.dropIndex = function(value) {
|
||||
TableCompiler.prototype.dropIndex = function (value) {
|
||||
this.pushQuery('drop index' + value);
|
||||
};
|
||||
|
||||
// Drop the unique
|
||||
TableCompiler.prototype.dropUnique =
|
||||
TableCompiler.prototype.dropForeign = function() {
|
||||
TableCompiler.prototype.dropForeign = function () {
|
||||
throw new Error('Method implemented in the dialect driver');
|
||||
};
|
||||
|
||||
TableCompiler.prototype.dropColumnPrefix = 'drop column ';
|
||||
TableCompiler.prototype.dropColumn = function() {
|
||||
TableCompiler.prototype.dropColumn = function () {
|
||||
var columns = normalizeArr.apply(null, arguments);
|
||||
var drops = _.map(_.isArray(columns) ? columns : [columns], function(column) {
|
||||
var drops = _.map(_.isArray(columns) ? columns : [columns], function (column) {
|
||||
return this.dropColumnPrefix + this.formatter.wrap(column);
|
||||
}, this);
|
||||
this.pushQuery((this.lowerCase ? 'alter table ' : 'ALTER TABLE ') + this.tableName() + ' ' + drops.join(', '));
|
||||
@ -170,7 +198,7 @@ TableCompiler.prototype.dropColumn = function() {
|
||||
// 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.
|
||||
TableCompiler.prototype._indexCommand = function(type, tableName, columns) {
|
||||
TableCompiler.prototype._indexCommand = function (type, tableName, columns) {
|
||||
if (!_.isArray(columns)) columns = columns ? [columns] : [];
|
||||
var table = tableName.replace(/\.|-/g, '_');
|
||||
return (table + '_' + columns.join('_') + '_' + type).toLowerCase();
|
||||
|
||||
@ -88,9 +88,8 @@ module.exports = function(knex) {
|
||||
'create index "NkZo/dGRI9O73/NE2fHo+35d4jk" on "test_table_one" ("first_name")',
|
||||
'alter table "test_table_one" add constraint "test_table_one_email_unique" unique ("email")',
|
||||
'create index "test_table_one_logins_index" on "test_table_one" ("logins")']);
|
||||
tester('mssql', ['CREATE TABLE [test_table_one] ([id] bigint identity(1,1) not null primary key, [first_name] nvarchar(255), [last_name] nvarchar(255), [email] nvarchar(255) null, [logins] int default \'1\', [about] nvarchar(max), [created_at] datetime, [updated_at] datetime)',
|
||||
tester('mssql', ['CREATE TABLE [test_table_one] ([id] bigint identity(1,1) not null primary key, [first_name] nvarchar(255), [last_name] nvarchar(255), [email] nvarchar(255) null, [logins] int default \'1\', [about] nvarchar(max), [created_at] datetime, [updated_at] datetime, CONSTRAINT test_table_one_email_unique UNIQUE ([email]))',
|
||||
'CREATE INDEX test_table_one_first_name_index ON [test_table_one] ([first_name])',
|
||||
'CREATE UNIQUE INDEX test_table_one_email_unique ON [test_table_one] ([email])',
|
||||
'CREATE INDEX test_table_one_logins_index ON [test_table_one] ([logins])']);
|
||||
});
|
||||
});
|
||||
@ -126,7 +125,7 @@ module.exports = function(knex) {
|
||||
tester('pg', ['create table "test_table_three" ("main" integer not null, "paragraph" text default \'Lorem ipsum Qui quis qui in.\')','alter table "test_table_three" add primary key ("main")']);
|
||||
tester('sqlite3', ['create table "test_table_three" ("main" integer not null, "paragraph" text default \'Lorem ipsum Qui quis qui in.\', primary key ("main"))']);
|
||||
tester('oracle', ['create table "test_table_three" ("main" integer not null, "paragraph" clob default \'Lorem ipsum Qui quis qui in.\')','alter table "test_table_three" add primary key ("main")']);
|
||||
tester('mssql', ['CREATE TABLE [test_table_three] ([main] int not null, [paragraph] nvarchar(max))','ALTER TABLE [test_table_three] ADD PRIMARY KEY ([main])']);
|
||||
tester('mssql', ['CREATE TABLE [test_table_three] ([main] int not null, [paragraph] nvarchar(max), CONSTRAINT test_table_three_main_primary PRIMARY KEY ([main]))']);
|
||||
});
|
||||
});
|
||||
|
||||
@ -161,7 +160,7 @@ module.exports = function(knex) {
|
||||
"create or replace trigger \"test_foreign_table_two_id_trg\" before insert on \"test_foreign_table_two\" for each row when (new.\"id\" is null) begin select \"test_foreign_table_two_seq\".nextval into :new.\"id\" from dual; end;",
|
||||
'alter table "test_foreign_table_two" add constraint "q7TfvbIx3HUQbh+l+e5N+J+Guag" foreign key ("fkey_two") references "test_table_two" ("id")'
|
||||
]);
|
||||
tester('mssql', ['CREATE TABLE [test_foreign_table_two] ([id] int identity(1,1) not null primary key, [fkey_two] int)','ALTER TABLE [test_foreign_table_two] ADD CONSTRAINT test_foreign_table_two_fkey_two_foreign FOREIGN KEY ([fkey_two]) REFERENCES [test_table_two] ([id])']);
|
||||
tester('mssql', ['CREATE TABLE [test_foreign_table_two] ([id] int identity(1,1) not null primary key, [fkey_two] int, CONSTRAINT test_foreign_table_two_fkey_two_foreign FOREIGN KEY ([fkey_two]) REFERENCES [test_table_two] ([id]))']);
|
||||
});
|
||||
});
|
||||
|
||||
@ -178,7 +177,7 @@ module.exports = function(knex) {
|
||||
tester('pg', ['create table "composite_key_test" ("column_a" integer, "column_b" integer, "details" text, "status" smallint)','alter table "composite_key_test" add constraint composite_key_test_column_a_column_b_unique unique ("column_a", "column_b")']);
|
||||
tester('sqlite3', ['create table "composite_key_test" ("column_a" integer, "column_b" integer, "details" text, "status" tinyint)','create unique index composite_key_test_column_a_column_b_unique on "composite_key_test" ("column_a", "column_b")']);
|
||||
tester('oracle', ['create table "composite_key_test" ("column_a" integer, "column_b" integer, "details" clob, "status" smallint)','alter table "composite_key_test" add constraint "zYmMt0VQwlLZ20XnrMicXZ0ufZk" unique ("column_a", "column_b")']);
|
||||
tester('mssql', ['CREATE TABLE [composite_key_test] ([column_a] int, [column_b] int, [details] nvarchar(max), [status] tinyint)','CREATE UNIQUE INDEX composite_key_test_column_a_column_b_unique ON [composite_key_test] ([column_a], [column_b])']);
|
||||
tester('mssql', ['CREATE TABLE [composite_key_test] ([column_a] int, [column_b] int, [details] nvarchar(max), [status] tinyint, CONSTRAINT composite_key_test_column_a_column_b_unique UNIQUE ([column_a], [column_b]))']);
|
||||
}).then(function() {
|
||||
return knex('composite_key_test').insert([{
|
||||
column_a: 1,
|
||||
|
||||
@ -123,7 +123,7 @@ var testConfigs = {
|
||||
connection: testConfig.mssql || {
|
||||
user: "knex_test",
|
||||
password: "knex_test",
|
||||
server: "127.0.0.1",
|
||||
server: "DEGD02.degdarwin.com",//127.0.0.1",
|
||||
database: "knex_test"
|
||||
},
|
||||
pool: pool,
|
||||
|
||||
@ -89,7 +89,7 @@ describe("MSSQL SchemaBuilder", function() {
|
||||
}).toSQL();
|
||||
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('DROP INDEX users_foo_unique ON [users]');
|
||||
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] DROP CONSTRAINT users_foo_unique');
|
||||
});
|
||||
|
||||
it('test drop unique, custom', function() {
|
||||
@ -98,7 +98,7 @@ describe("MSSQL SchemaBuilder", function() {
|
||||
}).toSQL();
|
||||
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('DROP INDEX foo ON [users]');
|
||||
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] DROP CONSTRAINT foo');
|
||||
});
|
||||
|
||||
it('test drop index', function() {
|
||||
@ -196,9 +196,8 @@ describe("MSSQL SchemaBuilder", function() {
|
||||
table.integer('user_id').notNull().references('users.id').onDelete('SET NULL');
|
||||
table.integer('account_id').notNull().references('id').inTable('accounts').onUpdate('cascade');
|
||||
}).toSQL();
|
||||
equal(3, tableSql.length);
|
||||
expect(tableSql[1].sql).to.equal('ALTER TABLE [person] ADD CONSTRAINT person_user_id_foreign FOREIGN KEY ([user_id]) REFERENCES [users] ([id]) ON DELETE SET NULL');
|
||||
expect(tableSql[2].sql).to.equal('ALTER TABLE [person] ADD CONSTRAINT person_account_id_foreign FOREIGN KEY ([account_id]) REFERENCES [accounts] ([id]) ON UPDATE cascade');
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('CREATE TABLE [person] ([user_id] int not null, [account_id] int not null, CONSTRAINT person_user_id_foreign FOREIGN KEY ([user_id]) REFERENCES [users] ([id]) ON DELETE SET NULL, CONSTRAINT person_account_id_foreign FOREIGN KEY ([account_id]) REFERENCES [accounts] ([id]) ON UPDATE cascade)');
|
||||
});
|
||||
|
||||
it('test adding incrementing id', function() {
|
||||
@ -458,7 +457,7 @@ describe("MSSQL SchemaBuilder", function() {
|
||||
}).toSQL();
|
||||
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('DROP INDEX composite_key_test_column_a_column_b_unique ON [composite_key_test]');
|
||||
expect(tableSql[0].sql).to.equal('ALTER TABLE [composite_key_test] DROP CONSTRAINT composite_key_test_column_a_column_b_unique');
|
||||
});
|
||||
|
||||
it('allows default as alias for defaultTo', function() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user