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 058666ae
authored
Dec 07, 2011
by
sdepold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
renameColumn
1 parent
c359834f
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
131 additions
and
86 deletions
lib/connectors/mysql/query-generator.js
lib/connectors/query-generator.js
lib/query-interface.js
spec/assets/migrations/20111206163300-renameSignatureColumnOfUserToSig.js
spec/migrator.spec.js
lib/connectors/mysql/query-generator.js
View file @
058666a
...
@@ -74,6 +74,19 @@ var QueryGenerator = module.exports = {
...
@@ -74,6 +74,19 @@ var QueryGenerator = module.exports = {
return
Utils
.
_
.
template
(
query
)({
tableName
:
tableName
,
attributes
:
attrString
})
return
Utils
.
_
.
template
(
query
)({
tableName
:
tableName
,
attributes
:
attrString
})
},
},
renameColumnQuery
:
function
(
tableName
,
attrBefore
,
attributes
)
{
var
query
=
"ALTER TABLE `<%= tableName %>` CHANGE <%= attributes %>;"
var
attrString
=
Utils
.
_
.
map
(
attributes
,
function
(
definition
,
attributeName
)
{
return
Utils
.
_
.
template
(
'`<%= before %>` `<%= after %>` <%= definition %>'
)({
before
:
attrBefore
,
after
:
attributeName
,
definition
:
definition
})
}).
join
(
', '
)
return
Utils
.
_
.
template
(
query
)({
tableName
:
tableName
,
attributes
:
attrString
})
},
selectQuery
:
function
(
tableName
,
options
)
{
selectQuery
:
function
(
tableName
,
options
)
{
options
=
options
||
{}
options
=
options
||
{}
options
.
table
=
Utils
.
addTicks
(
tableName
)
options
.
table
=
Utils
.
addTicks
(
tableName
)
...
...
lib/connectors/query-generator.js
View file @
058666a
...
@@ -52,16 +52,28 @@ module.exports = (function() {
...
@@ -52,16 +52,28 @@ module.exports = (function() {
/*
/*
Returns a query, which modifies an existing attribute from a table.
Returns a query, which modifies an existing attribute from a table.
- tableName: Name of the existing table.
Parameters:
- attributes: A hash with attribute-attributeOptions-pairs.
- tableName: Name of the existing table.
- key: attributeName
- attributes: A hash with attribute-attributeOptions-pairs.
- value: A hash with attribute specific options:
- key: attributeName
- type: DataType
- value: A hash with attribute specific options:
- defaultValue: A String with the default value
- type: DataType
- allowNull: Boolean
- defaultValue: A String with the default value
- allowNull: Boolean
*/
*/
changeColumnQuery
:
function
(
tableName
,
attribute
)
{
changeColumnQuery
:
function
(
tableName
,
attribute
)
{
throw
new
Error
(
'DEfine the method modifyColumnQuery!'
)
throw
new
Error
(
'Define the method modifyColumnQuery!'
)
},
/*
Returns a query, which renames an existing attribute.
Parameters:
- tableName: Name of an existing table.
- attrNameBefore: The name of the attribute, which shall be renamed.
- attrNameAfter: The name of the attribute, after renaming.
*/
renameColumnQuery
:
function
(
tableName
,
attrNameBefore
,
attrNameAfter
)
{
throw
new
Error
(
'Define the method renameColumnQuery!'
)
},
},
/*
/*
...
...
lib/query-interface.js
View file @
058666a
...
@@ -9,25 +9,17 @@ module.exports = (function() {
...
@@ -9,25 +9,17 @@ module.exports = (function() {
Utils
.
addEventEmitter
(
QueryInterface
)
Utils
.
addEventEmitter
(
QueryInterface
)
QueryInterface
.
prototype
.
createTable
=
function
(
tableName
,
attributes
,
options
)
{
QueryInterface
.
prototype
.
createTable
=
function
(
tableName
,
attributes
,
options
)
{
var
_query
=
query
.
call
(
this
,
this
.
QueryGenerator
.
createTableQuery
(
tableName
,
attributes
,
options
))
var
sql
=
this
.
QueryGenerator
.
createTableQuery
(
tableName
,
attributes
,
options
)
,
self
=
this
,
self
=
this
_query
.
success
(
function
(){
self
.
emit
(
'createTable'
,
null
)
})
.
error
(
function
(
err
){
self
.
emit
(
'createTable'
,
err
)})
return
_query
return
queryAndEmit
.
call
(
this
,
sql
,
'createTable'
)
}
}
QueryInterface
.
prototype
.
dropTable
=
function
(
tableName
)
{
QueryInterface
.
prototype
.
dropTable
=
function
(
tableName
)
{
var
_query
=
query
.
call
(
this
,
this
.
QueryGenerator
.
dropTableQuery
(
tableName
))
var
sql
=
this
.
QueryGenerator
.
dropTableQuery
(
tableName
)
,
self
=
this
,
self
=
this
_query
.
success
(
function
(){
self
.
emit
(
'dropTable'
,
null
)
})
.
error
(
function
(
err
){
self
.
emit
(
'dropTable'
,
err
)})
return
_query
return
queryAndEmit
.
call
(
this
,
sql
,
'dropTable'
)
}
}
QueryInterface
.
prototype
.
dropAllTables
=
function
()
{
QueryInterface
.
prototype
.
dropAllTables
=
function
()
{
...
@@ -58,15 +50,10 @@ module.exports = (function() {
...
@@ -58,15 +50,10 @@ module.exports = (function() {
}
}
QueryInterface
.
prototype
.
renameTable
=
function
(
before
,
after
)
{
QueryInterface
.
prototype
.
renameTable
=
function
(
before
,
after
)
{
var
_query
=
query
.
call
(
this
,
this
.
QueryGenerator
.
renameTableQuery
(
before
,
after
))
var
sql
=
this
.
QueryGenerator
.
renameTableQuery
(
before
,
after
)
,
self
=
this
,
self
=
this
_query
.
success
(
function
(){
self
.
emit
(
'renameTable'
,
null
)
})
.
error
(
function
(
err
){
self
.
emit
(
'renameTable'
,
err
)})
return
_query
return
queryAndEmit
.
call
(
this
,
sql
,
'renameTable'
)
}
}
QueryInterface
.
prototype
.
showAllTables
=
function
()
{
QueryInterface
.
prototype
.
showAllTables
=
function
()
{
...
@@ -108,33 +95,17 @@ module.exports = (function() {
...
@@ -108,33 +95,17 @@ module.exports = (function() {
attributes
[
attributeName
]
=
dataTypeOrOptions
attributes
[
attributeName
]
=
dataTypeOrOptions
var
options
=
Utils
.
simplifyAttributes
(
attributes
)
var
options
=
Utils
.
simplifyAttributes
(
attributes
)
,
query
=
this
.
QueryGenerator
.
addColumnQuery
(
tableName
,
options
)
,
sql
=
this
.
QueryGenerator
.
addColumnQuery
(
tableName
,
options
)
,
self
=
this
,
self
=
this
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
return
queryAndEmit
.
call
(
this
,
sql
,
'addColumn'
)
self
.
sequelize
.
query
(
query
).
success
(
function
()
{
self
.
emit
(
'addColumn'
,
null
)
emitter
.
emit
(
'success'
,
null
)
}).
error
(
function
(
err
)
{
self
.
emit
(
'addColumn'
,
err
)
emitter
.
emit
(
'failure'
,
err
)
})
}).
run
()
}
}
QueryInterface
.
prototype
.
removeColumn
=
function
(
tableName
,
attributeName
)
{
QueryInterface
.
prototype
.
removeColumn
=
function
(
tableName
,
attributeName
)
{
var
s
elf
=
this
var
s
ql
=
this
.
QueryGenerator
.
removeColumnQuery
(
tableName
,
attributeName
)
,
query
=
this
.
QueryGenerator
.
removeColumnQuery
(
tableName
,
attributeName
)
,
self
=
this
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
return
queryAndEmit
.
call
(
this
,
sql
,
'removeColumn'
)
self
.
sequelize
.
query
(
query
).
success
(
function
()
{
self
.
emit
(
'removeColumn'
,
null
)
emitter
.
emit
(
'success'
,
null
)
}).
error
(
function
(
err
)
{
self
.
emit
(
'removeColumn'
,
err
)
emitter
.
emit
(
'failure'
,
err
)
})
}).
run
()
}
}
QueryInterface
.
prototype
.
changeColumn
=
function
(
tableName
,
attributeName
,
dataTypeOrOptions
)
{
QueryInterface
.
prototype
.
changeColumn
=
function
(
tableName
,
attributeName
,
dataTypeOrOptions
)
{
...
@@ -146,23 +117,44 @@ module.exports = (function() {
...
@@ -146,23 +117,44 @@ module.exports = (function() {
attributes
[
attributeName
]
=
dataTypeOrOptions
attributes
[
attributeName
]
=
dataTypeOrOptions
var
options
=
Utils
.
simplifyAttributes
(
attributes
)
var
options
=
Utils
.
simplifyAttributes
(
attributes
)
,
query
=
this
.
QueryGenerator
.
changeColumnQuery
(
tableName
,
options
)
,
sql
=
this
.
QueryGenerator
.
changeColumnQuery
(
tableName
,
options
)
,
self
=
this
,
self
=
this
return
queryAndEmit
.
call
(
this
,
sql
,
'changeColumn'
)
}
QueryInterface
.
prototype
.
renameColumn
=
function
(
tableName
,
attrNameBefore
,
attrNameAfter
)
{
var
self
=
this
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
self
.
sequelize
.
query
(
query
).
success
(
function
()
{
self
.
describeTable
(
tableName
).
success
(
function
(
data
)
{
self
.
emit
(
'changeColumn'
,
null
)
data
=
data
.
filter
(
function
(
h
)
{
return
h
.
Field
==
attrNameBefore
})[
0
]
emitter
.
emit
(
'success'
,
null
)
var
options
=
{}
options
[
attrNameAfter
]
=
{
type
:
data
.
Type
,
allowNull
:
data
.
Null
==
'YES'
,
defaultValue
:
data
.
Default
}
var
sql
=
self
.
QueryGenerator
.
renameColumnQuery
(
tableName
,
attrNameBefore
,
Utils
.
simplifyAttributes
(
options
)
)
self
.
sequelize
.
query
(
sql
).
success
(
function
()
{
self
.
emit
(
'renameColumn'
,
null
)
emitter
.
emit
(
'success'
,
null
)
}).
error
(
function
(
err
)
{
self
.
emit
(
'renameColumn'
,
err
)
emitter
.
emit
(
'failure'
,
err
)
})
}).
error
(
function
(
err
)
{
}).
error
(
function
(
err
)
{
self
.
emit
(
'
chang
eColumn'
,
err
)
self
.
emit
(
'
renam
eColumn'
,
err
)
emitter
.
emit
(
'failure'
,
err
)
emitter
.
emit
(
'failure'
,
err
)
})
})
}).
run
()
}).
run
()
}
QueryInterface
.
prototype
.
renameColumn
=
function
()
{
}
}
QueryInterface
.
prototype
.
addIndex
=
function
()
{
QueryInterface
.
prototype
.
addIndex
=
function
()
{
...
@@ -193,32 +185,20 @@ module.exports = (function() {
...
@@ -193,32 +185,20 @@ module.exports = (function() {
}
}
QueryInterface
.
prototype
.
update
=
function
(
model
,
tableName
,
values
,
identifier
)
{
QueryInterface
.
prototype
.
update
=
function
(
model
,
tableName
,
values
,
identifier
)
{
var
sql
=
this
.
QueryGenerator
.
updateQuery
(
tableName
,
values
,
identifier
)
var
sql
=
this
.
QueryGenerator
.
updateQuery
(
tableName
,
values
,
identifier
)
,
_query
=
query
.
call
(
this
,
sql
,
model
)
,
self
=
this
,
self
=
this
_query
.
success
(
function
(){
self
.
emit
(
'update'
,
null
)
})
.
error
(
function
(
err
){
self
.
emit
(
'update'
,
err
)})
return
_query
return
queryAndEmit
.
call
(
this
,
sql
,
'update'
)
}
}
QueryInterface
.
prototype
.
delete
=
function
(
model
,
tableName
,
identifier
)
{
QueryInterface
.
prototype
.
delete
=
function
(
model
,
tableName
,
identifier
)
{
var
sql
=
this
.
QueryGenerator
.
deleteQuery
(
tableName
,
identifier
)
var
sql
=
this
.
QueryGenerator
.
deleteQuery
(
tableName
,
identifier
)
,
_query
=
query
.
call
(
this
,
sql
,
model
)
,
self
=
this
,
self
=
this
_query
.
success
(
function
(){
self
.
emit
(
'delete'
,
null
)
})
.
error
(
function
(
err
){
self
.
emit
(
'delete'
,
err
)})
return
_query
return
queryAndEmit
.
call
(
this
,
sql
,
'delete'
)
}
}
QueryInterface
.
prototype
.
select
=
function
(
factory
,
tableName
,
options
,
queryOptions
)
{
QueryInterface
.
prototype
.
select
=
function
(
factory
,
tableName
,
options
,
queryOptions
)
{
queryOptions
=
queryOptions
||
{}
var
sql
=
this
.
QueryGenerator
.
selectQuery
(
tableName
,
options
)
var
sql
=
this
.
QueryGenerator
.
selectQuery
(
tableName
,
options
)
,
_query
=
query
.
call
(
this
,
sql
,
factory
,
queryOptions
)
,
_query
=
query
.
call
(
this
,
sql
,
factory
,
queryOptions
)
,
self
=
this
,
self
=
this
...
@@ -250,9 +230,22 @@ module.exports = (function() {
...
@@ -250,9 +230,22 @@ module.exports = (function() {
}).
run
()
}).
run
()
}
}
// private
// private
var
queryAndEmit
=
function
(
sql
,
methodName
)
{
var
self
=
this
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
self
.
sequelize
.
query
(
sql
).
success
(
function
()
{
self
.
emit
(
methodName
,
null
)
emitter
.
emit
(
'success'
,
null
)
}).
error
(
function
(
err
)
{
self
.
emit
(
methodName
,
err
)
emitter
.
emit
(
'failure'
,
err
)
})
}).
run
()
}
var
query
=
function
()
{
var
query
=
function
()
{
var
args
=
Utils
.
_
.
map
(
arguments
,
function
(
arg
,
_
)
{
return
arg
})
var
args
=
Utils
.
_
.
map
(
arguments
,
function
(
arg
,
_
)
{
return
arg
})
...
...
spec/assets/migrations/20111206163300-renameSignatureColumnOfUserToSig.js
0 → 100644
View file @
058666a
module
.
exports
=
{
up
:
function
(
migration
,
DataTypes
)
{
migration
.
renameColumn
(
'User'
,
'signature'
,
'sig'
)
},
down
:
function
(
migration
,
DataTypes
)
{}
}
spec/migrator.spec.js
View file @
058666a
...
@@ -92,12 +92,8 @@ describe('Migrator', function() {
...
@@ -92,12 +92,8 @@ describe('Migrator', function() {
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
SequelizeMeta
.
create
({
lastMigrationId
:
'20111117063700'
}).
success
(
function
()
{
SequelizeMeta
.
create
({
lastMigrationId
:
'20111117063700'
}).
success
(
function
()
{
migrator
.
getUndoneMigrations
(
function
(
err
,
migrations
)
{
migrator
.
getUndoneMigrations
(
function
(
err
,
migrations
)
{
migrations
=
migrations
.
sort
(
function
(
a
,
b
){
return
parseInt
(
a
.
filename
.
split
(
'-'
)[
0
])
-
parseInt
(
b
.
filename
.
split
(
'-'
)[
0
])
})
expect
(
err
).
toBeFalsy
()
expect
(
err
).
toBeFalsy
()
expect
(
migrations
.
length
).
toEqual
(
6
)
expect
(
migrations
.
length
).
toEqual
(
7
)
expect
(
migrations
[
0
].
filename
).
toEqual
(
'20111123060700-addBirthdateToPerson.js'
)
expect
(
migrations
[
0
].
filename
).
toEqual
(
'20111123060700-addBirthdateToPerson.js'
)
done
()
done
()
})
})
...
@@ -254,7 +250,7 @@ describe('Migrator', function() {
...
@@ -254,7 +250,7 @@ describe('Migrator', function() {
})
})
describe
(
'changeColumn'
,
function
()
{
describe
(
'changeColumn'
,
function
()
{
it
(
'changes the signature column from user to default "" + notNull'
,
function
()
{
it
(
'changes the signature column from user to default "
signature
" + notNull'
,
function
()
{
setup
({
from
:
20111205064000
,
to
:
20111206063000
})
setup
({
from
:
20111205064000
,
to
:
20111206063000
})
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
...
@@ -278,5 +274,29 @@ describe('Migrator', function() {
...
@@ -278,5 +274,29 @@ describe('Migrator', function() {
})
})
})
})
})
})
describe
(
'renameColumn'
,
function
()
{
it
(
"renames the signature column from user to sig"
,
function
()
{
setup
({
from
:
20111117063700
,
to
:
20111206163300
})
Helpers
.
async
(
function
(
done
)
{
migrator
.
migrate
().
success
(
done
).
error
(
function
(
err
)
{
console
.
log
(
err
)
})
})
Helpers
.
async
(
function
(
done
)
{
sequelize
.
getQueryInterface
().
describeTable
(
'User'
).
success
(
function
(
data
)
{
var
signature
=
data
.
filter
(
function
(
hash
){
return
hash
.
Field
==
'signature'
})[
0
]
,
sig
=
data
.
filter
(
function
(
hash
){
return
hash
.
Field
==
'sig'
})[
0
]
expect
(
signature
).
toBeFalsy
()
expect
(
sig
).
toBeTruthy
()
done
()
}).
error
(
function
(
err
)
{
console
.
log
(
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