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 9a341ed6
authored
Sep 23, 2013
by
Jonathan M. Altman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed failing tests, tried to match style guidelines
1 parent
b7489045
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
104 additions
and
106 deletions
lib/dialects/postgres/query-generator.js
lib/query-interface.js
test/migrator.test.js
lib/dialects/postgres/query-generator.js
View file @
9a341ed
...
...
@@ -728,36 +728,36 @@ module.exports = (function() {
,
'<%= eventType %> <%= eventSpec %>'
,
'ON <%= tableName %>'
,
'<%= optionsSpec %>'
,
'EXECUTE PROCEDURE <%= functionName %>(<%= paramList %>)'
].
join
(
'\n\t'
);
,
'EXECUTE PROCEDURE <%= functionName %>(<%= paramList %>)
;
'
].
join
(
'\n\t'
)
return
Utils
.
_
.
template
(
sql
)({
constraintVal
:
this
.
triggerEventTypeIsConstraint
(
eventType
)
,
triggerName
:
triggerName
,
eventType
:
this
.
decodeTriggerEventType
(
eventType
)
,
eventSpec
:
this
.
expandTriggerEventSpec
(
fireOnSpec
)
,
tableName
:
tableName
,
optionsSpec
:
this
.
expandOptions
(
optionsArray
)
,
functionName
:
functionName
,
paramList
:
this
.
expandFunctionParamList
(
functionParams
)
constraintVal
:
this
.
triggerEventTypeIsConstraint
(
eventType
)
,
triggerName
:
triggerName
,
eventType
:
this
.
decodeTriggerEventType
(
eventType
),
eventSpec
:
this
.
expandTriggerEventSpec
(
fireOnSpec
),
tableName
:
tableName
,
optionsSpec
:
this
.
expandOptions
(
optionsArray
),
functionName
:
functionName
,
paramList
:
this
.
expandFunctionParamList
(
functionParams
)
})
},
dropTrigger
:
function
(
tableName
,
triggerName
)
{
var
sql
=
'DROP TRIGGER <%= triggerName %> ON <%= tableName %> RESTRICT'
var
sql
=
'DROP TRIGGER <%= triggerName %> ON <%= tableName %> RESTRICT
;
'
return
Utils
.
_
.
template
(
sql
)({
triggerName
:
triggerName
,
tableName
:
tableName
})
;
triggerName
:
triggerName
,
tableName
:
tableName
})
},
renameTrigger
:
function
(
tableName
,
oldTriggerName
,
newTriggerName
)
{
var
sql
=
'ALTER TRIGGER <%= oldTriggerName %> ON <%= tableName %> RENAME TO <%= newTriggerName%>'
var
sql
=
'ALTER TRIGGER <%= oldTriggerName %> ON <%= tableName %> RENAME TO <%= newTriggerName%>
;
'
return
Utils
.
_
.
template
(
sql
)({
tableName
:
tableName
,
oldTriggerName
:
oldTriggerName
,
newTriggerName
:
newTriggerName
})
;
tableName
:
tableName
,
oldTriggerName
:
oldTriggerName
,
newTriggerName
:
newTriggerName
})
},
createFunction
:
function
(
functionName
,
params
,
returnType
,
language
,
body
,
options
)
{
...
...
@@ -767,7 +767,7 @@ module.exports = (function() {
,
"\t<%= body %>"
,
"END;"
,
"$$ language '<%= language %>'<%= options %>;"
].
join
(
'\n'
)
;
].
join
(
'\n'
)
return
Utils
.
_
.
template
(
sql
)({
functionName
:
functionName
,
...
...
@@ -781,21 +781,20 @@ module.exports = (function() {
dropFunction
:
function
(
functionName
,
params
)
{
// RESTRICT is (currently, as of 9.2) default but we'll be explicit
var
sql
=
'DROP FUNCTION <%= functionName %>(<%= paramList %>) RESTRICT
'
;
var
sql
=
'DROP FUNCTION <%= functionName %>(<%= paramList %>) RESTRICT
;'
return
Utils
.
_
.
template
(
sql
)({
functionName
:
functionName
,
paramList
:
this
.
expandFunctionParamList
(
params
)
})
;
})
},
renameFunction
:
function
(
oldFunctionName
,
params
,
newFunctionName
)
{
// RESTRICT is (currently, as of 9.2) default but we'll be explicit
var
sql
=
'ALTER FUNCTION <%= oldFunctionName %>(<%= paramList %>) RENAME TO <%= newFunctionName %>'
;
var
sql
=
'ALTER FUNCTION <%= oldFunctionName %>(<%= paramList %>) RENAME TO <%= newFunctionName %>;'
return
Utils
.
_
.
template
(
sql
)({
oldFunctionName
:
oldFunctionName
,
paramList
:
this
.
expandFunctionParamList
(
params
),
newFunctionName
:
newFunctionName
})
;
})
},
databaseConnectionUri
:
function
(
config
)
{
...
...
@@ -817,74 +816,73 @@ module.exports = (function() {
expandFunctionParamList
:
function
expandFunctionParamList
(
params
)
{
if
(
Utils
.
_
.
isUndefined
(
params
)
||
!
Utils
.
_
.
isArray
(
params
))
{
throw
new
Error
(
"expandFunctionParamList: function parameters array required, including an empty one for no arguments"
)
;
throw
new
Error
(
"expandFunctionParamList: function parameters array required, including an empty one for no arguments"
)
}
var
paramList
=
Utils
.
_
.
each
(
params
,
function
expandParam
(
curParam
){
paramDef
=
[]
;
paramDef
=
[]
if
(
Utils
.
_
.
has
(
curParam
,
'type'
))
{
if
(
Utils
.
_
.
has
(
curParam
,
'direction'
))
{
paramDef
.
push
(
curParam
[
'direction'
]);
}
if
(
Utils
.
_
.
has
(
curParam
,
'name'
))
{
paramDef
.
push
(
curParam
[
'name'
]);
}
paramDef
.
push
(
curParam
[
'type'
]);
if
(
Utils
.
_
.
has
(
curParam
,
'direction'
))
{
paramDef
.
push
(
curParam
.
direction
)
}
if
(
Utils
.
_
.
has
(
curParam
,
'name'
))
{
paramDef
.
push
(
curParam
.
name
)
}
paramDef
.
push
(
curParam
.
type
)
}
else
{
throw
new
Error
(
'createFunction called with a parameter with no type'
)
;
throw
new
Error
(
'createFunction called with a parameter with no type'
)
}
return
paramDef
.
join
(
' '
)
;
})
;
return
paramList
.
join
(
', '
)
;
return
paramDef
.
join
(
' '
)
})
return
paramList
.
join
(
', '
)
},
expandOptions
:
function
expandOptions
(
options
)
{
return
Utils
.
_
.
isUndefined
(
options
)
||
Utils
.
_
.
isEmpty
(
options
)
?
''
:
'\n\t'
+
options
.
join
(
'\n\t'
);
''
:
'\n\t'
+
options
.
join
(
'\n\t'
)
},
decodeTriggerEventType
:
function
decodeTriggerEventType
(
eventSpecifier
)
{
var
EVENT_DECODER
=
{
'after'
:
'AFTER'
,
'before'
:
'BEFORE'
,
'instead_of'
:
'INSTEAD OF'
,
'after_constraint'
:
'AFTER'
'after'
:
'AFTER'
,
'before'
:
'BEFORE'
,
'instead_of'
:
'INSTEAD OF'
,
'after_constraint'
:
'AFTER'
}
if
(
!
Utils
.
_
.
has
(
EVENT_DECODER
,
eventSpecifier
))
{
throw
new
Error
(
'Invalid trigger event specified: '
+
eventSpecifier
)
;
throw
new
Error
(
'Invalid trigger event specified: '
+
eventSpecifier
)
}
return
EVENT_DECODER
[
eventSpecifier
]
;
return
EVENT_DECODER
[
eventSpecifier
]
},
triggerEventTypeIsConstraint
:
function
triggerEventTypeIsConstraint
(
eventSpecifier
)
{
return
eventSpecifier
===
'after_constrain'
?
'CONSTRAINT '
:
''
;
return
eventSpecifier
===
'after_constrain'
?
'CONSTRAINT '
:
''
},
expandTriggerEventSpec
:
function
expandTriggerEventSpec
(
fireOnSpec
)
{
if
(
Utils
.
_
.
isEmpty
(
fireOnSpec
))
{
throw
new
Error
(
'no table change events specified to trigger on'
)
;
throw
new
Error
(
'no table change events specified to trigger on'
)
}
return
Utils
.
_
.
map
(
fireOnSpec
,
function
parseTriggerEventSpec
(
fireValue
,
fireKey
){
var
EVENT_MAP
=
{
'insert'
:
'INSERT'
,
'update'
:
'UPDATE'
,
'delete'
:
'DELETE'
,
'truncate'
:
'TRUNCATE'
}
;
'insert'
:
'INSERT'
,
'update'
:
'UPDATE'
,
'delete'
:
'DELETE'
,
'truncate'
:
'TRUNCATE'
}
if
(
!
Utils
.
_
.
has
(
EVENT_MAP
,
fireKey
))
{
throw
new
Error
(
'parseTriggerEventSpec: undefined trigger event '
+
fireKey
)
;
throw
new
Error
(
'parseTriggerEventSpec: undefined trigger event '
+
fireKey
)
}
var
eventSpec
=
EVENT_MAP
[
fireKey
]
;
var
eventSpec
=
EVENT_MAP
[
fireKey
]
if
(
eventSpec
===
'UPDATE'
)
{
if
(
Utils
.
_
.
isArray
(
fireValue
)
&&
fireValue
.
length
>
0
)
{
eventSpec
+=
' OF '
+
fireValue
.
join
(
', '
)
;
eventSpec
+=
' OF '
+
fireValue
.
join
(
', '
)
}
}
return
eventSpec
;
}).
join
(
' OR '
)
;
return
eventSpec
}).
join
(
' OR '
)
},
pgListEnums
:
function
(
tableName
,
attrName
,
options
)
{
...
...
lib/query-interface.js
View file @
9a341ed
...
...
@@ -631,7 +631,7 @@ module.exports = (function() {
QueryInterface
.
prototype
.
createTrigger
=
function
(
tableName
,
triggerName
,
timingType
,
fireOnArray
,
functionName
,
functionParams
,
optionsArray
)
{
var
sql
=
this
.
QueryGenerator
.
createTrigger
(
tableName
,
triggerName
,
timingType
,
fireOnArray
,
functionName
,
functionParams
,
optionsArray
)
;
,
functionParams
,
optionsArray
)
if
(
sql
){
return
queryAndEmit
.
call
(
this
,
sql
,
'createTrigger'
)
}
else
{
...
...
@@ -667,7 +667,7 @@ module.exports = (function() {
}
QueryInterface
.
prototype
.
createFunction
=
function
(
functionName
,
params
,
returnType
,
language
,
body
,
options
)
{
var
sql
=
this
.
QueryGenerator
.
createFunction
(
functionName
,
params
,
returnType
,
language
,
body
,
options
)
;
var
sql
=
this
.
QueryGenerator
.
createFunction
(
functionName
,
params
,
returnType
,
language
,
body
,
options
)
if
(
sql
){
return
queryAndEmit
.
call
(
this
,
sql
,
'createFunction'
)
}
else
{
...
...
@@ -679,7 +679,7 @@ module.exports = (function() {
}
QueryInterface
.
prototype
.
dropFunction
=
function
(
functionName
,
params
)
{
var
sql
=
this
.
QueryGenerator
.
dropFunction
(
functionName
,
params
)
;
var
sql
=
this
.
QueryGenerator
.
dropFunction
(
functionName
,
params
)
if
(
sql
){
return
queryAndEmit
.
call
(
this
,
sql
,
'dropFunction'
)
}
else
{
...
...
@@ -691,7 +691,7 @@ module.exports = (function() {
}
QueryInterface
.
prototype
.
renameFunction
=
function
(
oldFunctionName
,
params
,
newFunctionName
)
{
var
sql
=
this
.
QueryGenerator
.
renameFunction
(
oldFunctionName
,
params
,
newFunctionName
)
;
var
sql
=
this
.
QueryGenerator
.
renameFunction
(
oldFunctionName
,
params
,
newFunctionName
)
if
(
sql
){
return
queryAndEmit
.
call
(
this
,
sql
,
'renameFunction'
)
}
else
{
...
...
test/migrator.test.js
View file @
9a341ed
...
...
@@ -311,41 +311,41 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
if
(
dialect
.
match
(
/^postgres/
))
{
describe
(
'function migrations'
,
function
(
done
)
{
var
generateFunctionCountQuery
=
function
generateFunctionCountQuery
(
functionName
,
languageOid
)
{
return
'SELECT * FROM pg_proc where prolang = '
+
languageOid
+
' AND proname = \''
+
functionName
+
'\''
;
};
var
FUNC_NAME
=
'get_an_answer'
;
var
RENAME_FUNC_NAME
=
'get_the_answer'
;
describe
(
'function migrations'
,
function
()
{
var
generateFunctionCountQuery
=
function
generateFunctionCountQuery
(
functionName
,
langName
)
{
return
[
'SELECT * FROM pg_proc p LEFT OUTER JOIN pg_language l ON (l.oid = p.prolang)'
,
'WHERE p.proname = \''
+
functionName
+
'\' AND l.lanname = \''
+
langName
+
'\';'
].
join
(
'\n'
)
}
var
FUNC_NAME
=
'get_an_answer'
var
RENAME_FUNC_NAME
=
'get_the_answer'
// Set up the table and trigger
before
(
function
(
done
){
var
self
=
this
this
.
init
({
from
:
20130909174103
,
to
:
20130909174103
},
function
(
migrator
)
{
migrator
.
migrate
().
success
(
function
(){
done
()
;
done
()
})
})
})
it
(
"creates a function "
+
FUNC_NAME
+
"()"
,
function
(
done
)
{
var
self
=
this
;
this
.
sequelize
.
query
(
generateFunctionCountQuery
(
FUNC_NAME
,
11771
)).
success
(
function
(
rows
){
this
.
sequelize
.
query
(
generateFunctionCountQuery
(
FUNC_NAME
,
'plpgsql'
)).
success
(
function
(
rows
){
expect
(
rows
.
length
).
to
.
equal
(
1
)
done
()
})
})
it
(
"renames a function "
+
FUNC_NAME
+
"() to "
+
RENAME_FUNC_NAME
+
"()"
,
function
(
done
)
{
var
self
=
this
;
var
self
=
this
this
.
init
({
from
:
20130909174253
,
to
:
20130909174253
},
function
(
migrator
)
{
migrator
.
migrate
().
success
(
function
(){
self
.
sequelize
.
query
(
generateFunctionCountQuery
(
FUNC_NAME
,
11771
)).
success
(
function
(
rows
){
expect
(
rows
.
length
).
to
.
equal
(
0
)
;
self
.
sequelize
.
query
(
generateFunctionCountQuery
(
RENAME_FUNC_NAME
,
11771
)).
success
(
function
(
rows
){
expect
(
rows
.
length
).
to
.
equal
(
1
)
;
self
.
sequelize
.
query
(
generateFunctionCountQuery
(
FUNC_NAME
,
'plpgsql'
)).
success
(
function
(
rows
){
expect
(
rows
.
length
).
to
.
equal
(
0
)
self
.
sequelize
.
query
(
generateFunctionCountQuery
(
RENAME_FUNC_NAME
,
'plpgsql'
)).
success
(
function
(
rows
){
expect
(
rows
.
length
).
to
.
equal
(
1
)
done
()
})
})
...
...
@@ -354,27 +354,27 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
})
it
(
"deletes a function "
+
RENAME_FUNC_NAME
+
"()"
,
function
(
done
)
{
var
self
=
this
;
var
self
=
this
this
.
init
({
from
:
20130909175000
,
to
:
20130909175000
},
function
(
migrator
)
{
migrator
.
migrate
().
success
(
function
(){
self
.
sequelize
.
query
(
generateFunctionCountQuery
(
RENAME_FUNC_NAME
,
11771
)).
success
(
function
(
rows
){
expect
(
rows
.
length
).
to
.
equal
(
0
)
;
done
()
;
self
.
sequelize
.
query
(
generateFunctionCountQuery
(
RENAME_FUNC_NAME
,
'plpgsql'
)).
success
(
function
(
rows
){
expect
(
rows
.
length
).
to
.
equal
(
0
)
done
()
})
})
})
})
})
describe
(
'test trigger migrations'
,
function
(
done
)
{
describe
(
'test trigger migrations'
,
function
()
{
var
generateTriggerCountQuery
=
function
generateTriggerCountQuery
(
triggerName
)
{
return
'SELECT * FROM pg_trigger where tgname = \''
+
triggerName
+
'\''
;
}
;
var
generate
FunctionCountQuery
=
function
generateFunction
CountQuery
(
functionName
,
schemaName
)
{
return
'SELECT * FROM pg_tables where tablename = \''
+
functionName
+
'\' and schemaname = \''
+
schemaName
+
'\''
;
}
;
var
TRIGGER_NAME
=
'updated_at'
;
var
RENAME_TRIGGER_NAME
=
'update_updated_at'
;
return
'SELECT * FROM pg_trigger where tgname = \''
+
triggerName
+
'\''
}
var
generate
TableCountQuery
=
function
generateTable
CountQuery
(
functionName
,
schemaName
)
{
return
'SELECT * FROM pg_tables where tablename = \''
+
functionName
+
'\' and schemaname = \''
+
schemaName
+
'\''
}
var
TRIGGER_NAME
=
'updated_at'
var
RENAME_TRIGGER_NAME
=
'update_updated_at'
var
TABLE_NAME
=
'trigger_test'
var
CATALOG_NAME
=
'public'
...
...
@@ -387,24 +387,24 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
"RETURN NEW;\n"
+
"END;\n"
+
"$$ language 'plpgsql';"
).
success
(
function
()
{
done
()})
;
).
success
(
function
()
{
done
()})
})
// Clean up the function
after
(
function
(
done
){
this
.
sequelize
.
query
(
"DROP FUNCTION IF EXISTS bump_updated_at()"
).
success
(
function
(){
done
();
})
;
this
.
sequelize
.
query
(
"DROP FUNCTION IF EXISTS bump_updated_at()"
).
success
(
function
(){
done
();
})
})
it
(
"creates a trigger updated_at on trigger_test"
,
function
(
done
)
{
var
self
=
this
;
var
self
=
this
this
.
init
({
from
:
20130909175939
,
to
:
20130909180846
},
function
(
migrator
)
{
migrator
.
migrate
().
success
(
function
(){
self
.
sequelize
.
query
(
generate
Function
CountQuery
(
TABLE_NAME
,
CATALOG_NAME
)).
success
(
function
(
rows
){
expect
(
rows
.
length
).
to
.
equal
(
1
)
;
self
.
sequelize
.
query
(
generate
Table
CountQuery
(
TABLE_NAME
,
CATALOG_NAME
)).
success
(
function
(
rows
){
expect
(
rows
.
length
).
to
.
equal
(
1
)
self
.
sequelize
.
query
(
generateTriggerCountQuery
(
TRIGGER_NAME
)).
success
(
function
(
rows
){
expect
(
rows
.
length
).
to
.
equal
(
1
)
;
done
()
;
expect
(
rows
.
length
).
to
.
equal
(
1
)
done
()
})
})
})
...
...
@@ -412,33 +412,33 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
})
it
(
"renames a trigger on "
+
TABLE_NAME
+
" from "
+
TRIGGER_NAME
+
" to "
+
RENAME_TRIGGER_NAME
,
function
(
done
){
var
self
=
this
;
var
self
=
this
this
.
init
({
from
:
20130909175939
,
to
:
20130909181148
},
function
(
migrator
)
{
migrator
.
migrate
().
success
(
function
(){
self
.
sequelize
.
query
(
generate
Function
CountQuery
(
TABLE_NAME
,
CATALOG_NAME
)).
success
(
function
(
rows
){
expect
(
rows
.
length
).
to
.
equal
(
1
)
;
self
.
sequelize
.
query
(
generate
Table
CountQuery
(
TABLE_NAME
,
CATALOG_NAME
)).
success
(
function
(
rows
){
expect
(
rows
.
length
).
to
.
equal
(
1
)
self
.
sequelize
.
query
(
generateTriggerCountQuery
(
RENAME_TRIGGER_NAME
)).
success
(
function
(
rows
){
expect
(
rows
.
length
).
to
.
equal
(
1
)
;
expect
(
rows
.
length
).
to
.
equal
(
1
)
self
.
sequelize
.
query
(
generateTriggerCountQuery
(
TRIGGER_NAME
)).
success
(
function
(
rows
){
expect
(
rows
.
length
).
to
.
equal
(
0
);
done
();
})
expect
(
rows
.
length
).
to
.
equal
(
0
)
done
()
})
})
})
})
})
})
})
it
(
"deletes a trigger "
+
TRIGGER_NAME
+
" on trigger_test"
,
function
(
done
)
{
var
self
=
this
;
var
self
=
this
this
.
init
({
from
:
20130909175939
,
to
:
20130909185621
},
function
(
migrator
)
{
migrator
.
migrate
().
success
(
function
(){
self
.
sequelize
.
query
(
generateTriggerCountQuery
(
TRIGGER_NAME
)).
success
(
function
(
rows
){
expect
(
rows
.
length
).
to
.
equal
(
0
)
;
expect
(
rows
.
length
).
to
.
equal
(
0
)
migrator
.
migrate
({
method
:
'down'
}).
success
(
function
(){
self
.
sequelize
.
query
(
generate
Function
CountQuery
(
TABLE_NAME
,
CATALOG_NAME
)).
success
(
function
(
rows
){
expect
(
rows
.
length
).
to
.
equal
(
0
)
;
done
()
;
self
.
sequelize
.
query
(
generate
Table
CountQuery
(
TABLE_NAME
,
CATALOG_NAME
)).
success
(
function
(
rows
){
expect
(
rows
.
length
).
to
.
equal
(
0
)
done
()
})
})
})
...
...
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