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 198cb52d
authored
Oct 16, 2018
by
Wei-Liang Liou
Committed by
Sushant
Oct 16, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(postgres): remove if not exists and cascade from create/drop database queries (#10033)
1 parent
55e5734e
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
37 additions
and
21 deletions
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/query-generator.js
lib/query-interface.js
test/unit/dialects/mysql/query-generator.test.js
test/unit/dialects/postgres/query-generator.test.js
lib/dialects/mysql/query-generator.js
View file @
198cb52
...
...
@@ -26,8 +26,8 @@ class MySQLQueryGenerator extends AbstractQueryGenerator {
const
values
=
{
database
:
this
.
quoteIdentifier
(
databaseName
),
charset
:
options
.
charset
?
' DEFAULT CHAR
SET SET =
'
+
this
.
escape
(
options
.
charset
)
:
''
,
collation
:
options
.
collate
?
' DEFAULT COLLATE
=
'
+
this
.
escape
(
options
.
collate
)
:
''
charset
:
options
.
charset
?
' DEFAULT CHAR
ACTER SET
'
+
this
.
escape
(
options
.
charset
)
:
''
,
collation
:
options
.
collate
?
' DEFAULT COLLATE '
+
this
.
escape
(
options
.
collate
)
:
''
};
return
_
.
template
(
query
,
this
.
_templateSettings
)(
values
).
trim
()
+
';'
;
...
...
lib/dialects/postgres/query-generator.js
View file @
198cb52
...
...
@@ -21,14 +21,16 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
const
values
=
{
database
:
this
.
quoteTable
(
databaseName
),
encoding
:
options
.
encoding
?
' ENCODING = '
+
this
.
escape
(
options
.
encoding
)
:
''
,
collation
:
options
.
collate
?
' LC_COLLATE = '
+
this
.
escape
(
options
.
collate
)
:
''
collation
:
options
.
collate
?
' LC_COLLATE = '
+
this
.
escape
(
options
.
collate
)
:
''
,
ctype
:
options
.
ctype
?
' LC_CTYPE = '
+
this
.
escape
(
options
.
ctype
)
:
''
,
template
:
options
.
template
?
' TEMPLATE = '
+
this
.
escape
(
options
.
template
)
:
''
};
return
`CREATE DATABASE
IF NOT EXISTS
${
values
.
database
}${
values
.
encoding
}${
values
.
collation
}
;`
;
return
`CREATE DATABASE
${
values
.
database
}${
values
.
encoding
}${
values
.
collation
}${
values
.
ctype
}${
values
.
template
}
;`
;
}
dropDatabaseQuery
(
databaseName
)
{
return
`DROP DATABASE IF EXISTS
${
this
.
quoteTable
(
databaseName
)}
CASCADE
;`
;
return
`DROP DATABASE IF EXISTS
${
this
.
quoteTable
(
databaseName
)}
;`
;
}
createSchema
(
schema
)
{
...
...
lib/query-interface.js
View file @
198cb52
...
...
@@ -27,31 +27,33 @@ class QueryInterface {
/**
* Creates a database
*
* @param {string}
schema Schema
name to create
* @param {string}
database Database
name to create
* @param {Object} [options] Query options
* @param {string} [options.charset] Database default character set, MYSQL only
* @param {string} [options.encoding] Database default character set, PostgreSQL only
* @param {string} [options.collate] Database default collation
* @param {string} [options.encoding] Database default character set, PostgreSQL only
* @param {string} [options.ctype] Database character classification, PostgreSQL only
* @param {string} [options.template] The name of the template from which to create the new database, PostgreSQL only
*
* @returns {Promise}
*/
createDatabase
(
schema
,
options
)
{
createDatabase
(
database
,
options
)
{
options
=
options
||
{};
const
sql
=
this
.
QueryGenerator
.
createDatabaseQuery
(
schema
,
options
);
const
sql
=
this
.
QueryGenerator
.
createDatabaseQuery
(
database
,
options
);
return
this
.
sequelize
.
query
(
sql
,
options
);
}
/**
* Drops a database
*
* @param {string}
schema Schema
name to drop
* @param {string}
database Database
name to drop
* @param {Object} [options] Query options
*
* @returns {Promise}
*/
dropDatabase
(
schema
,
options
)
{
dropDatabase
(
database
,
options
)
{
options
=
options
||
{};
const
sql
=
this
.
QueryGenerator
.
dropDatabaseQuery
(
schema
);
const
sql
=
this
.
QueryGenerator
.
dropDatabaseQuery
(
database
);
return
this
.
sequelize
.
query
(
sql
,
options
);
}
...
...
test/unit/dialects/mysql/query-generator.test.js
View file @
198cb52
...
...
@@ -18,15 +18,15 @@ if (dialect === 'mysql') {
},
{
arguments
:
[
'myDatabase'
,
{
charset
:
'utf8mb4'
}],
expectation
:
'CREATE DATABASE IF NOT EXISTS `myDatabase` DEFAULT CHAR
SET SET =
\'utf8mb4\';'
expectation
:
'CREATE DATABASE IF NOT EXISTS `myDatabase` DEFAULT CHAR
ACTER SET
\'utf8mb4\';'
},
{
arguments
:
[
'myDatabase'
,
{
collate
:
'utf8mb4_unicode_ci'
}],
expectation
:
'CREATE DATABASE IF NOT EXISTS `myDatabase` DEFAULT COLLATE
=
\'utf8mb4_unicode_ci\';'
expectation
:
'CREATE DATABASE IF NOT EXISTS `myDatabase` DEFAULT COLLATE \'utf8mb4_unicode_ci\';'
},
{
arguments
:
[
'myDatabase'
,
{
charset
:
'utf8mb4'
,
collate
:
'utf8mb4_unicode_ci'
}],
expectation
:
'CREATE DATABASE IF NOT EXISTS `myDatabase` DEFAULT CHAR
SET SET = \'utf8mb4\' DEFAULT COLLATE =
\'utf8mb4_unicode_ci\';'
expectation
:
'CREATE DATABASE IF NOT EXISTS `myDatabase` DEFAULT CHAR
ACTER SET \'utf8mb4\' DEFAULT COLLATE
\'utf8mb4_unicode_ci\';'
}
],
dropDatabaseQuery
:
[
...
...
test/unit/dialects/postgres/query-generator.test.js
View file @
198cb52
...
...
@@ -17,25 +17,37 @@ if (dialect.match(/^postgres/)) {
createDatabaseQuery
:
[
{
arguments
:
[
'myDatabase'
],
expectation
:
'CREATE DATABASE
IF NOT EXISTS
"myDatabase";'
expectation
:
'CREATE DATABASE "myDatabase";'
},
{
arguments
:
[
'myDatabase'
,
{
encoding
:
'UTF8'
}],
expectation
:
'CREATE DATABASE
IF NOT EXISTS
"myDatabase" ENCODING = \'UTF8\';'
expectation
:
'CREATE DATABASE "myDatabase" ENCODING = \'UTF8\';'
},
{
arguments
:
[
'myDatabase'
,
{
collate
:
'en_US.UTF-8'
}],
expectation
:
'CREATE DATABASE
IF NOT EXISTS
"myDatabase" LC_COLLATE = \'en_US.UTF-8\';'
expectation
:
'CREATE DATABASE "myDatabase" LC_COLLATE = \'en_US.UTF-8\';'
},
{
arguments
:
[
'myDatabase'
,
{
encoding
:
'UTF8'
,
collate
:
'en_US.UTF-8'
}],
expectation
:
'CREATE DATABASE IF NOT EXISTS "myDatabase" ENCODING = \'UTF8\' LC_COLLATE = \'en_US.UTF-8\';'
arguments
:
[
'myDatabase'
,
{
encoding
:
'UTF8'
}],
expectation
:
'CREATE DATABASE "myDatabase" ENCODING = \'UTF8\';'
},
{
arguments
:
[
'myDatabase'
,
{
ctype
:
'zh_TW.UTF-8'
}],
expectation
:
'CREATE DATABASE "myDatabase" LC_CTYPE = \'zh_TW.UTF-8\';'
},
{
arguments
:
[
'myDatabase'
,
{
template
:
'template0'
}],
expectation
:
'CREATE DATABASE "myDatabase" TEMPLATE = \'template0\';'
},
{
arguments
:
[
'myDatabase'
,
{
encoding
:
'UTF8'
,
collate
:
'en_US.UTF-8'
,
ctype
:
'zh_TW.UTF-8'
,
template
:
'template0'
}],
expectation
:
'CREATE DATABASE "myDatabase" ENCODING = \'UTF8\' LC_COLLATE = \'en_US.UTF-8\' LC_CTYPE = \'zh_TW.UTF-8\' TEMPLATE = \'template0\';'
}
],
dropDatabaseQuery
:
[
{
arguments
:
[
'myDatabase'
],
expectation
:
'DROP DATABASE IF EXISTS "myDatabase"
CASCADE
;'
expectation
:
'DROP DATABASE IF EXISTS "myDatabase";'
}
],
arithmeticQuery
:
[
...
...
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