knex/lib/dialects/mysql/schema/tablecompiler.js

179 lines
6.7 KiB
JavaScript
Raw Normal View History

2014-09-01 16:56:30 +02:00
'use strict';
// MySQL Table Builder & Compiler
// -------
2015-05-09 13:58:18 -04:00
var inherits = require('inherits');
var TableCompiler = require('../../../schema/tablecompiler');
2015-05-09 13:58:18 -04:00
var helpers = require('../../../helpers');
var Promise = require('../../../promise');
var assign = require('lodash/object/assign');
// Table Compiler
// ------
function TableCompiler_MySQL() {
TableCompiler.apply(this, arguments);
}
inherits(TableCompiler_MySQL, TableCompiler);
assign(TableCompiler_MySQL.prototype, {
2015-05-09 13:58:18 -04:00
createQuery: function createQuery(columns, ifNot) {
var createStatement = ifNot ? 'create table if not exists ' : 'create table ';
2015-05-09 13:58:18 -04:00
var client = this.client,
conn = {},
sql = createStatement + this.tableName() + ' (' + columns.sql.join(', ') + ')';
// Check if the connection settings are set.
if (client.connectionSettings) {
conn = client.connectionSettings;
}
2015-05-09 13:58:18 -04:00
var charset = this.single.charset || conn.charset || '';
var collation = this.single.collate || conn.collate || '';
2015-05-09 13:58:18 -04:00
var engine = this.single.engine || '';
// var conn = builder.client.connectionSettings;
2015-05-09 13:58:18 -04:00
if (charset) sql += ' default character set ' + charset;
if (collation) sql += ' collate ' + collation;
2015-05-09 13:58:18 -04:00
if (engine) sql += ' engine = ' + engine;
if (this.single.comment) {
2015-05-09 13:58:18 -04:00
var comment = this.single.comment || '';
if (comment.length > 60) helpers.warn('The max length for a table comment is 60 characters');
2015-05-09 13:58:18 -04:00
sql += ' comment = \'' + comment + '\'';
}
this.pushQuery(sql);
},
addColumnsPrefix: 'add ',
2015-05-09 13:58:18 -04:00
dropColumnPrefix: 'drop ',
// Compiles the comment on the table.
2015-05-09 13:58:18 -04:00
comment: function comment(_comment) {
this.pushQuery('alter table ' + this.tableName() + ' comment = \'' + _comment + '\'');
},
2015-05-09 13:58:18 -04:00
changeType: function changeType() {},
// Renames a column on the table.
2015-05-09 13:58:18 -04:00
renameColumn: function renameColumn(from, to) {
var compiler = this;
2015-05-09 13:58:18 -04:00
var table = this.tableName();
var wrapped = this.formatter.wrap(from) + ' ' + this.formatter.wrap(to);
this.pushQuery({
2015-05-09 13:58:18 -04:00
sql: 'show fields from ' + table + ' where field = ' + this.formatter.parameter(from),
output: function output(resp) {
var column = resp[0];
var runner = this;
2015-05-09 13:58:18 -04:00
return compiler.getFKRefs(runner).get(0).then(function (refs) {
return Promise['try'](function () {
if (!refs.length) {
return;
}
return compiler.dropFKRefs(runner, refs);
}).then(function () {
return runner.query({
sql: 'alter table ' + table + ' change ' + wrapped + ' ' + column.Type
});
2015-05-09 13:58:18 -04:00
}).then(function () {
if (!refs.length) {
return;
}
return compiler.createFKRefs(runner, refs.map(function (ref) {
if (ref.REFERENCED_COLUMN_NAME === from) {
ref.REFERENCED_COLUMN_NAME = to;
}
if (ref.COLUMN_NAME === from) {
ref.COLUMN_NAME = to;
}
return ref;
}));
});
2015-05-09 13:58:18 -04:00
});
}
});
},
2015-05-09 13:58:18 -04:00
getFKRefs: function getFKRefs(runner) {
var formatter = this.client.formatter();
2015-05-09 13:58:18 -04:00
var sql = 'SELECT KCU.CONSTRAINT_NAME, KCU.TABLE_NAME, KCU.COLUMN_NAME, ' + ' KCU.REFERENCED_TABLE_NAME, KCU.REFERENCED_COLUMN_NAME, ' + ' RC.UPDATE_RULE, RC.DELETE_RULE ' + 'FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU ' + 'JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS RC ' + ' USING(CONSTRAINT_NAME)' + 'WHERE KCU.REFERENCED_TABLE_NAME = ' + formatter.parameter(this.tableNameRaw) + ' ' + ' AND KCU.CONSTRAINT_SCHEMA = ' + formatter.parameter(this.client.database());
return runner.query({
sql: sql,
bindings: formatter.bindings
});
},
2015-05-09 13:58:18 -04:00
dropFKRefs: function dropFKRefs(runner, refs) {
var formatter = this.client.formatter();
2015-05-09 13:58:18 -04:00
return Promise.all(refs.map((function (ref) {
var constraintName = formatter.wrap(ref.CONSTRAINT_NAME);
return runner.query({
sql: 'alter table ' + this.tableName() + ' drop foreign key ' + constraintName
});
2015-05-09 13:58:18 -04:00
}).bind(this)));
},
2015-05-09 13:58:18 -04:00
createFKRefs: function createFKRefs(runner, refs) {
var formatter = this.client.formatter();
2015-05-09 13:58:18 -04:00
return Promise.all(refs.map((function (ref) {
var keyName = formatter.wrap(ref.COLUMN_NAME);
var column = formatter.columnize(ref.COLUMN_NAME);
var references = formatter.columnize(ref.REFERENCED_COLUMN_NAME);
2015-05-09 13:58:18 -04:00
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({
2015-05-09 13:58:18 -04:00
sql: 'alter table ' + this.tableName() + ' add constraint ' + keyName + ' ' + 'foreign key (' + column + ') references ' + inTable + ' (' + references + ')' + onUpdate + onDelete
});
2015-05-09 13:58:18 -04:00
}).bind(this)));
},
2015-05-09 13:58:18 -04:00
index: function index(columns, indexName) {
indexName = indexName || this._indexCommand('index', this.tableNameRaw, columns);
2015-05-09 13:58:18 -04:00
this.pushQuery('alter table ' + this.tableName() + ' add index ' + indexName + '(' + this.formatter.columnize(columns) + ')');
},
2015-05-09 13:58:18 -04:00
primary: function primary(columns, indexName) {
indexName = indexName || this._indexCommand('primary', this.tableNameRaw, columns);
2015-05-09 13:58:18 -04:00
this.pushQuery('alter table ' + this.tableName() + ' add primary key ' + indexName + '(' + this.formatter.columnize(columns) + ')');
},
2015-05-09 13:58:18 -04:00
unique: function unique(columns, indexName) {
indexName = indexName || this._indexCommand('unique', this.tableNameRaw, columns);
2015-05-09 13:58:18 -04:00
this.pushQuery('alter table ' + this.tableName() + ' add unique ' + indexName + '(' + this.formatter.columnize(columns) + ')');
},
// Compile a drop index command.
2015-05-09 13:58:18 -04:00
dropIndex: function dropIndex(columns, indexName) {
indexName = indexName || this._indexCommand('index', this.tableNameRaw, columns);
this.pushQuery('alter table ' + this.tableName() + ' drop index ' + indexName);
},
// Compile a drop foreign key command.
2015-05-09 13:58:18 -04:00
dropForeign: function dropForeign(columns, indexName) {
indexName = indexName || this._indexCommand('foreign', this.tableNameRaw, columns);
this.pushQuery('alter table ' + this.tableName() + ' drop foreign key ' + indexName);
},
// Compile a drop primary key command.
2015-05-09 13:58:18 -04:00
dropPrimary: function dropPrimary() {
this.pushQuery('alter table ' + this.tableName() + ' drop primary key');
},
// Compile a drop unique key command.
2015-05-09 13:58:18 -04:00
dropUnique: function dropUnique(column, indexName) {
indexName = indexName || this._indexCommand('unique', this.tableNameRaw, column);
this.pushQuery('alter table ' + this.tableName() + ' drop index ' + indexName);
}
2015-05-09 13:58:18 -04:00
});
module.exports = TableCompiler_MySQL;
2015-05-09 13:58:18 -04:00
// alter table + table + ' modify ' + wrapped + '// type';