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 c8d49662
authored
Mar 02, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
removeColum is working for sqlite + describeTable was unified
1 parent
c541ddc9
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
138 additions
and
59 deletions
lib/dialects/abstract/query.js
lib/dialects/postgres/query.js
lib/dialects/sqlite/query-generator.js
lib/dialects/sqlite/query.js
lib/query-interface.js
spec/migrator.spec.js
spec/query-interface.spec.js
lib/dialects/abstract/query.js
View file @
c8d4966
...
...
@@ -72,12 +72,13 @@ module.exports = (function() {
result
=
data
if
(
this
.
sql
.
toLowerCase
().
indexOf
(
'describe'
)
===
0
)
{
result
=
result
.
map
(
function
(
result
)
{
return
{
attribute
:
result
.
Field
,
type
:
result
.
Type
.
toUpperCase
(),
allowNull
:
(
result
.
Null
===
'YES'
),
defaultValue
:
result
.
Default
result
=
{}
data
.
forEach
(
function
(
_result
)
{
result
[
_result
.
Field
]
=
{
type
:
_result
.
Type
.
toUpperCase
(),
allowNull
:
(
_result
.
Null
===
'YES'
),
defaultValue
:
_result
.
Default
}
})
}
else
if
(
this
.
sql
.
toLowerCase
().
indexOf
(
'show index from'
)
===
0
)
{
...
...
lib/dialects/postgres/query.js
View file @
c8d4966
...
...
@@ -74,16 +74,25 @@ module.exports = (function() {
if
(
this
.
send
(
'isSelectQuery'
))
{
if
(
this
.
sql
.
toLowerCase
().
indexOf
(
'select column_name'
)
===
0
)
{
rows
=
rows
.
map
(
function
(
result
)
{
return
{
attribute
:
result
.
Field
,
type
:
result
.
Type
.
toUpperCase
(),
allowNull
:
(
result
.
Null
===
'YES'
),
defaultValue
:
result
.
Default
var
result
=
{}
rows
.
forEach
(
function
(
_result
)
{
result
[
_result
.
Field
]
=
{
type
:
_result
.
Type
.
toUpperCase
(),
allowNull
:
(
_result
.
Null
===
'YES'
),
defaultValue
:
_result
.
Default
}
if
(
result
[
_result
.
Field
].
type
===
'BOOLEAN'
)
{
result
[
_result
.
Field
].
defaultValue
=
{
'false'
:
false
,
'true'
:
true
}[
result
[
_result
.
Field
].
defaultValue
]
if
(
result
[
_result
.
Field
].
defaultValue
===
undefined
)
{
result
[
_result
.
Field
].
defaultValue
=
null
}
}
})
this
.
emit
(
'success'
,
r
ows
)
this
.
emit
(
'success'
,
r
esult
)
}
else
{
this
.
emit
(
'success'
,
this
.
send
(
'handleSelectQuery'
,
rows
))
}
...
...
lib/dialects/sqlite/query-generator.js
View file @
c8d4966
...
...
@@ -58,7 +58,13 @@ module.exports = (function() {
values
.
attributes
+=
", PRIMARY KEY ("
+
pkString
+
")"
}
return
Utils
.
_
.
template
(
query
)(
values
).
trim
()
+
";"
var
sql
=
Utils
.
_
.
template
(
query
,
values
).
trim
()
+
";"
return
QueryGenerator
.
replaceBooleanDefaults
(
sql
)
},
addColumnQuery
:
function
()
{
var
sql
=
MySqlQueryGenerator
.
addColumnQuery
.
apply
(
null
,
arguments
)
return
QueryGenerator
.
replaceBooleanDefaults
(
sql
)
},
showTablesQuery
:
function
()
{
...
...
@@ -227,15 +233,38 @@ module.exports = (function() {
},
describeTableQuery
:
function
(
tableName
)
{
var
sql
=
"PRAGMA TABLE_INFO('<%= tableName %>')"
var
sql
=
"PRAGMA TABLE_INFO('<%= tableName %>')
;
"
return
Utils
.
_
.
template
(
sql
,
{
tableName
:
tableName
})
},
renameTableQuery
:
function
(
before
,
after
)
{
var
query
=
"ALTER TABLE `<%= before %>` RENAME TO `<%= after %>`;"
return
Utils
.
_
.
template
(
query
,
{
before
:
before
,
after
:
after
})
},
removeColumnQuery
:
function
(
tableName
,
attributes
)
{
attributes
=
QueryGenerator
.
attributesToSQL
(
attributes
)
var
backupTableName
=
tableName
+
"_backup"
var
query
=
[
QueryGenerator
.
createTableQuery
(
backupTableName
,
attributes
).
replace
(
'CREATE TABLE'
,
'CREATE TEMPORARY TABLE'
),
"INSERT INTO <%= tableName %>_backup SELECT <%= attributeNames %> FROM <%= tableName %>;"
,
"DROP TABLE <%= tableName %>;"
,
QueryGenerator
.
createTableQuery
(
tableName
,
attributes
),
"INSERT INTO <%= tableName %> SELECT <%= attributeNames %> FROM <%= tableName %>_backup;"
,
"DROP TABLE <%= tableName %>_backup;"
,
].
join
(
""
)
return
Utils
.
_
.
template
(
query
,
{
tableName
:
tableName
,
attributeNames
:
Utils
.
_
.
keys
(
attributes
)
})
},
replaceBooleanDefaults
:
function
(
sql
)
{
return
sql
.
replace
(
/DEFAULT '
?
false'
?
/g
,
"DEFAULT 0"
).
replace
(
/DEFAULT '
?
true'
?
/g
,
"DEFAULT 1"
)
}
}
return
Utils
.
_
.
extend
(
MySqlQueryGenerator
,
QueryGenerator
)
return
Utils
.
_
.
extend
(
{},
MySqlQueryGenerator
,
QueryGenerator
)
})()
lib/dialects/sqlite/query.js
View file @
c8d4966
...
...
@@ -67,7 +67,7 @@ module.exports = (function() {
//private
var
getDatabaseMethod
=
function
()
{
if
(
this
.
send
(
'isInsertQuery'
)
||
this
.
send
(
'isUpdateQuery'
))
{
if
(
this
.
send
(
'isInsertQuery'
)
||
this
.
send
(
'isUpdateQuery'
)
||
(
this
.
sql
.
toLowerCase
().
indexOf
(
'CREATE TEMPORARY TABLE'
.
toLowerCase
())
!==
-
1
)
)
{
return
'run'
}
else
{
return
'all'
...
...
@@ -112,12 +112,21 @@ module.exports = (function() {
})
}
else
if
(
this
.
sql
.
indexOf
(
'PRAGMA TABLE_INFO'
)
!==
-
1
)
{
// this is the sqlite way of getting the metadata of a table
result
=
results
.
map
(
function
(
result
)
{
return
{
attribute
:
result
.
name
,
type
:
result
.
type
,
allowNull
:
(
result
.
notnull
===
0
),
defaultValue
:
result
.
dflt_value
result
=
{}
results
.
forEach
(
function
(
_result
)
{
result
[
_result
.
name
]
=
{
type
:
_result
.
type
,
allowNull
:
(
_result
.
notnull
===
0
),
defaultValue
:
_result
.
dflt_value
}
if
(
result
[
_result
.
name
].
type
===
'TINYINT(1)'
)
{
result
[
_result
.
name
].
defaultValue
=
{
'0'
:
false
,
'1'
:
true
}[
result
[
_result
.
name
].
defaultValue
]
}
if
(
result
[
_result
.
name
].
defaultValue
===
undefined
)
{
result
[
_result
.
name
].
defaultValue
=
null
}
})
}
...
...
lib/query-interface.js
View file @
c8d4966
...
...
@@ -102,7 +102,7 @@ module.exports = (function() {
var
attributes
=
{}
if
(
Utils
.
_
.
values
(
DataTypes
).
indexOf
(
dataTypeOrOptions
)
>
-
1
)
{
attributes
[
attributeName
]
=
{
type
:
dataTypeOrOptions
,
allowNull
:
fals
e
}
attributes
[
attributeName
]
=
{
type
:
dataTypeOrOptions
,
allowNull
:
tru
e
}
}
else
{
attributes
[
attributeName
]
=
dataTypeOrOptions
}
...
...
@@ -114,9 +114,41 @@ module.exports = (function() {
}
QueryInterface
.
prototype
.
removeColumn
=
function
(
tableName
,
attributeName
)
{
if
(
this
.
sequelize
.
options
.
dialect
===
'sqlite'
)
{
// sqlite needs some special treatment as it cannot drop a column
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
this
.
describeTable
(
tableName
).
complete
(
function
(
err
,
fields
)
{
if
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
}
else
{
delete
fields
[
attributeName
]
var
sql
=
this
.
QueryGenerator
.
removeColumnQuery
(
tableName
,
fields
)
,
chainer
=
new
Utils
.
QueryChainer
()
,
subQueries
=
sql
.
split
(
';'
).
filter
(
function
(
q
)
{
return
q
!==
''
})
subQueries
.
splice
(
0
,
subQueries
.
length
-
1
).
forEach
(
function
(
subQuery
)
{
chainer
.
add
(
this
.
sequelize
,
'query'
,
[
subQuery
+
";"
,
null
,
{
raw
:
true
}])
}.
bind
(
this
))
chainer
.
runSerially
()
.
complete
(
function
(
err
)
{
if
(
err
)
{
emitter
.
emit
(
'removeColumn'
,
err
)
emitter
.
emit
(
'error'
,
err
)
}
else
{
queryAndEmit
.
call
(
this
,
subQueries
.
splice
(
subQueries
.
length
-
1
)[
0
],
'removeColumn'
)
}
}.
bind
(
this
))
}
}.
bind
(
this
))
}.
bind
(
this
)).
run
()
}
else
{
var
sql
=
this
.
QueryGenerator
.
removeColumnQuery
(
tableName
,
attributeName
)
return
queryAndEmit
.
call
(
this
,
sql
,
'removeColumn'
)
}
}
QueryInterface
.
prototype
.
changeColumn
=
function
(
tableName
,
attributeName
,
dataTypeOrOptions
)
{
var
attributes
=
{}
...
...
@@ -138,12 +170,12 @@ module.exports = (function() {
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
self
.
describeTable
(
tableName
).
success
(
function
(
data
)
{
data
=
data
.
filter
(
function
(
h
)
{
return
h
.
attribute
==
attrNameBefore
})[
0
]
||
{}
data
=
data
[
attrNameBefore
]
||
{}
var
options
=
{}
options
[
attrNameAfter
]
=
{
attribute
:
data
.
attribute
,
attribute
:
attrNameAfter
,
type
:
data
.
type
,
allowNull
:
data
.
allowNull
,
defaultValue
:
data
.
defaultValue
...
...
@@ -247,15 +279,13 @@ module.exports = (function() {
// private
var
queryAndEmit
=
function
(
sqlOrQueryParams
,
methodName
,
options
)
{
var
self
=
this
var
queryAndEmit
=
function
(
sqlOrQueryParams
,
methodName
,
options
,
emitter
)
{
options
=
Utils
.
_
.
extend
({
success
:
function
(){},
error
:
function
(){}
},
options
||
{})
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
var
execQuery
=
function
(
emitter
)
{
var
query
=
null
if
(
Array
.
isArray
(
sqlOrQueryParams
))
{
...
...
@@ -267,9 +297,9 @@ module.exports = (function() {
sqlOrQueryParams
.
push
({})
}
query
=
self
.
sequelize
.
query
.
apply
(
self
.
sequelize
,
sqlOrQueryParams
)
query
=
this
.
sequelize
.
query
.
apply
(
this
.
sequelize
,
sqlOrQueryParams
)
}
else
{
query
=
self
.
sequelize
.
query
(
sqlOrQueryParams
,
null
,
{})
query
=
this
.
sequelize
.
query
(
sqlOrQueryParams
,
null
,
{})
}
// append the query for better testing
...
...
@@ -277,17 +307,24 @@ module.exports = (function() {
query
.
success
(
function
(
obj
)
{
options
.
success
&&
options
.
success
(
obj
)
self
.
emit
(
methodName
,
null
)
this
.
emit
(
methodName
,
null
)
emitter
.
emit
(
'success'
,
obj
)
}).
error
(
function
(
err
)
{
}
.
bind
(
this
)
).
error
(
function
(
err
)
{
options
.
error
&&
options
.
error
(
err
)
self
.
emit
(
methodName
,
err
)
this
.
emit
(
methodName
,
err
)
emitter
.
emit
(
'error'
,
err
)
})
}.
bind
(
this
))
query
.
on
(
'sql'
,
function
(
sql
)
{
emitter
.
emit
(
'sql'
,
sql
)
});
}).
run
()
})
}.
bind
(
this
)
if
(
!!
emitter
)
{
execQuery
(
emitter
)
}
else
{
return
new
Utils
.
CustomEventEmitter
(
execQuery
).
run
()
}
}
return
QueryInterface
...
...
spec/migrator.spec.js
View file @
c8d4966
...
...
@@ -123,7 +123,7 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() {
it
(
"executes migration #20111117063700 and correctly adds isBetaMember"
,
function
(
done
)
{
this
.
sequelize
.
getQueryInterface
().
describeTable
(
'Person'
).
success
(
function
(
data
)
{
var
fields
=
data
.
map
(
function
(
d
)
{
return
d
.
attribute
}
).
sort
()
var
fields
=
Helpers
.
Sequelize
.
Utils
.
_
.
keys
(
data
).
sort
()
expect
(
fields
).
toEqual
([
'isBetaMember'
,
'name'
])
done
()
})
...
...
@@ -190,9 +190,9 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() {
this
.
init
({
from
:
20111117063700
,
to
:
20111205162700
},
function
(
migrator
)
{
migrator
.
migrate
().
success
(
function
()
{
this
.
sequelize
.
getQueryInterface
().
describeTable
(
'User'
).
success
(
function
(
data
)
{
var
signature
=
data
.
filter
(
function
(
hash
){
return
hash
.
Field
==
'signature'
})[
0
]
,
isAdmin
=
data
.
filter
(
function
(
hash
){
return
hash
.
Field
==
'isAdmin'
})[
0
]
,
shopId
=
data
.
filter
(
function
(
hash
){
return
hash
.
Field
==
'shopId'
})[
0
]
var
signature
=
data
.
signature
,
isAdmin
=
data
.
isAdmin
,
shopId
=
data
.
shopId
expect
(
signature
.
Field
).
toEqual
(
'signature'
)
expect
(
signature
.
Null
).
toEqual
(
'NO'
)
...
...
@@ -212,20 +212,17 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() {
})
describe
(
'removeColumn'
,
function
()
{
(
dialect
===
'mysql'
?
it
:
itEventually
)
(
'removes the shopId column from user'
,
function
(
done
)
{
it
(
'removes the shopId column from user'
,
function
(
done
)
{
this
.
init
({
to
:
20111206061400
},
function
(
migrator
)
{
migrator
.
migrate
().
success
(
function
(){
this
.
sequelize
.
getQueryInterface
().
describeTable
(
'User'
).
success
(
function
(
data
)
{
var
signature
=
data
.
filter
(
function
(
hash
){
return
hash
.
attribute
==
'signature'
})[
0
]
,
isAdmin
=
data
.
filter
(
function
(
hash
){
return
hash
.
attribute
==
'isAdmin'
})[
0
]
,
shopId
=
data
.
filter
(
function
(
hash
){
return
hash
.
attribute
==
'shopId'
})[
0
]
var
signature
=
data
.
signature
,
isAdmin
=
data
.
isAdmin
,
shopId
=
data
.
shopId
expect
(
signature
.
attribute
).
toEqual
(
'signature'
)
expect
(
signature
.
allowNull
).
toEqual
(
false
)
expect
(
isAdmin
.
attribute
).
toEqual
(
'isAdmin'
)
expect
(
signature
.
allowNull
).
toEqual
(
true
)
expect
(
isAdmin
.
allowNull
).
toEqual
(
false
)
expect
(
isAdmin
.
defaultValue
).
toEqual
(
'0'
)
expect
(
isAdmin
.
defaultValue
).
toEqual
(
false
)
expect
(
shopId
).
toBeFalsy
()
...
...
@@ -241,9 +238,8 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() {
this
.
init
({
to
:
20111206063000
},
function
(
migrator
)
{
migrator
.
migrate
().
success
(
function
()
{
this
.
sequelize
.
getQueryInterface
().
describeTable
(
'User'
).
success
(
function
(
data
)
{
var
signature
=
data
.
filter
(
function
(
hash
){
return
hash
.
attribute
==
'signature'
})[
0
]
var
signature
=
data
.
signature
expect
(
signature
.
attribute
).
toEqual
(
'signature'
)
expect
(
signature
.
type
).
toEqual
(
'VARCHAR(255)'
)
expect
(
signature
.
allowNull
).
toEqual
(
false
)
expect
(
signature
.
defaultValue
).
toEqual
(
'Signature'
)
...
...
@@ -261,8 +257,8 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() {
this
.
init
({
to
:
20111206163300
},
function
(
migrator
)
{
migrator
.
migrate
().
success
(
function
(){
this
.
sequelize
.
getQueryInterface
().
describeTable
(
'User'
).
success
(
function
(
data
)
{
var
signature
=
data
.
filter
(
function
(
hash
){
return
hash
.
attribute
===
'signature'
})[
0
]
,
sig
=
data
.
filter
(
function
(
hash
){
return
hash
.
attribute
===
'sig'
})[
0
]
var
signature
=
data
.
signature
,
sig
=
data
.
sig
expect
(
signature
).
toBeFalsy
()
expect
(
sig
).
toBeTruthy
()
...
...
spec/query-interface.spec.js
View file @
c8d4966
...
...
@@ -101,15 +101,13 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() {
this
.
interface
.
describeTable
(
'User'
).
complete
(
function
(
err
,
metadata
)
{
expect
(
err
).
toBeNull
()
var
username
=
metadata
.
filter
(
function
(
m
)
{
return
m
.
attribute
===
'username'
})[
0
]
var
isAdmin
=
metadata
.
filter
(
function
(
m
)
{
return
m
.
attribute
===
'isAdmin'
})[
0
]
var
username
=
metadata
.
username
var
isAdmin
=
metadata
.
isAdmin
expect
(
username
.
attribute
).
toEqual
(
'username'
)
expect
(
username
.
type
).
toEqual
(
dialect
===
'postgres'
?
'CHARACTER VARYING'
:
'VARCHAR(255)'
)
expect
(
username
.
allowNull
).
toBeTrue
()
expect
(
username
.
defaultValue
).
toBeNull
()
expect
(
isAdmin
.
attribute
).
toEqual
(
'isAdmin'
)
expect
(
isAdmin
.
type
).
toEqual
(
dialect
===
'postgres'
?
'BOOLEAN'
:
'TINYINT(1)'
)
expect
(
isAdmin
.
allowNull
).
toBeTrue
()
expect
(
isAdmin
.
defaultValue
).
toBeNull
()
...
...
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