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 04b5fe87
authored
Sep 27, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of github.com:sequelize/sequelize
2 parents
45457c81
4eb42375
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
480 additions
and
5 deletions
lib/dialects/abstract/query-generator.js
lib/dialects/postgres/query-generator.js
lib/query-interface.js
test/assets/migrations/20130909174103-createFunctionGetAnAnswer.js
test/assets/migrations/20130909174253-renameFunctionGetAnAnswerGetTheAnswer.js
test/assets/migrations/20130909175000-deleteFunctionGetTheAnswer.js
test/assets/migrations/20130909175939-createTestTableForTrigger.js
test/assets/migrations/20130909180846-createTriggerOnTriggerTestTable.js
test/assets/migrations/20130909181148-renameTriggerUpdatedAtToUpdateUpdatedAt.js
test/assets/migrations/20130909185621-deleteTriggerUpdateUpdatedAt.js
test/migrator.test.js
lib/dialects/abstract/query-generator.js
View file @
04b5fe8
...
@@ -303,6 +303,49 @@ module.exports = (function() {
...
@@ -303,6 +303,49 @@ module.exports = (function() {
},
},
/*
/*
Create a trigger
*/
createTrigger
:
function
(
tableName
,
triggerName
,
timingType
,
fireOnArray
,
functionName
,
functionParams
,
optionsArray
)
{
throwMethodUndefined
(
'createTrigger'
)
},
/*
Drop a trigger
*/
dropTrigger
:
function
(
tableName
,
triggerName
)
{
throwMethodUndefined
(
'dropTrigger'
)
},
/*
Rename a trigger
*/
renameTrigger
:
function
(
tableName
,
oldTriggerName
,
newTriggerName
)
{
throwMethodUndefined
(
'renameTrigger'
)
},
/*
Create a function
*/
createFunction
:
function
(
functionName
,
params
,
returnType
,
language
,
body
,
options
)
{
throwMethodUndefined
(
'createFunction'
)
},
/*
Drop a function
*/
dropFunction
:
function
(
functionName
,
params
)
{
throwMethodUndefined
(
'dropFunction'
)
},
/*
Rename a function
*/
renameFunction
:
function
(
oldFunctionName
,
params
,
newFunctionName
)
{
throwMethodUndefined
(
'renameFunction'
)
},
/*
Escape an identifier (e.g. a table or attribute name)
Escape an identifier (e.g. a table or attribute name)
*/
*/
quoteIdentifier
:
function
(
identifier
,
force
)
{
quoteIdentifier
:
function
(
identifier
,
force
)
{
...
...
lib/dialects/postgres/query-generator.js
View file @
04b5fe8
...
@@ -722,6 +722,81 @@ module.exports = (function() {
...
@@ -722,6 +722,81 @@ module.exports = (function() {
return
false
// not supported by dialect
return
false
// not supported by dialect
},
},
createTrigger
:
function
(
tableName
,
triggerName
,
eventType
,
fireOnSpec
,
functionName
,
functionParams
,
optionsArray
)
{
var
sql
=
[
'CREATE <%= constraintVal %>TRIGGER <%= triggerName %>'
,
'<%= eventType %> <%= eventSpec %>'
,
'ON <%= tableName %>'
,
'<%= optionsSpec %>'
,
'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
)
})
},
dropTrigger
:
function
(
tableName
,
triggerName
)
{
var
sql
=
'DROP TRIGGER <%= triggerName %> ON <%= tableName %> RESTRICT;'
return
Utils
.
_
.
template
(
sql
)({
triggerName
:
triggerName
,
tableName
:
tableName
})
},
renameTrigger
:
function
(
tableName
,
oldTriggerName
,
newTriggerName
)
{
var
sql
=
'ALTER TRIGGER <%= oldTriggerName %> ON <%= tableName %> RENAME TO <%= newTriggerName%>;'
return
Utils
.
_
.
template
(
sql
)({
tableName
:
tableName
,
oldTriggerName
:
oldTriggerName
,
newTriggerName
:
newTriggerName
})
},
createFunction
:
function
(
functionName
,
params
,
returnType
,
language
,
body
,
options
)
{
var
sql
=
[
"CREATE FUNCTION <%= functionName %>(<%= paramList %>)"
,
"RETURNS <%= returnType %> AS $$"
,
"BEGIN"
,
"\t<%= body %>"
,
"END;"
,
"$$ language '<%= language %>'<%= options %>;"
].
join
(
'\n'
)
return
Utils
.
_
.
template
(
sql
)({
functionName
:
functionName
,
paramList
:
this
.
expandFunctionParamList
(
params
),
returnType
:
returnType
,
body
:
body
.
replace
(
'\n'
,
'\n\t'
),
language
:
language
,
options
:
this
.
expandOptions
(
options
)
})
},
dropFunction
:
function
(
functionName
,
params
)
{
// RESTRICT is (currently, as of 9.2) default but we'll be explicit
var
sql
=
'DROP FUNCTION <%= functionName %>(<%= paramList %>) RESTRICT;'
return
Utils
.
_
.
template
(
sql
)({
functionName
:
functionName
,
paramList
:
this
.
expandFunctionParamList
(
params
)
})
},
renameFunction
:
function
(
oldFunctionName
,
params
,
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
)
{
databaseConnectionUri
:
function
(
config
)
{
var
template
=
'<%= protocol %>://<%= user %>:<%= password %>@<%= host %><% if(port) { %>:<%= port %><% } %>/<%= database %>'
var
template
=
'<%= protocol %>://<%= user %>:<%= password %>@<%= host %><% if(port) { %>:<%= port %><% } %>/<%= database %>'
...
@@ -739,6 +814,77 @@ module.exports = (function() {
...
@@ -739,6 +814,77 @@ module.exports = (function() {
return
this
.
quoteIdentifier
(
Utils
.
removeTicks
(
this
.
escape
(
val
),
"'"
))
return
this
.
quoteIdentifier
(
Utils
.
removeTicks
(
this
.
escape
(
val
),
"'"
))
},
},
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"
)
}
var
paramList
=
Utils
.
_
.
each
(
params
,
function
expandParam
(
curParam
){
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
)
}
else
{
throw
new
Error
(
'createFunction called with a parameter with no type'
)
}
return
paramDef
.
join
(
' '
)
})
return
paramList
.
join
(
', '
)
},
expandOptions
:
function
expandOptions
(
options
)
{
return
Utils
.
_
.
isUndefined
(
options
)
||
Utils
.
_
.
isEmpty
(
options
)
?
''
:
'\n\t'
+
options
.
join
(
'\n\t'
)
},
decodeTriggerEventType
:
function
decodeTriggerEventType
(
eventSpecifier
)
{
var
EVENT_DECODER
=
{
'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
)
}
return
EVENT_DECODER
[
eventSpecifier
]
},
triggerEventTypeIsConstraint
:
function
triggerEventTypeIsConstraint
(
eventSpecifier
)
{
return
eventSpecifier
===
'after_constrain'
?
'CONSTRAINT '
:
''
},
expandTriggerEventSpec
:
function
expandTriggerEventSpec
(
fireOnSpec
)
{
if
(
Utils
.
_
.
isEmpty
(
fireOnSpec
))
{
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'
}
if
(
!
Utils
.
_
.
has
(
EVENT_MAP
,
fireKey
))
{
throw
new
Error
(
'parseTriggerEventSpec: undefined trigger event '
+
fireKey
)
}
var
eventSpec
=
EVENT_MAP
[
fireKey
]
if
(
eventSpec
===
'UPDATE'
)
{
if
(
Utils
.
_
.
isArray
(
fireValue
)
&&
fireValue
.
length
>
0
)
{
eventSpec
+=
' OF '
+
fireValue
.
join
(
', '
)
}
}
return
eventSpec
}).
join
(
' OR '
)
},
pgListEnums
:
function
(
tableName
,
attrName
,
options
)
{
pgListEnums
:
function
(
tableName
,
attrName
,
options
)
{
if
(
arguments
.
length
===
1
)
{
if
(
arguments
.
length
===
1
)
{
options
=
tableName
options
=
tableName
...
...
lib/query-interface.js
View file @
04b5fe8
...
@@ -628,6 +628,80 @@ module.exports = (function() {
...
@@ -628,6 +628,80 @@ 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
)
if
(
sql
){
return
queryAndEmit
.
call
(
this
,
sql
,
'createTrigger'
)
}
else
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
this
.
emit
(
'createTrigger'
,
null
)
emitter
.
emit
(
'success'
)
}).
run
()
}
}
QueryInterface
.
prototype
.
dropTrigger
=
function
(
tableName
,
triggerName
)
{
var
sql
=
this
.
QueryGenerator
.
dropTrigger
(
tableName
,
triggerName
)
if
(
sql
){
return
queryAndEmit
.
call
(
this
,
sql
,
'dropTrigger'
)
}
else
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
this
.
emit
(
'dropTrigger'
,
null
)
emitter
.
emit
(
'success'
)
}).
run
()
}
}
QueryInterface
.
prototype
.
renameTrigger
=
function
(
tableName
,
oldTriggerName
,
newTriggerName
)
{
var
sql
=
this
.
QueryGenerator
.
renameTrigger
(
tableName
,
oldTriggerName
,
newTriggerName
)
if
(
sql
){
return
queryAndEmit
.
call
(
this
,
sql
,
'renameTrigger'
)
}
else
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
this
.
emit
(
'renameTrigger'
,
null
)
emitter
.
emit
(
'success'
)
}).
run
()
}
}
QueryInterface
.
prototype
.
createFunction
=
function
(
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
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
this
.
emit
(
'createFunction'
,
null
)
emitter
.
emit
(
'success'
)
}).
run
()
}
}
QueryInterface
.
prototype
.
dropFunction
=
function
(
functionName
,
params
)
{
var
sql
=
this
.
QueryGenerator
.
dropFunction
(
functionName
,
params
)
if
(
sql
){
return
queryAndEmit
.
call
(
this
,
sql
,
'dropFunction'
)
}
else
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
this
.
emit
(
'dropFunction'
,
null
)
emitter
.
emit
(
'success'
)
}).
run
()
}
}
QueryInterface
.
prototype
.
renameFunction
=
function
(
oldFunctionName
,
params
,
newFunctionName
)
{
var
sql
=
this
.
QueryGenerator
.
renameFunction
(
oldFunctionName
,
params
,
newFunctionName
)
if
(
sql
){
return
queryAndEmit
.
call
(
this
,
sql
,
'renameFunction'
)
}
else
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
this
.
emit
(
'renameFunction'
,
null
)
emitter
.
emit
(
'success'
)
}).
run
()
}
}
// Helper methods useful for querying
// Helper methods useful for querying
/**
/**
...
...
test/assets/migrations/20130909174103-createFunctionGetAnAnswer.js
0 → 100644
View file @
04b5fe8
module
.
exports
=
{
up
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
createFunction
(
'get_an_answer'
,
[],
'int'
,
'plpgsql'
,
'RETURN 42;'
).
complete
(
done
);
},
down
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
dropFunction
(
'get_an_answer'
,
[]).
complete
(
done
);
}
}
test/assets/migrations/20130909174253-renameFunctionGetAnAnswerGetTheAnswer.js
0 → 100644
View file @
04b5fe8
module
.
exports
=
{
up
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
renameFunction
(
'get_an_answer'
,
[],
'get_the_answer'
).
complete
(
done
);
},
down
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
renameFunction
(
'get_the_answer'
,
[],
'get_an_answer'
).
complete
(
done
);
}
}
test/assets/migrations/20130909175000-deleteFunctionGetTheAnswer.js
0 → 100644
View file @
04b5fe8
module
.
exports
=
{
up
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
dropFunction
(
'get_the_answer'
,
[]).
complete
(
done
);
},
down
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
createFunction
(
'get_the_answer'
,
'int'
,
'plpgsql'
,
'RETURN 42;'
).
complete
(
done
);
}
}
test/assets/migrations/20130909175939-createTestTableForTrigger.js
0 → 100644
View file @
04b5fe8
module
.
exports
=
{
up
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
createTable
(
'trigger_test'
,
{
name
:
DataTypes
.
STRING
,
updated_at
:
{
type
:
DataTypes
.
DATE
,
defaultValue
:
DataTypes
.
NOW
,
allowNull
:
false
}
})
.
complete
(
done
)
},
down
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
dropTable
(
'trigger_test'
).
complete
(
done
)
}
}
test/assets/migrations/20130909180846-createTriggerOnTriggerTestTable.js
0 → 100644
View file @
04b5fe8
module
.
exports
=
{
up
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
createTrigger
(
'trigger_test'
,
'updated_at'
,
'before'
,
{
update
:
true
},
'bump_updated_at'
,
[]).
complete
(
done
);
},
down
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
dropTrigger
(
'trigger_test'
,
'updated_at'
).
complete
(
done
);
}
}
test/assets/migrations/20130909181148-renameTriggerUpdatedAtToUpdateUpdatedAt.js
0 → 100644
View file @
04b5fe8
module
.
exports
=
{
up
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
renameTrigger
(
'trigger_test'
,
'updated_at'
,
'update_updated_at'
).
complete
(
done
);
},
down
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
renameTrigger
(
'trigger_test'
,
'update_updated_at'
,
'updated_at'
).
complete
(
done
);
}
}
test/assets/migrations/20130909185621-deleteTriggerUpdateUpdatedAt.js
0 → 100644
View file @
04b5fe8
module
.
exports
=
{
up
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
dropTrigger
(
'trigger_test'
,
'update_updated_at'
).
complete
(
done
);
},
down
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
createTrigger
(
'trigger_test'
,
'update_updated_at'
,
'before'
,
{
update
:
true
},
'bump_updated_at'
,
[]).
complete
(
done
);
}
}
test/migrator.test.js
View file @
04b5fe8
...
@@ -14,7 +14,7 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
...
@@ -14,7 +14,7 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
logging
:
function
(){}
logging
:
function
(){}
},
options
||
{})
},
options
||
{})
//
this.sequelize.options.logging = console.log
//this.sequelize.options.logging = console.log
var
migrator
=
new
Migrator
(
this
.
sequelize
,
options
)
var
migrator
=
new
Migrator
(
this
.
sequelize
,
options
)
migrator
migrator
...
@@ -28,7 +28,7 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
...
@@ -28,7 +28,7 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
describe
(
'getUndoneMigrations'
,
function
()
{
describe
(
'getUndoneMigrations'
,
function
()
{
it
(
"returns no files if timestamps are after the files timestamp"
,
function
(
done
)
{
it
(
"returns no files if timestamps are after the files timestamp"
,
function
(
done
)
{
this
.
init
({
from
:
201
2
0101010101
},
function
(
migrator
)
{
this
.
init
({
from
:
201
4
0101010101
},
function
(
migrator
)
{
migrator
.
getUndoneMigrations
(
function
(
err
,
migrations
)
{
migrator
.
getUndoneMigrations
(
function
(
err
,
migrations
)
{
expect
(
err
).
to
.
be
.
null
expect
(
err
).
to
.
be
.
null
expect
(
migrations
.
length
).
to
.
equal
(
0
)
expect
(
migrations
.
length
).
to
.
equal
(
0
)
...
@@ -86,7 +86,7 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
...
@@ -86,7 +86,7 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
SequelizeMeta
.
create
({
from
:
null
,
to
:
20111117063700
}).
success
(
function
()
{
SequelizeMeta
.
create
({
from
:
null
,
to
:
20111117063700
}).
success
(
function
()
{
migrator
.
getUndoneMigrations
(
function
(
err
,
migrations
)
{
migrator
.
getUndoneMigrations
(
function
(
err
,
migrations
)
{
expect
(
err
).
to
.
be
.
null
expect
(
err
).
to
.
be
.
null
expect
(
migrations
).
to
.
have
.
length
(
7
)
expect
(
migrations
).
to
.
have
.
length
(
14
)
expect
(
migrations
[
0
].
filename
).
to
.
equal
(
'20111130161100-emptyMigration.js'
)
expect
(
migrations
[
0
].
filename
).
to
.
equal
(
'20111130161100-emptyMigration.js'
)
done
()
done
()
})
})
...
@@ -286,7 +286,8 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
...
@@ -286,7 +286,8 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
})
})
})
})
})
})
})
})
describe
(
'renameColumn'
,
function
()
{
describe
(
'renameColumn'
,
function
()
{
it
(
"renames the signature column from user to sig"
,
function
(
done
)
{
it
(
"renames the signature column from user to sig"
,
function
(
done
)
{
...
@@ -307,5 +308,145 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
...
@@ -307,5 +308,145 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
})
})
})
})
})
})
})
if
(
dialect
.
match
(
/^postgres/
))
{
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
){
this
.
init
({
from
:
20130909174103
,
to
:
20130909174103
},
function
(
migrator
)
{
migrator
.
migrate
().
success
(
function
(){
done
()
})
})
})
it
(
"creates a function "
+
FUNC_NAME
+
"()"
,
function
(
done
)
{
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
this
.
init
({
from
:
20130909174253
,
to
:
20130909174253
},
function
(
migrator
)
{
migrator
.
migrate
().
success
(
function
(){
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
()
})
})
})
})
})
it
(
"deletes a function "
+
RENAME_FUNC_NAME
+
"()"
,
function
(
done
)
{
var
self
=
this
this
.
init
({
from
:
20130909175000
,
to
:
20130909175000
},
function
(
migrator
)
{
migrator
.
migrate
().
success
(
function
(){
self
.
sequelize
.
query
(
generateFunctionCountQuery
(
RENAME_FUNC_NAME
,
'plpgsql'
)).
success
(
function
(
rows
){
expect
(
rows
.
length
).
to
.
equal
(
0
)
done
()
})
})
})
})
})
describe
(
'test trigger migrations'
,
function
()
{
var
generateTriggerCountQuery
=
function
generateTriggerCountQuery
(
triggerName
)
{
return
'SELECT * FROM pg_trigger where tgname = \''
+
triggerName
+
'\''
}
var
generateTableCountQuery
=
function
generateTableCountQuery
(
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'
// Make sure the function is present
before
(
function
(
done
){
this
.
sequelize
.
query
(
"CREATE FUNCTION bump_updated_at()\n"
+
"RETURNS TRIGGER AS $$\n"
+
"BEGIN\n"
+
"NEW.updated_at = now();\n"
+
"RETURN NEW;\n"
+
"END;\n"
+
"$$ language 'plpgsql';"
).
success
(
function
()
{
done
()})
})
// Clean up the function
after
(
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
this
.
init
({
from
:
20130909175939
,
to
:
20130909180846
},
function
(
migrator
)
{
migrator
.
migrate
().
success
(
function
(){
self
.
sequelize
.
query
(
generateTableCountQuery
(
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
()
})
})
})
})
})
it
(
"renames a trigger on "
+
TABLE_NAME
+
" from "
+
TRIGGER_NAME
+
" to "
+
RENAME_TRIGGER_NAME
,
function
(
done
){
var
self
=
this
this
.
init
({
from
:
20130909175939
,
to
:
20130909181148
},
function
(
migrator
)
{
migrator
.
migrate
().
success
(
function
(){
self
.
sequelize
.
query
(
generateTableCountQuery
(
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
)
self
.
sequelize
.
query
(
generateTriggerCountQuery
(
TRIGGER_NAME
)).
success
(
function
(
rows
){
expect
(
rows
.
length
).
to
.
equal
(
0
)
done
()
})
})
})
})
})
})
it
(
"deletes a trigger "
+
TRIGGER_NAME
+
" on trigger_test"
,
function
(
done
)
{
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
)
migrator
.
migrate
({
method
:
'down'
}).
success
(
function
(){
self
.
sequelize
.
query
(
generateTableCountQuery
(
TABLE_NAME
,
CATALOG_NAME
)).
success
(
function
(
rows
){
expect
(
rows
.
length
).
to
.
equal
(
0
)
done
()
})
})
})
})
})
})
})
}
// if dialect postgres
})
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