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 e0679969
authored
Nov 28, 2011
by
sdepold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
extracted migration specific logic to separate class
1 parent
04c9410e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
135 additions
and
51 deletions
lib/migration.js
lib/migrator.js
spec/migrator.spec.js
lib/migration.js
0 → 100644
View file @
e067996
var
moment
=
require
(
"moment"
)
,
Utils
=
require
(
"./utils"
)
module
.
exports
=
(
function
()
{
var
Migration
=
function
(
path
)
{
var
split
=
path
.
split
(
'/'
)
this
.
path
=
path
this
.
filename
=
Utils
.
_
.
last
(
this
.
path
.
split
(
'/'
))
this
.
date
=
Migration
.
stringToDate
(
split
[
split
.
length
-
1
])
}
///////////////
// static /////
///////////////
Migration
.
getFormattedDateString
=
function
(
s
)
{
var
result
=
null
try
{
result
=
s
.
match
(
/
(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})
/
).
slice
(
1
,
6
).
join
(
'-'
)
}
catch
(
e
)
{
throw
new
Error
(
s
+
' is no valid migration timestamp format! Use YYYYMMDDHHmmss!'
)
}
return
result
}
Migration
.
stringToDate
=
function
(
s
)
{
return
moment
(
Migration
.
getFormattedDateString
(
s
),
"YYYYMMDDHHmmss"
)
}
///////////////
// member /////
///////////////
Migration
.
prototype
.
execute
=
function
()
{
}
Migration
.
prototype
.
isBefore
=
function
(
dateString
,
options
)
{
options
=
Utils
.
_
.
extend
({
withoutEquals
:
false
},
options
||
{})
var
date
=
Migration
.
stringToDate
(
dateString
.
toString
())
return
options
.
withoutEqual
?
(
date
>
this
.
date
)
:
(
date
>=
this
.
date
)
}
Migration
.
prototype
.
isAfter
=
function
(
dateString
,
options
)
{
options
=
Utils
.
_
.
extend
({
withoutEquals
:
false
},
options
||
{})
var
date
=
Migration
.
stringToDate
(
dateString
.
toString
())
return
options
.
withoutEqual
?
(
date
<
this
.
date
)
:
(
date
<=
this
.
date
)
}
return
Migration
})()
lib/migrator.js
View file @
e067996
...
...
@@ -10,7 +10,7 @@ var Utils = require("./utils")
module
.
exports
=
(
function
()
{
var
Migrator
=
function
(
sequelize
,
options
)
{
this
.
sequelize
=
sequelize
this
.
queryInterface
=
new
QueryInterface
(
this
.
sequelize
)
this
.
queryInterface
=
sequelize
.
getQueryInterface
(
)
this
.
options
=
Utils
.
_
.
extend
({
path
:
__dirname
+
'/../migrations'
,
from
:
null
,
...
...
@@ -22,8 +22,8 @@ module.exports = (function() {
var
self
=
this
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
self
.
getUndoneMigrations
(
function
(
migration
File
s
)
{
migrationFiles
.
forEach
(
function
()
{
self
.
executeMigration
()
}
)
self
.
getUndoneMigrations
(
function
(
migrations
)
{
console
.
log
(
migrations
)
emitter
.
emit
(
'success'
)
})
}).
run
()
...
...
@@ -32,50 +32,39 @@ module.exports = (function() {
Migrator
.
prototype
.
getUndoneMigrations
=
function
(
callback
)
{
var
self
=
this
var
filterFrom
=
function
(
files
,
from
,
callback
,
options
)
{
var
fromDate
=
stringToDate
(
from
.
toString
())
,
result
=
[]
result
=
files
.
filter
(
function
(
file
)
{
var
fileDate
=
stringToDate
(
file
.
split
(
"-"
)[
0
])
return
(
options
||
{}).
withoutEqual
?
(
fileDate
>
fromDate
)
:
(
fileDate
>=
fromDate
)
})
var
filterFrom
=
function
(
migrations
,
from
,
callback
,
options
)
{
var
result
=
migrations
.
filter
(
function
(
migration
)
{
return
migration
.
isAfter
(
from
,
options
)
})
callback
&&
callback
(
result
)
}
var
filterTo
=
function
(
files
,
to
,
callback
)
{
var
toDate
=
stringToDate
(
to
.
toString
())
,
result
=
[]
result
=
files
.
filter
(
function
(
file
)
{
var
fileDate
=
stringToDate
(
file
.
split
(
"-"
)[
0
])
return
(
fileDate
<=
toDate
)
})
var
filterTo
=
function
(
migrations
,
to
,
callback
,
options
)
{
var
result
=
migrations
.
filter
(
function
(
migration
)
{
return
migration
.
isBefore
(
to
,
options
)
})
callback
&&
callback
(
result
)
}
var
migrationFiles
=
fs
.
readdirSync
(
this
.
options
.
path
)
var
migrations
=
migrationFiles
.
map
(
function
(
file
)
{
return
new
Migration
(
self
.
options
.
path
+
'/'
+
file
)
})
if
(
this
.
options
.
from
)
{
filterFrom
(
migration
Files
,
this
.
options
.
from
,
function
(
file
s
)
{
filterFrom
(
migration
s
,
this
.
options
.
from
,
function
(
migration
s
)
{
if
(
self
.
options
.
to
)
filterTo
(
file
s
,
self
.
options
.
to
,
callback
)
filterTo
(
migration
s
,
self
.
options
.
to
,
callback
)
else
callback
&&
callback
(
file
s
)
callback
&&
callback
(
migration
s
)
})
}
else
{
getLastMigrationIdFromDatabase
.
call
(
this
).
success
(
function
(
lastMigrationId
)
{
if
(
lastMigrationId
)
{
filterFrom
(
migration
Files
,
lastMigrationId
,
function
(
file
s
)
{
filterFrom
(
migration
s
,
lastMigrationId
,
function
(
migration
s
)
{
if
(
self
.
options
.
to
)
filterTo
(
file
s
,
self
.
options
.
to
,
callback
)
filterTo
(
migration
s
,
self
.
options
.
to
,
callback
)
else
callback
&&
callback
(
file
s
)
callback
&&
callback
(
migration
s
)
},
{
withoutEqual
:
true
})
}
else
{
callback
&&
callback
(
migration
File
s
)
callback
&&
callback
(
migrations
)
}
})
}
...
...
@@ -103,6 +92,7 @@ module.exports = (function() {
}
Migrator
.
prototype
.
executeMigration
=
function
(
path
,
method
)
{
console
.
log
(
path
)
var
migration
=
require
(
path
)
migration
[
method
||
'up'
](
this
.
queryInterface
,
DataTypes
)
}
...
...
@@ -137,5 +127,29 @@ 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
})()
spec/migrator.spec.js
View file @
e067996
...
...
@@ -3,6 +3,7 @@ var config = require("./config/config")
,
sequelize
=
new
Sequelize
(
config
.
database
,
config
.
username
,
config
.
password
,
{
logging
:
false
})
,
Helpers
=
new
(
require
(
"./config/helpers"
))(
sequelize
)
,
Migrator
=
require
(
"../lib/migrator"
)
,
_
=
Sequelize
.
Utils
.
_
describe
(
'Migrator'
,
function
()
{
describe
(
'getUndoneMigrations'
,
function
()
{
...
...
@@ -31,8 +32,8 @@ describe('Migrator', function() {
setup
({
from
:
20120101010101
})
Helpers
.
async
(
function
(
done
)
{
migrator
.
getUndoneMigrations
(
function
(
file
s
)
{
expect
(
file
s
.
length
).
toEqual
(
0
)
migrator
.
getUndoneMigrations
(
function
(
migration
s
)
{
expect
(
migration
s
.
length
).
toEqual
(
0
)
done
()
})
})
...
...
@@ -42,9 +43,9 @@ describe('Migrator', function() {
setup
({
from
:
19700101000000
,
to
:
20111117063700
})
Helpers
.
async
(
function
(
done
)
{
migrator
.
getUndoneMigrations
(
function
(
file
s
)
{
expect
(
file
s
.
length
).
toEqual
(
1
)
expect
(
files
[
0
]
).
toEqual
(
'20111117063700-createPerson.js'
)
migrator
.
getUndoneMigrations
(
function
(
migration
s
)
{
expect
(
migration
s
.
length
).
toEqual
(
1
)
expect
(
_
.
last
(
migrations
).
filename
).
toEqual
(
'20111117063700-createPerson.js'
)
done
()
})
})
...
...
@@ -54,10 +55,10 @@ describe('Migrator', function() {
setup
({
from
:
20111117063700
,
to
:
20111123060700
})
Helpers
.
async
(
function
(
done
)
{
migrator
.
getUndoneMigrations
(
function
(
file
s
)
{
expect
(
file
s
.
length
).
toEqual
(
2
)
expect
(
files
[
0
]
).
toEqual
(
'20111117063700-createPerson.js'
)
expect
(
files
[
1
]
).
toEqual
(
'20111123060700-addBirthdateToPerson.js'
)
migrator
.
getUndoneMigrations
(
function
(
migration
s
)
{
expect
(
migration
s
.
length
).
toEqual
(
2
)
expect
(
migrations
[
0
].
filename
).
toEqual
(
'20111117063700-createPerson.js'
)
expect
(
migrations
[
1
].
filename
).
toEqual
(
'20111123060700-addBirthdateToPerson.js'
)
done
()
})
})
...
...
@@ -67,8 +68,8 @@ describe('Migrator', function() {
setup
({
to
:
20111123060700
})
Helpers
.
async
(
function
(
done
)
{
migrator
.
getUndoneMigrations
(
function
(
file
s
)
{
expect
(
file
s
.
length
).
toEqual
(
2
)
migrator
.
getUndoneMigrations
(
function
(
migration
s
)
{
expect
(
migration
s
.
length
).
toEqual
(
2
)
done
()
})
})
...
...
@@ -79,9 +80,9 @@ describe('Migrator', function() {
Helpers
.
async
(
function
(
done
)
{
SequelizeMeta
.
create
({
lastMigrationId
:
'20111117063700'
}).
success
(
function
()
{
migrator
.
getUndoneMigrations
(
function
(
file
s
)
{
expect
(
file
s
.
length
).
toEqual
(
1
)
expect
(
files
[
0
]
).
toEqual
(
'20111123060700-addBirthdateToPerson.js'
)
migrator
.
getUndoneMigrations
(
function
(
migration
s
)
{
expect
(
migration
s
.
length
).
toEqual
(
1
)
expect
(
migrations
[
0
].
filename
).
toEqual
(
'20111123060700-addBirthdateToPerson.js'
)
done
()
})
})
...
...
@@ -99,16 +100,23 @@ describe('Migrator', function() {
})
})
it
(
"should execute the migrations"
,
function
()
{
/*Helpers.async(function(done) {
new Migrator(sequelize, {
path: __dirname + '/assets/migrations',
from: 2011111706370020111117063700,
to: 20111117063700
}).migrate().success(function() {
xit
(
"should execute the specified migration (and e.g. create a file)"
,
function
()
{
var
migrator
=
new
Migrator
(
sequelize
,
{
path
:
__dirname
+
'/assets/migrations'
,
from
:
20111117063700
,
to
:
20111117063700
})
Helpers
.
async
(
function
(
done
)
{
migrator
.
migrate
().
success
(
done
).
error
(
function
(
err
)
{
console
.
log
(
err
)
})
})
Helpers
.
async
(
function
(
done
)
{
sequelize
.
getQueryInterface
().
showAllTables
(
function
(
tableName
)
{
expect
(
tableNames
.
length
).
toEqual
(
1
)
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