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 1432cfd1
authored
Apr 20, 2020
by
Andy Edwards
Committed by
GitHub
Apr 20, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor(dialects/mysql): asyncify methods (#12130)
1 parent
722ed505
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
35 additions
and
50 deletions
lib/dialects/mysql/connection-manager.js
lib/dialects/mysql/query-interface.js
lib/dialects/mysql/query.js
lib/dialects/mysql/connection-manager.js
View file @
1432cfd
...
...
@@ -54,7 +54,7 @@ class ConnectionManager extends AbstractConnectionManager {
* @returns {Promise<Connection>}
* @private
*/
connect
(
config
)
{
async
connect
(
config
)
{
const
connectionConfig
=
Object
.
assign
({
host
:
config
.
host
,
port
:
config
.
port
,
...
...
@@ -68,7 +68,8 @@ class ConnectionManager extends AbstractConnectionManager {
supportBigNumbers
:
true
},
config
.
dialectOptions
);
return
new
Promise
((
resolve
,
reject
)
=>
{
try
{
const
connection
=
await
new
Promise
((
resolve
,
reject
)
=>
{
const
connection
=
this
.
lib
.
createConnection
(
connectionConfig
);
const
errorHandler
=
e
=>
{
...
...
@@ -90,11 +91,9 @@ class ConnectionManager extends AbstractConnectionManager {
// if we will use `once.error` node process will crash on 2nd error emit
connection
.
on
(
'error'
,
errorHandler
);
connection
.
once
(
'connect'
,
connectHandler
);
})
.
then
(
result
=>
{
debug
(
'connection acquired'
);
return
result
;
})
.
then
(
connection
=>
{
});
debug
(
'connection acquired'
);
connection
.
on
(
'error'
,
error
=>
{
switch
(
error
.
code
)
{
case
'ESOCKET'
:
...
...
@@ -105,22 +104,16 @@ class ConnectionManager extends AbstractConnectionManager {
}
});
return
new
Promise
((
resolve
,
reject
)
=>
{
if
(
!
this
.
sequelize
.
config
.
keepDefaultTimezone
)
{
// set timezone for this connection
// but named timezone are not directly supported in mysql, so get its offset first
let
tzOffset
=
this
.
sequelize
.
options
.
timezone
;
tzOffset
=
/
\/
/
.
test
(
tzOffset
)
?
momentTz
.
tz
(
tzOffset
).
format
(
'Z'
)
:
tzOffset
;
return
connection
.
query
(
`SET time_zone = '
${
tzOffset
}
'`
,
err
=>
{
if
(
err
)
{
reject
(
err
);
}
else
{
resolve
(
connection
);
}
});
await
promisify
(
cb
=>
connection
.
query
(
`SET time_zone = '
${
tzOffset
}
'`
,
cb
))();
}
// return connection without executing SET time_zone query
resolve
(
connection
);
});
})
.
catch
(
err
=>
{
return
connection
;
}
catch
(
err
)
{
switch
(
err
.
code
)
{
case
'ECONNREFUSED'
:
throw
new
SequelizeErrors
.
ConnectionRefusedError
(
err
);
...
...
@@ -135,17 +128,17 @@ class ConnectionManager extends AbstractConnectionManager {
default
:
throw
new
SequelizeErrors
.
ConnectionError
(
err
);
}
});
}
}
disconnect
(
connection
)
{
async
disconnect
(
connection
)
{
// Don't disconnect connections with CLOSED state
if
(
connection
.
_closing
)
{
debug
(
'connection tried to disconnect but was already at CLOSED state'
);
return
Promise
.
resolve
()
;
return
;
}
return
promisify
(
callback
=>
connection
.
end
(
callback
))();
return
await
promisify
(
callback
=>
connection
.
end
(
callback
))();
}
validate
(
connection
)
{
...
...
lib/dialects/mysql/query-interface.js
View file @
1432cfd
...
...
@@ -21,31 +21,29 @@ const sequelizeErrors = require('../../errors');
@private
*/
function
removeColumn
(
qi
,
tableName
,
columnName
,
options
)
{
async
function
removeColumn
(
qi
,
tableName
,
columnName
,
options
)
{
options
=
options
||
{};
return
qi
.
sequelize
.
query
(
const
[
results
]
=
await
qi
.
sequelize
.
query
(
qi
.
QueryGenerator
.
getForeignKeyQuery
(
tableName
.
tableName
?
tableName
:
{
tableName
,
schema
:
qi
.
sequelize
.
config
.
database
},
columnName
),
Object
.
assign
({
raw
:
true
},
options
)
)
.
then
(([
results
])
=>
{
)
;
//Exclude primary key constraint
if
(
!
results
.
length
||
results
[
0
].
constraint_name
===
'PRIMARY'
)
{
// No foreign key constraints found, so we can remove the column
return
;
}
return
Promise
.
all
(
results
.
map
(
constraint
=>
qi
.
sequelize
.
query
(
if
(
results
.
length
&&
results
[
0
].
constraint_name
!==
'PRIMARY'
)
{
await
Promise
.
all
(
results
.
map
(
constraint
=>
qi
.
sequelize
.
query
(
qi
.
QueryGenerator
.
dropForeignKeyQuery
(
tableName
,
constraint
.
constraint_name
),
Object
.
assign
({
raw
:
true
},
options
)
)));
})
.
then
(()
=>
qi
.
sequelize
.
query
(
}
return
await
qi
.
sequelize
.
query
(
qi
.
QueryGenerator
.
removeColumnQuery
(
tableName
,
columnName
),
Object
.
assign
({
raw
:
true
},
options
)
)
);
);
}
/**
...
...
@@ -56,16 +54,16 @@ function removeColumn(qi, tableName, columnName, options) {
*
* @private
*/
function
removeConstraint
(
qi
,
tableName
,
constraintName
,
options
)
{
async
function
removeConstraint
(
qi
,
tableName
,
constraintName
,
options
)
{
const
sql
=
qi
.
QueryGenerator
.
showConstraintsQuery
(
tableName
.
tableName
?
tableName
:
{
tableName
,
schema
:
qi
.
sequelize
.
config
.
database
},
constraintName
);
return
qi
.
sequelize
.
query
(
sql
,
Object
.
assign
({},
options
,
{
type
:
qi
.
sequelize
.
QueryTypes
.
SHOWCONSTRAINTS
}))
.
then
(
constraints
=>
{
const
constraints
=
await
qi
.
sequelize
.
query
(
sql
,
Object
.
assign
({},
options
,
{
type
:
qi
.
sequelize
.
QueryTypes
.
SHOWCONSTRAINTS
}))
;
const
constraint
=
constraints
[
0
];
let
query
;
if
(
!
constraint
||
!
constraint
.
constraintType
)
{
...
...
@@ -83,8 +81,7 @@ function removeConstraint(qi, tableName, constraintName, options) {
query
=
qi
.
QueryGenerator
.
removeIndexQuery
(
constraint
.
tableName
,
constraint
.
constraintName
);
}
return
qi
.
sequelize
.
query
(
query
,
options
);
});
return
await
qi
.
sequelize
.
query
(
query
,
options
);
}
exports
.
removeConstraint
=
removeConstraint
;
...
...
lib/dialects/mysql/query.js
View file @
1432cfd
'use strict'
;
const
Utils
=
require
(
'../../utils'
);
const
AbstractQuery
=
require
(
'../abstract/query'
);
const
sequelizeErrors
=
require
(
'../../errors'
);
const
_
=
require
(
'lodash'
);
...
...
@@ -27,7 +26,7 @@ class Query extends AbstractQuery {
return
[
sql
,
bindParam
.
length
>
0
?
bindParam
:
undefined
];
}
run
(
sql
,
parameters
)
{
async
run
(
sql
,
parameters
)
{
this
.
sql
=
sql
;
const
{
connection
,
options
}
=
this
;
...
...
@@ -36,7 +35,7 @@ class Query extends AbstractQuery {
const
complete
=
this
.
_logQuery
(
sql
,
debug
,
parameters
);
return
new
Utils
.
Promise
((
resolve
,
reject
)
=>
{
const
results
=
await
new
Promise
((
resolve
,
reject
)
=>
{
const
handler
=
(
err
,
results
)
=>
{
complete
();
...
...
@@ -59,16 +58,13 @@ class Query extends AbstractQuery {
}
else
{
connection
.
query
({
sql
},
handler
).
setMaxListeners
(
100
);
}
})
})
;
// Log warnings if we've got them.
.
then
(
results
=>
{
if
(
showWarnings
&&
results
&&
results
.
warningStatus
>
0
)
{
return
this
.
logWarnings
(
results
);
await
this
.
logWarnings
(
results
);
}
return
results
;
})
// Return formatted results...
.
then
(
results
=>
this
.
formatResults
(
results
)
);
return
this
.
formatResults
(
results
);
}
/**
...
...
@@ -165,8 +161,8 @@ class Query extends AbstractQuery {
return
result
;
}
logWarnings
(
results
)
{
return
this
.
run
(
'SHOW WARNINGS'
).
then
(
warningResults
=>
{
async
logWarnings
(
results
)
{
const
warningResults
=
await
this
.
run
(
'SHOW WARNINGS'
);
const
warningMessage
=
`MySQL Warnings (
${
this
.
connection
.
uuid
||
'default'
}
): `
;
const
messages
=
[];
for
(
const
_warningRow
of
warningResults
)
{
...
...
@@ -185,7 +181,6 @@ class Query extends AbstractQuery {
this
.
sequelize
.
log
(
warningMessage
+
messages
.
join
(
'; '
),
this
.
options
);
return
results
;
});
}
formatError
(
err
)
{
...
...
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