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 60a43179
authored
Nov 29, 2011
by
sdepold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
successfully observing queryInterface for making async handling in migrations unnecessary
1 parent
8704a6e1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
76 additions
and
43 deletions
lib/migration.js
lib/migrator.js
lib/migration.js
View file @
60a4317
var
moment
=
require
(
"moment"
)
,
Utils
=
require
(
"./utils"
)
var
moment
=
require
(
"moment"
)
,
Utils
=
require
(
"./utils"
)
,
DataTypes
=
require
(
"./data-types"
)
,
QueryInterface
=
require
(
"./query-interface"
)
module
.
exports
=
(
function
()
{
var
Migration
=
function
(
path
)
{
var
Migration
=
function
(
migrator
,
path
)
{
var
split
=
path
.
split
(
'/'
)
this
.
path
=
path
this
.
filename
=
Utils
.
_
.
last
(
this
.
path
.
split
(
'/'
))
this
.
date
=
Migration
.
stringToDate
(
split
[
split
.
length
-
1
])
this
.
migrator
=
migrator
this
.
path
=
path
this
.
filename
=
Utils
.
_
.
last
(
this
.
path
.
split
(
'/'
))
this
.
date
=
Migration
.
stringToDate
(
split
[
split
.
length
-
1
])
this
.
queryInterface
=
this
.
migrator
.
queryInterface
this
.
undoneMethods
=
0
}
///////////////
...
...
@@ -34,8 +39,21 @@ module.exports = (function() {
// member /////
///////////////
Migration
.
prototype
.
execute
=
function
()
{
Migration
.
prototype
.
execute
=
function
(
options
)
{
var
self
=
this
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
options
=
Utils
.
_
.
extend
({
method
:
'up'
},
options
||
{})
extendMigrationWithQueryInterfaceMethods
.
call
(
self
,
function
allMethodsDone
()
{
emitter
.
emit
(
'success'
,
null
)
})
var
methods
=
require
(
self
.
path
)
methods
[
options
.
method
].
call
(
null
,
self
,
DataTypes
)
}).
run
()
}
Migration
.
prototype
.
isBefore
=
function
(
dateString
,
options
)
{
...
...
@@ -58,5 +76,35 @@ module.exports = (function() {
return
options
.
withoutEqual
?
(
date
<
this
.
date
)
:
(
date
<=
this
.
date
)
}
// extends the Migration prototype with all methods of QueryInterface.prototype
// with additional tracking of start and finish. this is done in order to minimize
// asynchronous handling in migrations
var
extendMigrationWithQueryInterfaceMethods
=
function
(
callback
)
{
var
self
=
this
for
(
var
method
in
QueryInterface
.
prototype
)
{
(
function
(
_method
)
{
self
[
_method
]
=
function
()
{
var
emitter
=
self
.
QueryInterface
,
args
=
Utils
.
_
.
map
(
arguments
,
function
(
arg
,
_
)
{
return
arg
})
self
.
undoneMethods
++
// bind listeners to the query interface
// the event will have the same name like the method
self
.
queryInterface
.
on
(
_method
,
function
(
err
)
{
self
.
undoneMethods
--
if
(
err
)
throw
new
Error
(
err
)
else
(
self
.
undoneMethods
==
0
)
&&
callback
&&
callback
()
})
self
.
queryInterface
[
_method
].
apply
(
self
.
queryInterface
,
args
)
}
})(
method
)
}
}
return
Migration
})()
lib/migrator.js
View file @
60a4317
...
...
@@ -3,14 +3,12 @@ const fs = require("fs")
,
moment
=
require
(
"moment"
)
var
Utils
=
require
(
"./utils"
)
,
DataTypes
=
require
(
"./data-types"
)
,
QueryInterface
=
require
(
"./query-interface"
)
,
Migration
=
require
(
"./migration"
)
,
DataTypes
=
require
(
"./data-types"
)
module
.
exports
=
(
function
()
{
var
Migrator
=
function
(
sequelize
,
options
)
{
this
.
sequelize
=
sequelize
this
.
queryInterface
=
sequelize
.
getQueryInterface
()
this
.
options
=
Utils
.
_
.
extend
({
path
:
__dirname
+
'/../migrations'
,
from
:
null
,
...
...
@@ -18,13 +16,30 @@ module.exports = (function() {
},
options
||
{})
}
Object
.
defineProperty
(
Migrator
.
prototype
,
"queryInterface"
,
{
get
:
function
()
{
return
this
.
sequelize
.
getQueryInterface
()
}
})
Migrator
.
prototype
.
migrate
=
function
()
{
var
self
=
this
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
self
.
getUndoneMigrations
(
function
(
migrations
)
{
console
.
log
(
migrations
)
emitter
.
emit
(
'success'
)
var
chainer
=
new
Utils
.
QueryChainer
migrations
.
forEach
(
function
(
migration
)
{
chainer
.
add
(
migration
.
execute
())
})
chainer
.
run
()
.
success
(
function
()
{
emitter
.
emit
(
'success'
,
null
)
}).
error
(
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
})
}).
run
()
}
...
...
@@ -44,7 +59,7 @@ module.exports = (function() {
var
migrationFiles
=
fs
.
readdirSync
(
this
.
options
.
path
)
var
migrations
=
migrationFiles
.
map
(
function
(
file
)
{
return
new
Migration
(
self
.
options
.
path
+
'/'
+
file
)
return
new
Migration
(
self
,
self
.
options
.
path
+
'/'
+
file
)
})
if
(
this
.
options
.
from
)
{
...
...
@@ -91,12 +106,6 @@ module.exports = (function() {
}).
run
()
}
Migrator
.
prototype
.
executeMigration
=
function
(
path
,
method
)
{
console
.
log
(
path
)
var
migration
=
require
(
path
)
migration
[
method
||
'up'
](
this
.
queryInterface
,
DataTypes
)
}
// private
var
getLastMigrationIdFromDatabase
=
function
()
{
...
...
@@ -127,29 +136,5 @@ module.exports = (function() {
return
moment
(
getFormattedDateString
(
s
),
"YYYYMMDDHHmmss"
)
}
var
observeQueryInterfaceForSuccess
=
function
(
callback
)
{
var
self
=
this
for
(
var
method
in
QueryInterface
.
prototype
)
{
self
[
method
]
=
function
()
{
var
emitter
=
self
.
queryInterface
// bind listeners to the query interface
// the event will have the same name like the method
self
.
queryInterface
.
on
(
method
,
function
(
err
)
{
if
(
err
)
{
throw
new
Error
(
err
)
}
else
{
}
})
}
}
console
.
log
(
proto
)
//this.queryInterface.prototype.each ...
}
return
Migrator
})()
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