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 e38dde35
authored
Jun 19, 2013
by
Daniel Durante
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Made better error messages for when we have trouble connecting to a database. Closes #227
1 parent
2eb96be5
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
138 additions
and
9 deletions
lib/dialects/mysql/connector-manager.js
lib/dialects/postgres/connector-manager.js
lib/dialects/sqlite/connector-manager.js
spec/configuration.spec.js
lib/dialects/mysql/connector-manager.js
View file @
e38dde3
...
@@ -241,7 +241,9 @@ module.exports = (function() {
...
@@ -241,7 +241,9 @@ module.exports = (function() {
}
}
var
connect
=
function
(
done
,
config
)
{
var
connect
=
function
(
done
,
config
)
{
var
emitter
=
new
(
require
(
'events'
).
EventEmitter
)()
config
=
config
||
this
.
config
config
=
config
||
this
.
config
var
connection
=
mysql
.
createConnection
({
var
connection
=
mysql
.
createConnection
({
host
:
config
.
host
,
host
:
config
.
host
,
port
:
config
.
port
,
port
:
config
.
port
,
...
@@ -250,6 +252,23 @@ module.exports = (function() {
...
@@ -250,6 +252,23 @@ module.exports = (function() {
database
:
config
.
database
,
database
:
config
.
database
,
timezone
:
'Z'
timezone
:
'Z'
})
})
connection
.
connect
(
function
(
err
)
{
if
(
err
)
{
switch
(
err
.
code
)
{
case
'ECONNREFUSED'
:
case
'ER_ACCESS_DENIED_ERROR'
:
emitter
.
emit
(
'error'
,
new
Error
(
"Failed to authenticate for MySQL. Please double check your settings."
))
break
case
'ENOTFOUND'
:
case
'EHOSTUNREACH'
:
case
'EINVAL'
:
emitter
.
emit
(
'error'
,
new
Error
(
"Failed to find MySQL server. Please double check your settings."
))
break
}
}
})
connection
.
query
(
"SET time_zone = '+0:00'"
);
connection
.
query
(
"SET time_zone = '+0:00'"
);
// client.setMaxListeners(self.maxConcurrentQueries)
// client.setMaxListeners(self.maxConcurrentQueries)
this
.
isConnecting
=
false
this
.
isConnecting
=
false
...
...
lib/dialects/postgres/connector-manager.js
View file @
e38dde3
...
@@ -27,7 +27,8 @@ module.exports = (function() {
...
@@ -27,7 +27,8 @@ module.exports = (function() {
ConnectorManager
.
prototype
.
query
=
function
(
sql
,
callee
,
options
)
{
ConnectorManager
.
prototype
.
query
=
function
(
sql
,
callee
,
options
)
{
var
self
=
this
var
self
=
this
if
(
this
.
client
==
null
)
{
if
(
this
.
client
===
null
)
{
this
.
connect
()
this
.
connect
()
}
}
...
@@ -68,7 +69,18 @@ module.exports = (function() {
...
@@ -68,7 +69,18 @@ module.exports = (function() {
self
.
isConnecting
=
false
self
.
isConnecting
=
false
if
(
!!
err
)
{
if
(
!!
err
)
{
emitter
.
emit
(
'error'
,
err
)
switch
(
err
.
code
)
{
case
'ECONNREFUSED'
:
emitter
.
emit
(
'error'
,
new
Error
(
"Failed to authenticate for PostgresSQL. Please double check your settings."
))
break
case
'ENOTFOUND'
:
case
'EHOSTUNREACH'
:
case
'EINVAL'
:
emitter
.
emit
(
'error'
,
new
Error
(
"Failed to find PostgresSQL server. Please double check your settings."
))
break
default
:
emitter
.
emit
(
'error'
,
err
)
}
}
else
if
(
client
)
{
}
else
if
(
client
)
{
client
.
query
(
"SET TIME ZONE 'UTC'"
)
client
.
query
(
"SET TIME ZONE 'UTC'"
)
.
on
(
'end'
,
function
()
{
.
on
(
'end'
,
function
()
{
...
...
lib/dialects/sqlite/connector-manager.js
View file @
e38dde3
...
@@ -5,17 +5,34 @@ var Utils = require("../../utils")
...
@@ -5,17 +5,34 @@ var Utils = require("../../utils")
module
.
exports
=
(
function
()
{
module
.
exports
=
(
function
()
{
var
ConnectorManager
=
function
(
sequelize
)
{
var
ConnectorManager
=
function
(
sequelize
)
{
this
.
sequelize
=
sequelize
this
.
sequelize
=
sequelize
this
.
database
=
db
=
new
sqlite3
.
Database
(
sequelize
.
options
.
storage
||
':memory:'
,
function
(
err
)
{
}
if
(
!
err
&&
sequelize
.
options
.
foreignKeys
!==
false
)
{
Utils
.
_
.
extend
(
ConnectorManager
.
prototype
,
require
(
"../connector-manager"
).
prototype
)
ConnectorManager
.
prototype
.
connect
=
function
()
{
var
emitter
=
new
(
require
(
'events'
).
EventEmitter
)()
,
self
=
this
this
.
database
=
db
=
new
sqlite3
.
Database
(
self
.
sequelize
.
options
.
storage
||
':memory:'
,
function
(
err
)
{
if
(
err
)
{
if
(
err
.
code
===
"SQLITE_CANTOPEN"
)
{
emitter
.
emit
(
'error'
,
new
Error
(
"Failed to find SQL server. Please double check your settings."
))
}
}
if
(
!
err
&&
self
.
sequelize
.
options
.
foreignKeys
!==
false
)
{
// Make it possible to define and use foreign key constraints unless
// Make it possible to define and use foreign key constraints unless
// explicitly disallowed. It's still opt-in per relation
// explicitly disallowed. It's still opt-in per relation
db
.
run
(
'PRAGMA FOREIGN_KEYS=ON'
)
db
.
run
(
'PRAGMA FOREIGN_KEYS=ON'
)
}
}
})
})
}
}
Utils
.
_
.
extend
(
ConnectorManager
.
prototype
,
require
(
"../connector-manager"
).
prototype
)
ConnectorManager
.
prototype
.
query
=
function
(
sql
,
callee
,
options
)
{
ConnectorManager
.
prototype
.
query
=
function
(
sql
,
callee
,
options
)
{
if
(
!
this
.
database
)
{
this
.
connect
()
}
return
new
Query
(
this
.
database
,
this
.
sequelize
,
callee
,
options
).
run
(
sql
)
return
new
Query
(
this
.
database
,
this
.
sequelize
,
callee
,
options
).
run
(
sql
)
}
}
...
...
spec/configuration.spec.js
View file @
e38dde3
if
(
typeof
require
===
'function'
)
{
if
(
typeof
require
===
'function'
)
{
const
buster
=
require
(
"buster"
)
const
buster
=
require
(
"buster"
)
,
CustomEventEmitter
=
require
(
"../lib/emitters/custom-event-emitter"
)
,
CustomEventEmitter
=
require
(
"../lib/emitters/custom-event-emitter"
)
,
Helpers
=
require
(
'./buster-helpers'
)
,
Helpers
=
require
(
'./buster-helpers'
)
,
dialect
=
Helpers
.
getTestDialect
()
,
config
=
require
(
__dirname
+
"/config/config"
)
,
dialect
=
Helpers
.
getTestDialect
()
}
}
buster
.
spec
.
expose
()
buster
.
spec
.
expose
()
...
@@ -11,6 +12,86 @@ buster.testRunner.timeout = 1000
...
@@ -11,6 +12,86 @@ buster.testRunner.timeout = 1000
var
Sequelize
=
require
(
__dirname
+
'/../index'
)
var
Sequelize
=
require
(
__dirname
+
'/../index'
)
describe
(
Helpers
.
getTestDialectTeaser
(
"Configuration"
),
function
()
{
describe
(
Helpers
.
getTestDialectTeaser
(
"Configuration"
),
function
()
{
describe
(
'Connections problems should fail with a nice message'
,
function
()
{
it
(
'should give us an error for not having the correct server details'
,
function
(
done
)
{
var
domain
try
{
domain
=
require
(
'domain'
)
}
catch
(
err
)
{
console
.
log
(
'WARNING: Configuration specs requires Node version >= 0.8'
)
expect
(
''
).
toEqual
(
''
)
// Silence Buster!
done
()
}
var
d
=
domain
.
create
()
var
sequelize
=
new
Sequelize
(
config
[
dialect
].
database
,
config
[
dialect
].
username
,
config
[
dialect
].
password
,
{
storage
:
'/path/to/no/where/land'
,
logging
:
false
,
host
:
'0.0.0.1'
,
port
:
config
[
dialect
].
port
,
dialect
:
dialect
})
d
.
add
(
sequelize
.
query
)
d
.
on
(
'error'
,
function
(
err
){
d
.
remove
(
sequelize
.
query
)
var
msg
=
'Failed to find SQL server. Please double check your settings.'
if
(
dialect
===
"postgres"
||
dialect
===
"postgres-native"
)
{
msg
=
'Failed to find PostgresSQL server. Please double check your settings.'
}
else
if
(
dialect
===
"mysql"
)
{
msg
=
'Failed to find MySQL server. Please double check your settings.'
}
expect
(
err
.
message
).
toEqual
(
msg
)
done
()
})
d
.
run
(
function
(){
sequelize
.
query
(
'select 1 as hello'
)
.
success
(
function
(){})
})
})
it
(
'should give us an error for not having the correct login information'
,
function
(
done
)
{
if
(
dialect
!==
"postgres"
&&
dialect
!==
"postgres-native"
&&
dialect
!==
"mysql"
)
{
// This dialect doesn't support incorrect login information
expect
(
''
).
toEqual
(
''
)
// Silence Buster
return
done
()
}
var
domain
try
{
domain
=
require
(
'domain'
)
}
catch
(
err
)
{
console
.
log
(
'WARNING: Configuration specs requires Node version >= 0.8'
)
expect
(
''
).
toEqual
(
''
)
// Silence Buster!
done
()
}
var
d
=
domain
.
create
()
var
sequelize
=
new
Sequelize
(
config
[
dialect
].
database
,
config
[
dialect
].
username
,
'fakepass123'
,
{
logging
:
false
,
host
:
config
[
dialect
].
host
,
port
:
1
,
dialect
:
dialect
})
d
.
add
(
sequelize
.
query
)
d
.
on
(
'error'
,
function
(
err
){
d
.
remove
(
sequelize
.
query
)
var
msg
=
'Failed to authenticate for SQL. Please double check your settings.'
if
(
dialect
===
"postgres"
||
dialect
===
"postgres-native"
)
{
msg
=
'Failed to authenticate for PostgresSQL. Please double check your settings.'
}
else
if
(
dialect
===
"mysql"
)
{
msg
=
'Failed to authenticate for MySQL. Please double check your settings.'
}
expect
(
err
.
message
).
toEqual
(
msg
)
done
()
})
d
.
run
(
function
(){
sequelize
.
query
(
'select 1 as hello'
)
.
success
(
function
(){})
})
})
})
describe
(
'Instantiation with a URL string'
,
function
()
{
describe
(
'Instantiation with a URL string'
,
function
()
{
it
(
'should accept username, password, host, port, and database'
,
function
()
{
it
(
'should accept username, password, host, port, and database'
,
function
()
{
var
sequelize
=
new
Sequelize
(
'mysql://user:pass@example.com:9821/dbname'
)
var
sequelize
=
new
Sequelize
(
'mysql://user:pass@example.com:9821/dbname'
)
...
...
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