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 c7739be1
authored
Sep 11, 2013
by
Jonathan M. Altman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Unit tests for function and trigger migrations
1 parent
d8b1565d
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
221 additions
and
9 deletions
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/postgres/query-generator.js
View file @
c7739be
...
@@ -750,7 +750,7 @@ module.exports = (function() {
...
@@ -750,7 +750,7 @@ module.exports = (function() {
},
},
dropTrigger
:
function
(
tableName
,
triggerName
)
{
dropTrigger
:
function
(
tableName
,
triggerName
)
{
var
sql
=
'DROP TRIGGER
IF EXISTS
<%= triggerName %> ON <%= tableName %> RESTRICT'
var
sql
=
'DROP TRIGGER <%= triggerName %> ON <%= tableName %> RESTRICT'
return
Utils
.
_
.
template
(
sql
)({
return
Utils
.
_
.
template
(
sql
)({
triggerName
:
triggerName
triggerName
:
triggerName
,
tableName
:
tableName
,
tableName
:
tableName
...
@@ -787,7 +787,7 @@ module.exports = (function() {
...
@@ -787,7 +787,7 @@ module.exports = (function() {
dropFunction
:
function
(
functionName
,
params
)
{
dropFunction
:
function
(
functionName
,
params
)
{
// RESTRICT is (currently, as of 9.2) default but we'll be explicit
// RESTRICT is (currently, as of 9.2) default but we'll be explicit
var
sql
=
'DROP FUNCTION
IF EXISTS
<%= functionName %>(<%= paramList %>) RESTRICT'
;
var
sql
=
'DROP FUNCTION <%= functionName %>(<%= paramList %>) RESTRICT'
;
return
Utils
.
_
.
template
(
sql
)({
return
Utils
.
_
.
template
(
sql
)({
functionName
:
functionName
,
functionName
:
functionName
,
paramList
:
this
.
expandFunctionParamList
(
params
)
paramList
:
this
.
expandFunctionParamList
(
params
)
...
...
lib/query-interface.js
View file @
c7739be
...
@@ -507,7 +507,7 @@ module.exports = (function() {
...
@@ -507,7 +507,7 @@ module.exports = (function() {
return
queryAndEmit
.
call
(
this
,
sql
,
'renameTrigger'
)
return
queryAndEmit
.
call
(
this
,
sql
,
'renameTrigger'
)
}
else
{
}
else
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
this
.
emit
(
'
drop
Trigger'
,
null
)
this
.
emit
(
'
rename
Trigger'
,
null
)
emitter
.
emit
(
'success'
)
emitter
.
emit
(
'success'
)
}).
run
()
}).
run
()
}
}
...
@@ -548,7 +548,7 @@ module.exports = (function() {
...
@@ -548,7 +548,7 @@ module.exports = (function() {
}).
run
()
}).
run
()
}
}
}
}
// Helper methods useful for querying
// Helper methods useful for querying
/**
/**
...
...
test/assets/migrations/20130909174103-createFunctionGetAnAnswer.js
0 → 100644
View file @
c7739be
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 @
c7739be
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 @
c7739be
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 @
c7739be
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 @
c7739be
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 @
c7739be
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 @
c7739be
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 @
c7739be
...
@@ -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
(
15
)
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
(
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'
;
// 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
();
})
})
})
it
(
"creates a function "
+
FUNC_NAME
+
"()"
,
function
(
done
)
{
var
self
=
this
;
this
.
sequelize
.
query
(
generateFunctionCountQuery
(
FUNC_NAME
,
11771
)).
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
,
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
);
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
,
11771
)).
success
(
function
(
rows
){
expect
(
rows
.
length
).
to
.
equal
(
0
);
done
();
})
})
})
})
})
describe
(
'test trigger migrations'
,
function
(
done
)
{
var
generateTriggerCountQuery
=
function
generateTriggerCountQuery
(
triggerName
)
{
return
'SELECT * FROM pg_trigger where tgname = \''
+
triggerName
+
'\''
;
};
var
generateFunctionCountQuery
=
function
generateFunctionCountQuery
(
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
(
generateFunctionCountQuery
(
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
(
generateFunctionCountQuery
(
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
(
generateFunctionCountQuery
(
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