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

Commit c15eacdd by Sascha Depold

Merge branch 'make_binary_verbose' of git://github.com/terraflubb/sequelize into…

… terraflubb-make_binary_verbose
2 parents 48eba5f5 61482c0b
Showing with 52 additions and 19 deletions
...@@ -8,6 +8,7 @@ const path = require("path") ...@@ -8,6 +8,7 @@ const path = require("path")
, _ = Sequelize.Utils._ , _ = Sequelize.Utils._
var configPath = process.cwd() + '/config' var configPath = process.cwd() + '/config'
, environment = process.env.NODE_ENV || 'development'
, migrationsPath = process.cwd() + '/migrations' , migrationsPath = process.cwd() + '/migrations'
, packageJsonPath = __dirname + '/../package.json' , packageJsonPath = __dirname + '/../package.json'
, packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString()) , packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString())
...@@ -50,34 +51,52 @@ var createMigrationsFolder = function(force) { ...@@ -50,34 +51,52 @@ var createMigrationsFolder = function(force) {
} }
var readConfig = function() { var readConfig = function() {
var config
try { try {
var config = JSON.parse(fs.readFileSync(configFile)) config = fs.readFileSync(configFile)
, env = process.env.NODE_ENV || 'development' } catch(e) {
throw new Error('Error reading "config/config.json".')
}
if (config[env]) { try {
config = config[env] config = JSON.parse(config)
} } catch (e) {
throw new Error('Error parsing "config/config.json" as JSON.')
}
return config if (config[environment]) {
} catch(e) { config = config[environment]
throw new Error('The config.json is not available or contains invalid JSON.')
} }
return config
} }
program program
.version(packageJson.version) .version(packageJson.version)
.option('-i, --init', 'Initializes the project. Creates a config/config.json') .option('-i, --init', 'Initializes the project.')
.option('-m, --migrate', 'Runs undone migrations') .option('-e, --env <environment>', 'Specify the environment.')
.option('-m, --migrate', 'Run pending migrations.')
.option('-u, --undo', 'Undo the last migration.') .option('-u, --undo', 'Undo the last migration.')
.option('-f, --force', 'Forces the action to be done.') .option('-f, --force', 'Forces the action to be done.')
.option('-c, --create-migration [migration-name]', 'Create a new migration skeleton file.') .option('-c, --create-migration [migration-name]', 'Creates a new migration.')
.parse(process.argv) .parse(process.argv)
if(typeof program.env === 'string') {
environment = program.env
}
console.log("Using environment '" + environment + "'.")
if(program.migrate) { if(program.migrate) {
if(configFileExists) { if(configFileExists) {
var config = readConfig() var config
, options = {} , options = {}
try {
config = readConfig()
} catch(e) {
console.log(e.message)
process.exit(1)
}
_.each(config, function(value, key) { _.each(config, function(value, key) {
if(['database', 'username', 'password'].indexOf(key) == -1) { if(['database', 'username', 'password'].indexOf(key) == -1) {
options[key] = value options[key] = value
...@@ -104,7 +123,8 @@ if(program.migrate) { ...@@ -104,7 +123,8 @@ if(program.migrate) {
sequelize.migrate() sequelize.migrate()
} }
} else { } else {
throw new Error('Please add a configuration file under config/config.json. You might run "sequelize --init".') console.log('Cannot find "config/config.json". Have you run "sequelize --init"?')
process.exit(1)
} }
} else if(program.init) { } else if(program.init) {
if(!configFileExists || !!program.force) { if(!configFileExists || !!program.force) {
...@@ -129,9 +149,10 @@ if(program.migrate) { ...@@ -129,9 +149,10 @@ if(program.migrate) {
} }
}) })
console.log('Successfully created config.json') console.log('Created "config/config.json"')
} else { } else {
console.log('A config.json already exists. Run "sequelize --init --force" to overwrite it.') console.log('"config/config.json" already exists. Run "sequelize --init --force" to overwrite.')
process.exit(1)
} }
createMigrationsFolder(program.force) createMigrationsFolder(program.force)
...@@ -140,21 +161,23 @@ if(program.migrate) { ...@@ -140,21 +161,23 @@ if(program.migrate) {
var migrationName = [ var migrationName = [
moment().format('YYYYMMDDHHmmss'), moment().format('YYYYMMDDHHmmss'),
(typeof program.createMigration == 'string') ? program.createMigration : 'unnamed-migration' (typeof program.createMigration === 'string') ? program.createMigration : 'unnamed-migration'
].join('-') + '.js' ].join('-') + '.js'
var migrationContent = [ var migrationContent = [
"module.exports = {", "module.exports = {",
" up: function(migration, DataTypes, done) {", " up: function(migration, DataTypes, done) {",
" // add altering commands here, calling 'done' when finished", " // add altering commands here, calling 'done' when finished",
" done()",
" },", " },",
" down: function(migration, DataTypes, done) {", " down: function(migration, DataTypes, done) {",
" // add reverting commands here, calling 'done' when finished", " // add reverting commands here, calling 'done' when finished",
" done()",
" }", " }",
"}" "}"
].join('\n') ].join('\n')
fs.writeFileSync(migrationsPath + '/' + migrationName, migrationContent) fs.writeFileSync(migrationsPath + '/' + migrationName, migrationContent)
} else { } else {
console.log('Please define any params!') console.log('Try "sequelize --help" for usage information.')
} }
...@@ -53,16 +53,26 @@ module.exports = (function() { ...@@ -53,16 +53,26 @@ module.exports = (function() {
migrations.reverse() migrations.reverse()
} }
if (migrations.length === 0) {
self.options.logging("There are no pending migrations.")
} else {
self.options.logging("Running migrations...")
}
migrations.forEach(function(migration) { migrations.forEach(function(migration) {
var migrationTime
chainer.add(migration, 'execute', [options], { chainer.add(migration, 'execute', [options], {
before: function(migration) { before: function(migration) {
if (self.options.logging !== false) { if (self.options.logging !== false) {
self.options.logging('Executing migration: ' + migration.filename) self.options.logging(migration.filename)
} }
migrationTime = process.hrtime()
}, },
after: function(migration) { after: function(migration) {
migrationTime = process.hrtime(migrationTime)
migrationTime = Math.round( (migrationTime[0] * 1000) + (migrationTime[1] / 1000000));
if (self.options.logging !== false) { if (self.options.logging !== false) {
self.options.logging('Executed migration: ' + migration.filename) self.options.logging('Completed in ' + migrationTime + 'ms')
} }
}, },
success: function(migration, callback) { success: function(migration, callback) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!