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 09a9f780
authored
Nov 17, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
further steps for migrations
1 parent
a8497312
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
81 additions
and
10 deletions
lib/migrator.js
package.json
spec/migrator.spec.js
lib/migrator.js
View file @
09a9f78
var
fs
=
require
(
"fs"
)
var
fs
=
require
(
"fs"
)
,
path
=
require
(
"path"
)
,
path
=
require
(
"path"
)
,
Utils
=
require
(
"./utils"
)
,
moment
=
require
(
"moment"
)
,
Utils
=
require
(
"./utils"
)
,
DataTypes
=
require
(
"./data-types"
)
module
.
exports
=
(
function
()
{
module
.
exports
=
(
function
()
{
var
Migrator
=
function
(
options
)
{
var
Migrator
=
function
(
sequelize
,
options
)
{
this
.
options
=
Utils
.
_
.
extend
({
this
.
sequelize
=
sequelize
this
.
options
=
Utils
.
_
.
extend
({
path
:
__dirname
+
'/../migrations'
,
path
:
__dirname
+
'/../migrations'
,
from
:
null
,
from
:
null
,
to
:
null
to
:
null
...
@@ -12,17 +15,80 @@ module.exports = (function() {
...
@@ -12,17 +15,80 @@ module.exports = (function() {
}
}
Migrator
.
prototype
.
migrate
=
function
()
{
Migrator
.
prototype
.
migrate
=
function
()
{
getLastMigrationId
.
call
(
this
)
var
self
=
this
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
getLastMigrationId
.
call
(
self
).
success
(
function
(
migrationId
)
{
migrationId
=
migrationId
||
'19700101000000'
console
.
log
(
migrationId
)
var
nonMigratedFiles
=
getMigrationsFilesSince
.
call
(
self
,
migrationId
)
console
.
log
(
nonMigratedFiles
)
emitter
.
emit
(
'success'
)
})
}).
run
()
}
}
// private
// private
var
getLastMigrationId
=
function
()
{
var
getLastMigrationId
=
function
()
{
var
self
=
this
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
var
SequelizeMeta
=
self
.
sequelize
.
modelManager
.
getModel
(
'SequelizeMeta'
)
var
findLastMigrationId
=
function
()
{
SequelizeMeta
.
find
({
order
:
'id DESC'
}).
success
(
function
(
sequelizeMeta
)
{
emitter
.
emit
(
'success'
,
sequelizeMeta
?
sequelizeMeta
.
lastMigrationId
:
null
)
})
}
if
(
SequelizeMeta
)
{
findLastMigrationId
()
}
else
{
SequelizeMeta
=
self
.
sequelize
.
define
(
'SequelizeMeta'
,
{
lastMigrationId
:
DataTypes
.
STRING
})
SequelizeMeta
.
sync
().
success
(
function
()
{
findLastMigrationId
()
}).
error
(
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
}
}).
run
()
}
var
getFormattedDateString
=
function
(
s
)
{
var
result
=
null
var
result
=
null
fs
.
readdirSync
(
this
.
options
.
path
).
filter
(
function
(
file
)
{
try
{
console
.
log
(
file
)
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
}
var
stringToDate
=
function
(
s
)
{
var
formattedString
=
getFormattedDateString
(
s
)
,
date
=
moment
(
formattedString
,
"YYYYMMDDHHmmss"
)
return
date
}
var
getMigrationsFilesSince
=
function
(
sinceString
)
{
var
result
=
[]
,
since
=
stringToDate
(
sinceString
)
var
undoneMigrationFiles
=
fs
.
readdirSync
(
this
.
options
.
path
).
filter
(
function
(
file
)
{
var
fileDateString
=
file
.
split
(
"-"
)[
0
]
,
fileDate
=
stringToDate
(
fileDateString
)
return
fileDate
.
diff
(
since
)
>
0
})
})
return
undoneMigrationFiles
}
}
return
Migrator
return
Migrator
...
...
package.json
View file @
09a9f78
...
@@ -11,7 +11,8 @@
...
@@ -11,7 +11,8 @@
"underscore"
:
"=1.2.1"
,
"underscore"
:
"=1.2.1"
,
"underscore.string"
:
"=1.2.0"
,
"underscore.string"
:
"=1.2.0"
,
"lingo"
:
"=0.0.4"
,
"lingo"
:
"=0.0.4"
,
"validator"
:
"=0.3.5"
"validator"
:
"=0.3.5"
,
"moment"
:
"=1.1.1"
},
},
"devDependencies"
:
{
"devDependencies"
:
{
"jasmine-node"
:
"=1.0.12"
,
"jasmine-node"
:
"=1.0.12"
,
...
...
spec/migrator.spec.js
View file @
09a9f78
...
@@ -7,7 +7,11 @@ var config = require("./config/config")
...
@@ -7,7 +7,11 @@ var config = require("./config/config")
describe
(
'Migrator'
,
function
()
{
describe
(
'Migrator'
,
function
()
{
describe
(
'getLastMigrationId'
,
function
()
{
describe
(
'getLastMigrationId'
,
function
()
{
it
(
"should correctly transform array into IN"
,
function
()
{
it
(
"should correctly transform array into IN"
,
function
()
{
new
Migrator
({
path
:
__dirname
+
'/assets/migrations'
}).
migrate
()
Helpers
.
async
(
function
(
done
)
{
new
Migrator
(
sequelize
,
{
path
:
__dirname
+
'/assets/migrations'
}).
migrate
().
success
(
function
()
{
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