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 c359834f
authored
Dec 06, 2011
by
sdepold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
changeColumn
1 parent
552fc607
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
91 additions
and
21 deletions
lib/connectors/mysql/query-generator.js
lib/connectors/query-generator.js
lib/query-interface.js
spec/assets/migrations/20111206063000-changeSignatureColumnOfUserToMendatory.js
spec/migrator.spec.js
lib/connectors/mysql/query-generator.js
View file @
c359834
...
...
@@ -62,19 +62,18 @@ var QueryGenerator = module.exports = {
return
Utils
.
_
.
template
(
query
)({
tableName
:
tableName
,
attributeName
:
attributeName
})
},
/*
Returns a query for selecting elements in the database <tableName>.
Options:
- attributes -> An array of attributes (e.g. ['name', 'birthday']). Default: *
- where -> A hash with conditions (e.g. {name: 'foo'})
OR an ID as integer
OR a string with conditions (e.g. 'name="foo"').
If you use a string, you have to escape it on your own.
- order -> e.g. 'id DESC'
- group
- limit -> The maximum count you want to get.
- offset -> An offset value to start from. Only useable with limit!
*/
changeColumnQuery
:
function
(
tableName
,
attributes
)
{
var
query
=
"ALTER TABLE `<%= tableName %>` CHANGE <%= attributes %>;"
var
attrString
=
Utils
.
_
.
map
(
attributes
,
function
(
definition
,
attributeName
)
{
return
Utils
.
_
.
template
(
'`<%= attributeName %>` `<%= attributeName %>` <%= definition %>'
)({
attributeName
:
attributeName
,
definition
:
definition
})
}).
join
(
', '
)
return
Utils
.
_
.
template
(
query
)({
tableName
:
tableName
,
attributes
:
attrString
})
},
selectQuery
:
function
(
tableName
,
options
)
{
options
=
options
||
{}
options
.
table
=
Utils
.
addTicks
(
tableName
)
...
...
lib/connectors/query-generator.js
View file @
c359834
...
...
@@ -29,13 +29,14 @@ module.exports = (function() {
Returns a query, which adds an attribute to an existing table.
Parameters:
- tableName: Name of the existing table.
- attributeName: Name of the new attribute.
- options: A hash with attribute specific options:
- type: DataType
- defaultValue: A String with the default value
- allowNull: Boolean
- attributes: A hash with attribute-attributeOptions-pairs.
- key: attributeName
- value: A hash with attribute specific options:
- type: DataType
- defaultValue: A String with the default value
- allowNull: Boolean
*/
addColumnQuery
:
function
(
tableName
,
attribute
Name
,
option
s
)
{
addColumnQuery
:
function
(
tableName
,
attributes
)
{
throw
new
Error
(
'Define the method addColumnQuery!'
)
},
...
...
@@ -50,6 +51,20 @@ module.exports = (function() {
},
/*
Returns a query, which modifies an existing attribute from a table.
- tableName: Name of the existing table.
- attributes: A hash with attribute-attributeOptions-pairs.
- key: attributeName
- value: A hash with attribute specific options:
- type: DataType
- defaultValue: A String with the default value
- allowNull: Boolean
*/
changeColumnQuery
:
function
(
tableName
,
attribute
)
{
throw
new
Error
(
'DEfine the method modifyColumnQuery!'
)
},
/*
Returns a query for selecting elements in the table <tableName>.
Options:
- attributes -> An array of attributes (e.g. ['name', 'birthday']). Default: *
...
...
lib/query-interface.js
View file @
c359834
...
...
@@ -137,7 +137,27 @@ module.exports = (function() {
}).
run
()
}
QueryInterface
.
prototype
.
changeColumn
=
function
()
{
QueryInterface
.
prototype
.
changeColumn
=
function
(
tableName
,
attributeName
,
dataTypeOrOptions
)
{
var
attributes
=
{}
if
(
Utils
.
_
.
values
(
DataTypes
).
indexOf
(
dataTypeOrOptions
)
>
-
1
)
attributes
[
attributeName
]
=
{
type
:
dataTypeOrOptions
,
allowNull
:
false
}
else
attributes
[
attributeName
]
=
dataTypeOrOptions
var
options
=
Utils
.
simplifyAttributes
(
attributes
)
,
query
=
this
.
QueryGenerator
.
changeColumnQuery
(
tableName
,
options
)
,
self
=
this
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
self
.
sequelize
.
query
(
query
).
success
(
function
()
{
self
.
emit
(
'changeColumn'
,
null
)
emitter
.
emit
(
'success'
,
null
)
}).
error
(
function
(
err
)
{
self
.
emit
(
'changeColumn'
,
err
)
emitter
.
emit
(
'failure'
,
err
)
})
}).
run
()
}
...
...
spec/assets/migrations/20111206063000-changeSignatureColumnOfUserToMendatory.js
0 → 100644
View file @
c359834
module
.
exports
=
{
up
:
function
(
migration
,
DataTypes
)
{
migration
.
changeColumn
(
'User'
,
'signature'
,
{
type
:
DataTypes
.
STRING
,
allowNull
:
false
,
defaultValue
:
'Signature'
})
},
down
:
function
(
migration
,
DataTypes
)
{}
}
spec/migrator.spec.js
View file @
c359834
...
...
@@ -97,7 +97,7 @@ describe('Migrator', function() {
})
expect
(
err
).
toBeFalsy
()
expect
(
migrations
.
length
).
toEqual
(
5
)
expect
(
migrations
.
length
).
toEqual
(
6
)
expect
(
migrations
[
0
].
filename
).
toEqual
(
'20111123060700-addBirthdateToPerson.js'
)
done
()
})
...
...
@@ -252,6 +252,31 @@ describe('Migrator', function() {
})
})
describe
(
'changeColumn'
,
function
()
{
it
(
'changes the signature column from user to default "" + notNull'
,
function
()
{
setup
({
from
:
20111205064000
,
to
:
20111206063000
})
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
]
expect
(
signature
.
Field
).
toEqual
(
'signature'
)
expect
(
signature
.
Type
).
toEqual
(
'varchar(255)'
)
expect
(
signature
.
Null
).
toEqual
(
'NO'
)
expect
(
signature
.
Default
).
toEqual
(
'Signature'
)
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