不要怂,就是干,撸起袖子干!

Commit c4b5f2af by Jan Aagaard Meier

bug(index) Change addIndex to ALTER TABLE in mysql. Closes #3600

1 parent ce84e892
......@@ -46,6 +46,7 @@ AbstractDialect.prototype.supports = {
using: true,
},
joinTableDependent: true,
indexViaAlter: false,
JSON: false,
};
......
......@@ -548,19 +548,35 @@ module.exports = (function() {
tableName = this.quoteTable(tableName);
}
return Utils._.compact([
'CREATE',
var concurrently = this._dialect.supports.index.concurrently && options.concurrently ? 'CONCURRENTLY' : undefined
, ind;
if (this._dialect.supports.indexViaAlter) {
ind = [
'ALTER TABLE',
tableName,
concurrently,
'ADD'
];
} else {
ind = ['CREATE'];
}
ind = ind.concat(
options.unique ? 'UNIQUE' : '',
options.type, 'INDEX',
this._dialect.supports.index.concurrently && options.concurrently ? 'CONCURRENTLY' : undefined,
!this._dialect.supports.indexViaAlter ? concurrently : undefined,
this.quoteIdentifiers(options.name),
this._dialect.supports.index.using === 1 && options.using ? 'USING ' + options.using : '',
'ON', tableName,
!this._dialect.supports.indexViaAlter ? 'ON ' + tableName : undefined,
this._dialect.supports.index.using === 2 && options.using ? 'USING ' + options.using : '',
'(' + fieldsSql.join(', ') + (options.operator ? ' '+options.operator : '') + ')',
(this._dialect.supports.index.parser && options.parser ? 'WITH PARSER ' + options.parser : undefined),
(this._dialect.supports.index.where && options.where ? options.where : undefined)
]).join(' ');
);
return Utils._.compact(ind).join(' ');
},
/*
......
......@@ -31,6 +31,7 @@ MysqlDialect.prototype.supports = _.merge(_.cloneDeep(Abstract.prototype.support
type: true,
using: 1,
},
indexViaAlter: true,
NUMERIC: true
});
......
......@@ -506,46 +506,6 @@ if (Support.dialectIsMySQL()) {
}
],
addIndexQuery: [
{
arguments: ['User', ['username', 'isAdmin'], {}, 'User'],
expectation: 'CREATE INDEX `user_username_is_admin` ON `User` (`username`, `isAdmin`)'
}, {
arguments: [
'User', [
{ attribute: 'username', length: 10, order: 'ASC'},
'isAdmin'
],
{},
'User'
],
expectation: 'CREATE INDEX `user_username_is_admin` ON `User` (`username`(10) ASC, `isAdmin`)'
}, {
arguments: [
'User', ['username', 'isAdmin'], { parser: 'foo', indicesType: 'FULLTEXT', indexName: 'bar'}, 'User'
],
expectation: 'CREATE FULLTEXT INDEX `bar` ON `User` (`username`, `isAdmin`) WITH PARSER foo'
}, {
arguments: [
'User', ['username', 'isAdmin'], { indicesType: 'UNIQUE'}, 'User'
],
expectation: 'CREATE UNIQUE INDEX `user_username_is_admin` ON `User` (`username`, `isAdmin`)'
}, {
arguments: ['User', ['fieldB', {attribute: 'fieldA', collate: 'en_US', order: 'DESC', length: 5}], {
name: 'a_b_uniq',
unique: true,
method: 'BTREE'
}, 'User'],
expectation: 'CREATE UNIQUE INDEX `a_b_uniq` USING BTREE ON `User` (`fieldB`, `fieldA`(5) DESC)'
}, {
arguments: ['User', ['fieldC'], {
type: 'FULLTEXT',
concurrently: true
}, 'User'],
expectation: 'CREATE FULLTEXT INDEX `user_field_c` ON `User` (`fieldC`)'
}
],
showIndexesQuery: [
{
arguments: ['User'],
......
......@@ -851,68 +851,6 @@ if (dialect.match(/^postgres/)) {
}
],
addIndexQuery: [
{
arguments: ['User', ['username', 'isAdmin'], {}, 'User'],
expectation: 'CREATE INDEX \"user_username_is_admin\" ON \"User\" (\"username\", \"isAdmin\")'
}, {
arguments: [
'User', [
{ attribute: 'username', length: 10, order: 'ASC'},
'isAdmin'
],
{},
'User'
],
expectation: 'CREATE INDEX \"user_username_is_admin\" ON \"User\" (\"username\" ASC, \"isAdmin\")'
}, {
arguments: ['User', ['username', 'isAdmin'], { indexName: 'bar'}, 'User'],
expectation: 'CREATE INDEX \"bar\" ON \"User\" (\"username\", \"isAdmin\")'
}, {
arguments: ['mySchema.User', ['username', 'isAdmin'], {}, 'User'],
expectation: 'CREATE INDEX \"user_username_is_admin\" ON \"mySchema\".\"User\" (\"username\", \"isAdmin\")'
}, {
arguments: ['User', ['fieldB', {attribute: 'fieldA', collate: 'en_US', order: 'DESC', length: 5}], {
name: 'a_b_uniq',
unique: true,
method: 'BTREE'
}, 'User'],
expectation: 'CREATE UNIQUE INDEX "a_b_uniq" ON "User" USING BTREE ("fieldB", "fieldA" COLLATE "en_US" DESC)'
}, {
arguments: ['User', ['fieldC'], {
type: 'FULLTEXT',
concurrently: true
}, 'User'],
expectation: 'CREATE INDEX CONCURRENTLY "user_field_c" ON "User" ("fieldC")'
},
// Variants when quoteIdentifiers is false
{
arguments: ['User', ['username', 'isAdmin'], {}, 'User'],
expectation: 'CREATE INDEX user_username_is_admin ON User (username, isAdmin)',
context: {options: {quoteIdentifiers: false}}
}, {
arguments: [
'User', [
{ attribute: 'username', length: 10, order: 'ASC'},
'isAdmin'
],
{},
'User'
],
expectation: 'CREATE INDEX user_username_is_admin ON User (username ASC, isAdmin)',
context: {options: {quoteIdentifiers: false}}
}, {
arguments: ['User', ['username', 'isAdmin'], { indexName: 'bar'}, 'User'],
expectation: 'CREATE INDEX bar ON User (username, isAdmin)',
context: {options: {quoteIdentifiers: false}}
}, {
arguments: ['mySchema.User', ['username', 'isAdmin'], {}, 'User'],
expectation: 'CREATE INDEX user_username_is_admin ON mySchema.User (username, isAdmin)',
context: {options: {quoteIdentifiers: false}}
}
],
removeIndexQuery: [
{
arguments: ['User', 'user_foo_bar'],
......
......@@ -491,39 +491,6 @@ if (dialect === 'sqlite') {
arguments: ['myTable', {name: 'foo'}, {limit: null}],
expectation: "DELETE FROM `myTable` WHERE `name` = 'foo'"
}
],
addIndexQuery: [
{
arguments: ['User', ['username', 'isAdmin'], {}, 'User'],
expectation: 'CREATE INDEX `user_username_is_admin` ON `User` (`username`, `isAdmin`)'
}, {
arguments: [
'User', [
{ attribute: 'username', length: 10, order: 'ASC'},
'isAdmin'
],
{},
'User'
],
expectation: 'CREATE INDEX `user_username_is_admin` ON `User` (`username` ASC, `isAdmin`)'
}, {
arguments: ['User', ['username', 'isAdmin'], { indexName: 'bar'}, 'User'],
expectation: 'CREATE INDEX `bar` ON `User` (`username`, `isAdmin`)'
}, {
arguments: ['User', ['fieldB', {attribute: 'fieldA', collate: 'en_US', order: 'DESC', length: 5}], {
name: 'a_b_uniq',
unique: true,
method: 'BTREE'
}, 'User'],
expectation: 'CREATE UNIQUE INDEX `a_b_uniq` ON `User` (`fieldB`, `fieldA` COLLATE `en_US` DESC)'
}, {
arguments: ['User', ['fieldC'], {
type: 'FULLTEXT',
concurrently: true
}, 'User'],
expectation: 'CREATE INDEX `user_field_c` ON `User` (`fieldC`)'
}
]
};
......
......@@ -12,12 +12,13 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
suite('addIndex', function () {
test('naming', function () {
expectsql(sql.addIndexQuery('table', ['column1', 'column2'], {}, 'table'), {
default: 'CREATE INDEX [table_column1_column2] ON [table] ([column1], [column2])'
default: 'CREATE INDEX [table_column1_column2] ON [table] ([column1], [column2])',
mysql: 'ALTER TABLE `table` ADD INDEX `table_column1_column2` (`column1`, `column2`)'
});
if (current.dialect.supports.schemas) {
expectsql(sql.addIndexQuery('schema.table', ['column1', 'column2'], {}), {
default: 'CREATE INDEX [schema_table_column1_column2] ON [schema].[table] ([column1], [column2])'
default: 'CREATE INDEX [schema_table_column1_column2] ON [schema].[table] ([column1], [column2])',
});
expectsql(sql.addIndexQuery({
......@@ -36,17 +37,42 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
}
});
test('type and method', function () {
expectsql(sql.addIndexQuery('User', ['fieldC'], {
type: 'FULLTEXT',
concurrently: true
}), {
sqlite: 'CREATE INDEX `user_field_c` ON `User` (`fieldC`)',
mssql: 'CREATE FULLTEXT INDEX [user_field_c] ON [User] ([fieldC])',
postgres: 'CREATE INDEX CONCURRENTLY "user_field_c" ON "User" ("fieldC")',
mysql: 'ALTER TABLE `User` ADD FULLTEXT INDEX `user_field_c` (`fieldC`)'
});
expectsql(sql.addIndexQuery('User', ['fieldB', {attribute: 'fieldA', collate: 'en_US', order: 'DESC', length: 5}], {
name: 'a_b_uniq',
unique: true,
method: 'BTREE',
parser: 'foo'
}), {
sqlite: 'CREATE UNIQUE INDEX `a_b_uniq` ON `User` (`fieldB`, `fieldA` COLLATE `en_US` DESC)',
mssql: 'CREATE UNIQUE INDEX [a_b_uniq] ON [User] ([fieldB], [fieldA] DESC)',
postgres: 'CREATE UNIQUE INDEX "a_b_uniq" ON "User" USING BTREE ("fieldB", "fieldA" COLLATE "en_US" DESC)',
mysql: 'ALTER TABLE `User` ADD UNIQUE INDEX `a_b_uniq` USING BTREE (`fieldB`, `fieldA`(5) DESC) WITH PARSER foo',
});
});
test('POJO field', function () {
expectsql(sql.addIndexQuery('table', [{ attribute: 'column', collate: 'BINARY', length: 5, order: 'DESC'}], {}, 'table'), {
default: 'CREATE INDEX [table_column] ON [table] ([column] COLLATE [BINARY] DESC)',
mysql: 'CREATE INDEX `table_column` ON `table` (`column`(5) DESC)',
mssql: 'CREATE INDEX [table_column] ON [table] ([column] DESC)'
mssql: 'CREATE INDEX [table_column] ON [table] ([column] DESC)',
mysql: 'ALTER TABLE `table` ADD INDEX `table_column` (`column`(5) DESC)'
});
});
test('function', function () {
expectsql(sql.addIndexQuery('table', [current.fn('UPPER', current.col('test'))], { name: 'myindex'}), {
default: 'CREATE INDEX [myindex] ON [table] (UPPER([test]))'
default: 'CREATE INDEX [myindex] ON [table] (UPPER([test]))',
mysql: 'ALTER TABLE `table` ADD INDEX `myindex` (UPPER(`test`))'
});
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!