2016-05-17 01:01:34 +10:00
|
|
|
/* eslint max-len:0 no-console:0*/
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// MySQL Table Builder & Compiler
|
|
|
|
// -------
|
2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
|
|
|
import TableCompiler from '../../../schema/tablecompiler';
|
|
|
|
import * as helpers from '../../../helpers';
|
|
|
|
import Promise from '../../../promise';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-18 20:22:50 +10:00
|
|
|
import { assign } from 'lodash'
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Table Compiler
|
|
|
|
// ------
|
|
|
|
|
|
|
|
function TableCompiler_MySQL() {
|
|
|
|
TableCompiler.apply(this, arguments);
|
|
|
|
}
|
|
|
|
inherits(TableCompiler_MySQL, TableCompiler);
|
|
|
|
|
|
|
|
assign(TableCompiler_MySQL.prototype, {
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
createQuery(columns, ifNot) {
|
|
|
|
const createStatement = ifNot ? 'create table if not exists ' : 'create table ';
|
|
|
|
const { client } = this;
|
|
|
|
let conn = {};
|
|
|
|
let sql = createStatement + this.tableName() + ' (' + columns.sql.join(', ') + ')';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Check if the connection settings are set.
|
|
|
|
if (client.connectionSettings) {
|
|
|
|
conn = client.connectionSettings;
|
|
|
|
}
|
|
|
|
|
2016-05-18 19:59:24 +10:00
|
|
|
const charset = this.single.charset || conn.charset || '';
|
2016-05-17 01:01:34 +10:00
|
|
|
const collation = this.single.collate || conn.collate || '';
|
2016-05-18 19:59:24 +10:00
|
|
|
const engine = this.single.engine || '';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// var conn = builder.client.connectionSettings;
|
2016-05-17 01:01:34 +10:00
|
|
|
if (charset) sql += ` default character set ${charset}`;
|
|
|
|
if (collation) sql += ` collate ${collation}`;
|
|
|
|
if (engine) sql += ` engine = ${engine}`;
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
if (this.single.comment) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const comment = (this.single.comment || '');
|
2016-03-02 17:07:05 +01:00
|
|
|
if (comment.length > 60) helpers.warn('The max length for a table comment is 60 characters');
|
2016-05-17 01:01:34 +10:00
|
|
|
sql += ` comment = '${comment}'`;
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.pushQuery(sql);
|
|
|
|
},
|
|
|
|
|
|
|
|
addColumnsPrefix: 'add ',
|
|
|
|
|
|
|
|
dropColumnPrefix: 'drop ',
|
|
|
|
|
|
|
|
// Compiles the comment on the table.
|
2016-05-17 01:01:34 +10:00
|
|
|
comment(comment) {
|
|
|
|
this.pushQuery(`alter table ${this.tableName()} comment = '${comment}'`);
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
changeType() {
|
2016-03-02 17:07:05 +01:00
|
|
|
// alter table + table + ' modify ' + wrapped + '// type';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Renames a column on the table.
|
2016-05-17 01:01:34 +10:00
|
|
|
renameColumn(from, to) {
|
|
|
|
const compiler = this;
|
2016-05-18 19:59:24 +10:00
|
|
|
const table = this.tableName();
|
|
|
|
const wrapped = this.formatter.wrap(from) + ' ' + this.formatter.wrap(to);
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
this.pushQuery({
|
2016-05-17 01:01:34 +10:00
|
|
|
sql: `show fields from ${table} where field = ` +
|
2016-03-02 17:07:05 +01:00
|
|
|
this.formatter.parameter(from),
|
2016-05-17 01:01:34 +10:00
|
|
|
output(resp) {
|
|
|
|
const column = resp[0];
|
|
|
|
const runner = this;
|
2016-03-02 17:07:05 +01:00
|
|
|
return compiler.getFKRefs(runner).get(0)
|
2016-05-17 01:01:34 +10:00
|
|
|
.then(refs =>
|
|
|
|
Promise.try(function () {
|
2016-03-02 17:07:05 +01:00
|
|
|
if (!refs.length) { return; }
|
|
|
|
return compiler.dropFKRefs(runner, refs);
|
|
|
|
}).then(function () {
|
2016-04-01 23:28:15 +02:00
|
|
|
let sql = `alter table ${table} change ${wrapped} ${column.Type}`;
|
|
|
|
|
|
|
|
if(String(column.Null).toUpperCase() !== 'YES') {
|
|
|
|
sql += ` NOT NULL`
|
|
|
|
}
|
|
|
|
if(column.Default !== void 0 && column.Default !== null) {
|
|
|
|
sql += ` DEFAULT '${column.Default}'`
|
|
|
|
}
|
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
return runner.query({
|
2016-05-17 01:01:34 +10:00
|
|
|
sql
|
2016-03-02 17:07:05 +01: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;
|
|
|
|
}));
|
2016-05-17 01:01:34 +10:00
|
|
|
})
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
getFKRefs (runner) {
|
|
|
|
const formatter = this.client.formatter();
|
|
|
|
const sql = 'SELECT KCU.CONSTRAINT_NAME, KCU.TABLE_NAME, KCU.COLUMN_NAME, '+
|
2016-03-02 17:07:05 +01:00
|
|
|
' 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()) + ' '+
|
|
|
|
' AND RC.CONSTRAINT_SCHEMA = ' + formatter.parameter(this.client.database());
|
|
|
|
|
|
|
|
return runner.query({
|
2016-05-17 01:01:34 +10:00
|
|
|
sql,
|
2016-03-02 17:07:05 +01:00
|
|
|
bindings: formatter.bindings
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
dropFKRefs (runner, refs) {
|
|
|
|
const formatter = this.client.formatter();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
return Promise.all(refs.map(function (ref) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const constraintName = formatter.wrap(ref.CONSTRAINT_NAME);
|
2016-05-18 19:59:24 +10:00
|
|
|
const tableName = formatter.wrap(ref.TABLE_NAME);
|
2016-03-02 17:07:05 +01:00
|
|
|
return runner.query({
|
2016-05-17 01:01:34 +10:00
|
|
|
sql: `alter table ${tableName} drop foreign key ${constraintName}`
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
}));
|
|
|
|
},
|
2016-05-17 01:01:34 +10:00
|
|
|
createFKRefs (runner, refs) {
|
|
|
|
const formatter = this.client.formatter();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
return Promise.all(refs.map(function (ref) {
|
2016-05-18 19:59:24 +10:00
|
|
|
const tableName = formatter.wrap(ref.TABLE_NAME);
|
|
|
|
const keyName = formatter.wrap(ref.CONSTRAINT_NAME);
|
|
|
|
const column = formatter.columnize(ref.COLUMN_NAME);
|
2016-05-17 01:01:34 +10:00
|
|
|
const references = formatter.columnize(ref.REFERENCED_COLUMN_NAME);
|
2016-05-18 19:59:24 +10:00
|
|
|
const inTable = formatter.wrap(ref.REFERENCED_TABLE_NAME);
|
|
|
|
const onUpdate = ` ON UPDATE ${ref.UPDATE_RULE}`;
|
|
|
|
const onDelete = ` ON DELETE ${ref.DELETE_RULE}`;
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
return runner.query({
|
2016-05-17 01:01:34 +10:00
|
|
|
sql: `alter table ${tableName} add constraint ${keyName} ` +
|
2016-03-02 17:07:05 +01:00
|
|
|
'foreign key (' + column + ') references ' + inTable + ' (' + references + ')' + onUpdate + onDelete
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
},
|
2016-05-17 01:01:34 +10:00
|
|
|
index(columns, indexName) {
|
2015-07-15 23:16:30 -03:00
|
|
|
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`alter table ${this.tableName()} add index ${indexName}(${this.formatter.columnize(columns)})`);
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-19 21:30:17 +02:00
|
|
|
primary(columns, constraintName) {
|
|
|
|
constraintName = constraintName ? this.formatter.wrap(constraintName) : this._indexCommand('primary', this.tableNameRaw, columns);
|
|
|
|
this.pushQuery(`alter table ${this.tableName()} add primary key ${constraintName}(${this.formatter.columnize(columns)})`);
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
unique(columns, indexName) {
|
2015-07-15 23:16:30 -03:00
|
|
|
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, columns);
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`alter table ${this.tableName()} add unique ${indexName}(${this.formatter.columnize(columns)})`);
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a drop index command.
|
2016-05-17 01:01:34 +10:00
|
|
|
dropIndex(columns, indexName) {
|
2015-07-15 23:16:30 -03:00
|
|
|
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`alter table ${this.tableName()} drop index ${indexName}`);
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a drop foreign key command.
|
2016-05-17 01:01:34 +10:00
|
|
|
dropForeign(columns, indexName) {
|
2015-07-15 23:16:30 -03:00
|
|
|
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('foreign', this.tableNameRaw, columns);
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`alter table ${this.tableName()} drop foreign key ${indexName}`);
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a drop primary key command.
|
2016-05-17 01:01:34 +10:00
|
|
|
dropPrimary() {
|
|
|
|
this.pushQuery(`alter table ${this.tableName()} drop primary key`);
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a drop unique key command.
|
2016-05-17 01:01:34 +10:00
|
|
|
dropUnique(column, indexName) {
|
2015-07-15 23:16:30 -03:00
|
|
|
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, column);
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`alter table ${this.tableName()} drop index ${indexName}`);
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default TableCompiler_MySQL;
|