不要怂,就是干,撸起袖子干!

Commit 8dae0e11 by Sascha Depold

Merge branch 'master' into milestones/2.0.0

2 parents 29ce9ba2 e4f26921
......@@ -7,11 +7,12 @@ var path = require("path")
, moment = require("moment")
, _ = Sequelize.Utils._
, url = require("url")
, supportCS = undefined
var configuration = {
configFile: process.cwd() + '/config/config.json',
environment: process.env.NODE_ENV || 'development',
version: require(__dirname + '/../package.json').version,
configFile: process.cwd() + '/config/config.json',
environment: process.env.NODE_ENV || 'development',
version: require(__dirname + '/../package.json').version,
migrationsPath: process.cwd() + '/migrations'
}
......@@ -115,6 +116,7 @@ var parseDbUrl = function(urlString) {
var readConfig = function() {
var config
if (program.url) {
config = parseDbUrl(program.url);
} else {
......@@ -125,7 +127,7 @@ var readConfig = function() {
}
}
if(typeof config != 'object') {
if (typeof config != 'object') {
throw new Error('Config must be an object: ' + relativeConfigFile());
}
......@@ -139,6 +141,7 @@ var readConfig = function() {
console.log('Using environment "' + configuration.environment + '".')
config = config[configuration.environment]
}
return config
}
......@@ -152,6 +155,7 @@ program
.option('-c, --create-migration [migration-name]', 'Creates a new migration.')
.option('-U, --url <url>', 'Database url. An alternative to a config file')
.option('--config <config_file>', 'Specifies alternate config file.')
.option('--coffee', 'Consider coffee script files as migration source.')
.parse(process.argv)
if(typeof program.config === 'string') {
......@@ -166,6 +170,21 @@ if(typeof program.env === 'string') {
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 (configFileExists() || program.url) {
var config
......@@ -205,7 +224,12 @@ if (program.migrate || program.undo) {
var sequelize = new Sequelize(config.database, config.username, config.password, options)
, migratorOptions = { path: configuration.migrationsPath }
, migrator = sequelize.getMigrator(migratorOptions)
if (supportCoffeeScript()) {
migratorOptions = _.merge(migratorOptions, { filesFilter: /\.js$|\.coffee$/ })
}
var migrator = sequelize.getMigrator(migratorOptions)
if (program.undo) {
migrator.findOrCreateSequelizeMetaDAO().success(function(Meta) {
......@@ -244,23 +268,39 @@ if (program.migrate || program.undo) {
} else if(program.createMigration) {
createMigrationsFolder()
var migrationName = [
moment().format('YYYYMMDDHHmmss'),
(typeof program.createMigration === 'string') ? program.createMigration : 'unnamed-migration'
].join('-') + '.js'
var 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"
var mirationContent = ""
, migrationExtension = (supportCoffeeScript()) ? '.coffee' : '.js'
, migrationName = [
moment().format('YYYYMMDDHHmmss'),
(typeof program.createMigration === 'string') ? program.createMigration : 'unnamed-migration'
].join('-') + migrationExtension
if (supportCoffeeScript()) {
migrationContent = [
"module.exports = ",
" up: (migration, DataTypes, done) ->",
" # add altering commands here, calling 'done' when finished",
" done()",
"",
" down: (migration, DataTypes, done) ->",
" # add reverting commands here, calling 'done' when finished",
" 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)
console.log('New migration "' + migrationName + '" was added to "' +
......
......@@ -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() 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)
- [FEATURE] Support coffee script files in migrations. [#1357](https://github.com/sequelize/sequelize/pull/1357)
# v1.7.0-rc4
- 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
- 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`
......
......@@ -50,6 +50,15 @@ module.exports = (function() {
Object.defineProperty(Migration.prototype, 'migration', {
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)
}
})
......
......@@ -69,7 +69,8 @@
"lcov-result-merger": "0.0.2",
"istanbul": "~0.1.45",
"coveralls": "~2.5.0",
"async": "~0.2.9"
"async": "~0.2.9",
"coffee-script": "~1.7.1"
},
"keywords": [
"mysql",
......
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
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)
}
}
......@@ -30,7 +30,7 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
describe('getUndoneMigrations', function() {
it("supports coffee files", function(done) {
this.init({
filesFilter: /\.cs$/,
filesFilter: /\.coffee$/,
to: 20111130161100
}, function(migrator) {
migrator.getUndoneMigrations(function(err, migrations) {
......@@ -120,6 +120,24 @@ describe(Support.getTestDialectTeaser("Migrator"), 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) {
this.sequelize.getQueryInterface().showAllTables().success(function(tableNames) {
tableNames = tableNames.filter(function(e){ return e != 'SequelizeMeta' })
......
......@@ -142,12 +142,63 @@ describe(Support.getTestDialectTeaser("Executable"), function() {
})
})(['--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) {
flags.forEach(function(flag) {
var prepare = function(callback) {
exec("rm -rf ./*", { 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) {
var dialect = Support.getTestDialect()
, config = require(__dirname + '/config/config.js')
......@@ -208,9 +259,11 @@ describe(Support.getTestDialectTeaser("Executable"), function() {
})
})([
'--migrate',
'--migrate --coffee',
'--migrate --config ../tmp/config/config.json',
'--migrate --config ' + path.join(__dirname, 'tmp', 'config', 'config.json'),
'-m',
'-m --coffee',
'-m --config ../tmp/config/config.json',
'-m --config ' + path.join(__dirname, 'tmp', 'config', 'config.json')
])
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!