Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit c4b5f2af
authored
May 17, 2015
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bug(index) Change addIndex to ALTER TABLE in mysql. Closes #3600
1 parent
ce84e892
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
54 additions
and
145 deletions
lib/dialects/abstract/index.js
lib/dialects/abstract/query-generator.js
lib/dialects/mysql/index.js
test/integration/dialects/mysql/query-generator.test.js
test/integration/dialects/postgres/query-generator.test.js
test/integration/dialects/sqlite/query-generator.test.js
test/unit/sql/index.test.js
lib/dialects/abstract/index.js
View file @
c4b5f2a
...
...
@@ -46,6 +46,7 @@ AbstractDialect.prototype.supports = {
using
:
true
,
},
joinTableDependent
:
true
,
indexViaAlter
:
false
,
JSON
:
false
,
};
...
...
lib/dialects/abstract/query-generator.js
View file @
c4b5f2a
...
...
@@ -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
(
' '
);
},
/*
...
...
lib/dialects/mysql/index.js
View file @
c4b5f2a
...
...
@@ -31,6 +31,7 @@ MysqlDialect.prototype.supports = _.merge(_.cloneDeep(Abstract.prototype.support
type
:
true
,
using
:
1
,
},
indexViaAlter
:
true
,
NUMERIC
:
true
});
...
...
test/integration/dialects/mysql/query-generator.test.js
View file @
c4b5f2a
...
...
@@ -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'
],
...
...
test/integration/dialects/postgres/query-generator.test.js
View file @
c4b5f2a
...
...
@@ -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'
],
...
...
test/integration/dialects/sqlite/query-generator.test.js
View file @
c4b5f2a
...
...
@@ -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`)'
}
]
};
...
...
test/unit/sql/index.test.js
View file @
c4b5f2a
...
...
@@ -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)'
,
m
ysql
:
'CREATE INDEX `table_column` ON `table` (`column`(5)
DESC)'
,
m
ssql
:
'CREATE INDEX [table_column] ON [table] ([column]
DESC)'
m
ssql
:
'CREATE INDEX [table_column] ON [table] ([column]
DESC)'
,
m
ysql
:
'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`))'
});
});
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment