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 8dae0e11
authored
Feb 07, 2014
by
Sascha Depold
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' into milestones/2.0.0
2 parents
29ce9ba2
e4f26921
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
162 additions
and
43 deletions
bin/sequelize
changelog.md
lib/migration.js
package.json
test/assets/migrations/20111117063700-createPerson.coffee
test/assets/migrations/20111117063700-createPerson.cs
test/migrator.test.js
test/sequelize.executable.test.js
bin/sequelize
View file @
8dae0e1
...
@@ -7,11 +7,12 @@ var path = require("path")
...
@@ -7,11 +7,12 @@ var path = require("path")
,
moment
=
require
(
"moment"
)
,
moment
=
require
(
"moment"
)
,
_
=
Sequelize
.
Utils
.
_
,
_
=
Sequelize
.
Utils
.
_
,
url
=
require
(
"url"
)
,
url
=
require
(
"url"
)
,
supportCS
=
undefined
var
configuration
=
{
var
configuration
=
{
configFile
:
process
.
cwd
()
+
'/config/config.json'
,
configFile
:
process
.
cwd
()
+
'/config/config.json'
,
environment
:
process
.
env
.
NODE_ENV
||
'development'
,
environment
:
process
.
env
.
NODE_ENV
||
'development'
,
version
:
require
(
__dirname
+
'/../package.json'
).
version
,
version
:
require
(
__dirname
+
'/../package.json'
).
version
,
migrationsPath
:
process
.
cwd
()
+
'/migrations'
migrationsPath
:
process
.
cwd
()
+
'/migrations'
}
}
...
@@ -115,6 +116,7 @@ var parseDbUrl = function(urlString) {
...
@@ -115,6 +116,7 @@ var parseDbUrl = function(urlString) {
var
readConfig
=
function
()
{
var
readConfig
=
function
()
{
var
config
var
config
if
(
program
.
url
)
{
if
(
program
.
url
)
{
config
=
parseDbUrl
(
program
.
url
);
config
=
parseDbUrl
(
program
.
url
);
}
else
{
}
else
{
...
@@ -125,7 +127,7 @@ var readConfig = function() {
...
@@ -125,7 +127,7 @@ var readConfig = function() {
}
}
}
}
if
(
typeof
config
!=
'object'
)
{
if
(
typeof
config
!=
'object'
)
{
throw
new
Error
(
'Config must be an object: '
+
relativeConfigFile
());
throw
new
Error
(
'Config must be an object: '
+
relativeConfigFile
());
}
}
...
@@ -139,6 +141,7 @@ var readConfig = function() {
...
@@ -139,6 +141,7 @@ var readConfig = function() {
console
.
log
(
'Using environment "'
+
configuration
.
environment
+
'".'
)
console
.
log
(
'Using environment "'
+
configuration
.
environment
+
'".'
)
config
=
config
[
configuration
.
environment
]
config
=
config
[
configuration
.
environment
]
}
}
return
config
return
config
}
}
...
@@ -152,6 +155,7 @@ program
...
@@ -152,6 +155,7 @@ program
.
option
(
'-c, --create-migration [migration-name]'
,
'Creates a new migration.'
)
.
option
(
'-c, --create-migration [migration-name]'
,
'Creates a new migration.'
)
.
option
(
'-U, --url <url>'
,
'Database url. An alternative to a config file'
)
.
option
(
'-U, --url <url>'
,
'Database url. An alternative to a config file'
)
.
option
(
'--config <config_file>'
,
'Specifies alternate config file.'
)
.
option
(
'--config <config_file>'
,
'Specifies alternate config file.'
)
.
option
(
'--coffee'
,
'Consider coffee script files as migration source.'
)
.
parse
(
process
.
argv
)
.
parse
(
process
.
argv
)
if
(
typeof
program
.
config
===
'string'
)
{
if
(
typeof
program
.
config
===
'string'
)
{
...
@@ -166,6 +170,21 @@ if(typeof program.env === 'string') {
...
@@ -166,6 +170,21 @@ if(typeof program.env === 'string') {
configuration
.
environment
=
program
.
env
configuration
.
environment
=
program
.
env
}
}
var
supportCoffeeScript
=
function
()
{
if
(
supportCS
===
undefined
)
{
try
{
config
=
readConfig
()
}
catch
(
e
)
{
console
.
log
(
e
.
message
)
process
.
exit
(
1
)
}
supportCS
=
program
.
coffee
||
config
.
coffee
}
return
supportCS
}
if
(
program
.
migrate
||
program
.
undo
)
{
if
(
program
.
migrate
||
program
.
undo
)
{
if
(
configFileExists
()
||
program
.
url
)
{
if
(
configFileExists
()
||
program
.
url
)
{
var
config
var
config
...
@@ -205,7 +224,12 @@ if (program.migrate || program.undo) {
...
@@ -205,7 +224,12 @@ if (program.migrate || program.undo) {
var
sequelize
=
new
Sequelize
(
config
.
database
,
config
.
username
,
config
.
password
,
options
)
var
sequelize
=
new
Sequelize
(
config
.
database
,
config
.
username
,
config
.
password
,
options
)
,
migratorOptions
=
{
path
:
configuration
.
migrationsPath
}
,
migratorOptions
=
{
path
:
configuration
.
migrationsPath
}
,
migrator
=
sequelize
.
getMigrator
(
migratorOptions
)
if
(
supportCoffeeScript
())
{
migratorOptions
=
_
.
merge
(
migratorOptions
,
{
filesFilter
:
/
\.
js$|
\.
coffee$/
})
}
var
migrator
=
sequelize
.
getMigrator
(
migratorOptions
)
if
(
program
.
undo
)
{
if
(
program
.
undo
)
{
migrator
.
findOrCreateSequelizeMetaDAO
().
success
(
function
(
Meta
)
{
migrator
.
findOrCreateSequelizeMetaDAO
().
success
(
function
(
Meta
)
{
...
@@ -244,23 +268,39 @@ if (program.migrate || program.undo) {
...
@@ -244,23 +268,39 @@ if (program.migrate || program.undo) {
}
else
if
(
program
.
createMigration
)
{
}
else
if
(
program
.
createMigration
)
{
createMigrationsFolder
()
createMigrationsFolder
()
var
migrationName
=
[
var
mirationContent
=
""
moment
().
format
(
'YYYYMMDDHHmmss'
),
,
migrationExtension
=
(
supportCoffeeScript
())
?
'.coffee'
:
'.js'
(
typeof
program
.
createMigration
===
'string'
)
?
program
.
createMigration
:
'unnamed-migration'
,
migrationName
=
[
].
join
(
'-'
)
+
'.js'
moment
().
format
(
'YYYYMMDDHHmmss'
),
(
typeof
program
.
createMigration
===
'string'
)
?
program
.
createMigration
:
'unnamed-migration'
var
migrationContent
=
[
].
join
(
'-'
)
+
migrationExtension
"module.exports = {"
,
" up: function(migration, DataTypes, done) {"
,
if
(
supportCoffeeScript
())
{
" // add altering commands here, calling 'done' when finished"
,
migrationContent
=
[
" done()"
,
"module.exports = "
,
" },"
,
" up: (migration, DataTypes, done) ->"
,
" down: function(migration, DataTypes, done) {"
,
" # add altering commands here, calling 'done' when finished"
,
" // add reverting commands here, calling 'done' when finished"
,
" done()"
,
" done()"
,
""
,
" }"
,
" down: (migration, DataTypes, done) ->"
,
"}"
" # add reverting commands here, calling 'done' when finished"
,
].
join
(
'\n'
)
+
"\n"
" done()"
].
join
(
'\n'
)
+
"\n"
}
else
{
migrationContent
=
[
"module.exports = {"
,
" up: function(migration, DataTypes, done) {"
,
" // add altering commands here, calling 'done' when finished"
,
" done()"
,
" },"
,
" down: function(migration, DataTypes, done) {"
,
" // add reverting commands here, calling 'done' when finished"
,
" done()"
,
" }"
,
"}"
].
join
(
'\n'
)
+
"\n"
}
fs
.
writeFileSync
(
configuration
.
migrationsPath
+
'/'
+
migrationName
,
migrationContent
)
fs
.
writeFileSync
(
configuration
.
migrationsPath
+
'/'
+
migrationName
,
migrationContent
)
console
.
log
(
'New migration "'
+
migrationName
+
'" was added to "'
+
console
.
log
(
'New migration "'
+
migrationName
+
'" was added to "'
+
...
...
changelog.md
View file @
8dae0e1
...
@@ -4,10 +4,11 @@ Notice: All 1.7.x changes are present in 2.0.x aswell
...
@@ -4,10 +4,11 @@ Notice: All 1.7.x changes are present in 2.0.x aswell
-
sync() now correctly returns with an error when foreign key constraints reference unknown tables
-
sync() now correctly returns with an error when foreign key constraints reference unknown tables
-
sync() no longer fails with foreign key constraints references own table (toposort self-dependency error)
-
sync() no longer fails with foreign key constraints references own table (toposort self-dependency error)
-
makes it possible to specify exactly what timestamp attributes you want to utilize
[
#1334
](
https://github.com/sequelize/sequelize/pull/1334
)
-
makes it possible to specify exactly what timestamp attributes you want to utilize
[
#1334
](
https://github.com/sequelize/sequelize/pull/1334
)
-
[
FEATURE
]
Support coffee script files in migrations.
[
#1357
](
https://github.com/sequelize/sequelize/pull/1357
)
# v1.7.0-rc4
# v1.7.0-rc4
-
fixes issue with postgres sync and enums
[
#1020
](
https://github.com/sequelize/sequelize/issues/1020
)
-
fixes issue with postgres sync and enums
[
#1020
](
https://github.com/sequelize/sequelize/issues/1020
)
-
fixes various issues with limit and includes
[
#1322
](
https://github.com/sequelize/sequelize/pull/1322
)
-
fixes various issues with limit and includes
[
#1322
](
https://github.com/sequelize/sequelize/pull/1322
)
-
fixes issues with migrations/queryInterface createTable and enums
-
fixes issues with migrations/queryInterface createTable and enums
-
migration/queryInterface.addIndex() no longer fals on reserved keywords like 'from'
-
migration/queryInterface.addIndex() no longer fals on reserved keywords like 'from'
-
bulkCreate now supports a
`ignoreDuplicates`
option for MySQL, SQLite and MariaDB that will use
`INSERT IGNORE`
-
bulkCreate now supports a
`ignoreDuplicates`
option for MySQL, SQLite and MariaDB that will use
`INSERT IGNORE`
...
...
lib/migration.js
View file @
8dae0e1
...
@@ -50,6 +50,15 @@ module.exports = (function() {
...
@@ -50,6 +50,15 @@ module.exports = (function() {
Object
.
defineProperty
(
Migration
.
prototype
,
'migration'
,
{
Object
.
defineProperty
(
Migration
.
prototype
,
'migration'
,
{
get
:
function
()
{
get
:
function
()
{
if
(
this
.
path
.
match
(
/
\.
coffee$/
))
{
try
{
require
(
'coffee-script/register'
)
}
catch
(
e
)
{
console
.
log
(
"You have to add \"coffee-script\" to your package.json."
)
process
.
exit
(
1
)
}
}
return
require
(
this
.
path
)
return
require
(
this
.
path
)
}
}
})
})
...
...
package.json
View file @
8dae0e1
...
@@ -69,7 +69,8 @@
...
@@ -69,7 +69,8 @@
"lcov-result-merger"
:
"0.0.2"
,
"lcov-result-merger"
:
"0.0.2"
,
"istanbul"
:
"~0.1.45"
,
"istanbul"
:
"~0.1.45"
,
"coveralls"
:
"~2.5.0"
,
"coveralls"
:
"~2.5.0"
,
"async"
:
"~0.2.9"
"async"
:
"~0.2.9"
,
"coffee-script"
:
"~1.7.1"
},
},
"keywords"
:
[
"keywords"
:
[
"mysql"
,
"mysql"
,
...
...
test/assets/migrations/20111117063700-createPerson.coffee
0 → 100644
View file @
8dae0e1
module
.
exports
=
up
:
(
migration
,
DataTypes
,
done
)
->
migration
.
createTable
'Person'
,
name
:
DataTypes
.
STRING
isBetaMember
:
type
:
DataTypes
.
BOOLEAN
defaultValue
:
false
allowNull
:
false
.
complete
done
down
:
(
migration
,
DataTypes
,
done
)
->
migration
.
dropTable
'Person'
.
complete
done
test/assets/migrations/20111117063700-createPerson.cs
deleted
100644 → 0
View file @
29ce9ba
module
.
exports
=
{
up
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
createTable
(
'
Person
'
,
{
name
:
DataTypes
.
STRING
,
isBetaMember
:
{
type
:
DataTypes
.
BOOLEAN
,
defaultValue
:
false
,
allowNull
:
false
}
})
.
complete
(
done
)
},
down
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
dropTable
(
'
Person
'
).
complete
(
done
)
}
}
test/migrator.test.js
View file @
8dae0e1
...
@@ -30,7 +30,7 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
...
@@ -30,7 +30,7 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
describe
(
'getUndoneMigrations'
,
function
()
{
describe
(
'getUndoneMigrations'
,
function
()
{
it
(
"supports coffee files"
,
function
(
done
)
{
it
(
"supports coffee files"
,
function
(
done
)
{
this
.
init
({
this
.
init
({
filesFilter
:
/
\.
c
s
$/
,
filesFilter
:
/
\.
c
offee
$/
,
to
:
20111130161100
to
:
20111130161100
},
function
(
migrator
)
{
},
function
(
migrator
)
{
migrator
.
getUndoneMigrations
(
function
(
err
,
migrations
)
{
migrator
.
getUndoneMigrations
(
function
(
err
,
migrations
)
{
...
@@ -120,6 +120,24 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
...
@@ -120,6 +120,24 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
})
})
describe
(
'executions'
,
function
()
{
describe
(
'executions'
,
function
()
{
it
(
"supports coffee files"
,
function
(
done
)
{
var
self
=
this
this
.
init
({
filesFilter
:
/
\.
coffee$/
,
to
:
20111130161100
},
function
(
migrator
)
{
self
.
migrator
=
migrator
self
.
migrator
.
migrate
().
success
(
function
()
{
self
.
sequelize
.
getQueryInterface
().
showAllTables
().
success
(
function
(
tableNames
)
{
tableNames
=
tableNames
.
filter
(
function
(
e
){
return
e
!=
'SequelizeMeta'
})
expect
(
tableNames
).
to
.
eql
([
'Person'
])
done
()
})
})
})
})
it
(
"executes migration #20111117063700 and correctly creates the table"
,
function
(
done
)
{
it
(
"executes migration #20111117063700 and correctly creates the table"
,
function
(
done
)
{
this
.
sequelize
.
getQueryInterface
().
showAllTables
().
success
(
function
(
tableNames
)
{
this
.
sequelize
.
getQueryInterface
().
showAllTables
().
success
(
function
(
tableNames
)
{
tableNames
=
tableNames
.
filter
(
function
(
e
){
return
e
!=
'SequelizeMeta'
})
tableNames
=
tableNames
.
filter
(
function
(
e
){
return
e
!=
'SequelizeMeta'
})
...
...
test/sequelize.executable.test.js
View file @
8dae0e1
...
@@ -142,12 +142,63 @@ describe(Support.getTestDialectTeaser("Executable"), function() {
...
@@ -142,12 +142,63 @@ describe(Support.getTestDialectTeaser("Executable"), function() {
})
})
})([
'--create-migration'
,
'-c'
])
})([
'--create-migration'
,
'-c'
])
;(
function
(
flags
)
{
flags
.
forEach
(
function
(
flag
)
{
var
prepare
=
function
(
callback
)
{
exec
(
"rm -rf ./*"
,
{
cwd
:
__dirname
+
'/tmp'
},
function
()
{
exec
(
"../../bin/sequelize --init"
,
{
cwd
:
__dirname
+
'/tmp'
},
function
()
{
exec
(
"../../bin/sequelize "
+
flag
+
" 'foo'"
,
{
cwd
:
__dirname
+
'/tmp'
},
callback
)
})
})
}
describe
(
flag
,
function
()
{
it
(
"creates a new file with the current timestamp"
,
function
(
done
)
{
prepare
(
function
()
{
exec
(
"ls -1 migrations"
,
{
cwd
:
__dirname
+
'/tmp'
},
function
(
err
,
stdout
)
{
var
date
=
new
Date
()
,
format
=
function
(
i
)
{
return
(
parseInt
(
i
,
10
)
<
10
?
'0'
+
i
:
i
)
}
,
sDate
=
[
date
.
getFullYear
(),
format
(
date
.
getMonth
()
+
1
),
format
(
date
.
getDate
()),
format
(
date
.
getHours
()),
format
(
date
.
getMinutes
())].
join
(
''
)
expect
(
stdout
).
to
.
match
(
new
RegExp
(
sDate
+
"..-foo.coffee"
))
done
()
})
})
})
it
(
"adds a skeleton with an up and a down method"
,
function
(
done
)
{
prepare
(
function
()
{
exec
(
"cat migrations/*-foo.coffee"
,
{
cwd
:
__dirname
+
'/tmp'
},
function
(
err
,
stdout
)
{
expect
(
stdout
).
to
.
include
(
'up: (migration, DataTypes, done) ->'
)
expect
(
stdout
).
to
.
include
(
'down: (migration, DataTypes, done) ->'
)
done
()
})
})
})
it
(
"calls the done callback"
,
function
(
done
)
{
prepare
(
function
()
{
exec
(
"cat migrations/*-foo.coffee"
,
{
cwd
:
__dirname
+
'/tmp'
},
function
(
err
,
stdout
)
{
expect
(
stdout
).
to
.
include
(
'done()'
)
expect
(
stdout
.
match
(
/
(
done
\(\))
/
)).
to
.
have
.
length
(
2
)
done
()
})
})
})
})
})
})([
'--coffee --create-migration'
,
'--coffee -c'
])
;(
function
(
flags
)
{
;(
function
(
flags
)
{
flags
.
forEach
(
function
(
flag
)
{
flags
.
forEach
(
function
(
flag
)
{
var
prepare
=
function
(
callback
)
{
var
prepare
=
function
(
callback
)
{
exec
(
"rm -rf ./*"
,
{
cwd
:
__dirname
+
'/tmp'
},
function
(
error
,
stdout
)
{
exec
(
"rm -rf ./*"
,
{
cwd
:
__dirname
+
'/tmp'
},
function
(
error
,
stdout
)
{
exec
(
"../../bin/sequelize --init"
,
{
cwd
:
__dirname
+
'/tmp'
},
function
(
error
,
stdout
)
{
exec
(
"../../bin/sequelize --init"
,
{
cwd
:
__dirname
+
'/tmp'
},
function
(
error
,
stdout
)
{
exec
(
"cp ../assets/migrations/*-createPerson.js ./migrations/"
,
{
cwd
:
__dirname
+
'/tmp'
},
function
(
error
,
stdout
)
{
var
source
=
(
flag
.
indexOf
(
'coffee'
)
===
-
1
)
?
"../assets/migrations/*-createPerson.js"
:
"../assets/migrations/*-createPerson.coffee"
exec
(
"cp "
+
source
+
" ./migrations/"
,
{
cwd
:
__dirname
+
'/tmp'
},
function
(
error
,
stdout
)
{
exec
(
"cat ../support.js|sed s,/../,/../../, > ./support.js"
,
{
cwd
:
__dirname
+
'/tmp'
},
function
(
error
,
stdout
)
{
exec
(
"cat ../support.js|sed s,/../,/../../, > ./support.js"
,
{
cwd
:
__dirname
+
'/tmp'
},
function
(
error
,
stdout
)
{
var
dialect
=
Support
.
getTestDialect
()
var
dialect
=
Support
.
getTestDialect
()
,
config
=
require
(
__dirname
+
'/config/config.js'
)
,
config
=
require
(
__dirname
+
'/config/config.js'
)
...
@@ -208,9 +259,11 @@ describe(Support.getTestDialectTeaser("Executable"), function() {
...
@@ -208,9 +259,11 @@ describe(Support.getTestDialectTeaser("Executable"), function() {
})
})
})([
})([
'--migrate'
,
'--migrate'
,
'--migrate --coffee'
,
'--migrate --config ../tmp/config/config.json'
,
'--migrate --config ../tmp/config/config.json'
,
'--migrate --config '
+
path
.
join
(
__dirname
,
'tmp'
,
'config'
,
'config.json'
),
'--migrate --config '
+
path
.
join
(
__dirname
,
'tmp'
,
'config'
,
'config.json'
),
'-m'
,
'-m'
,
'-m --coffee'
,
'-m --config ../tmp/config/config.json'
,
'-m --config ../tmp/config/config.json'
,
'-m --config '
+
path
.
join
(
__dirname
,
'tmp'
,
'config'
,
'config.json'
)
'-m --config '
+
path
.
join
(
__dirname
,
'tmp'
,
'config'
,
'config.json'
)
])
])
...
...
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