Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for environments with dotenv #88

Merged
merged 1 commit into from
Nov 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ support
test
examples
*.sock
.db
.migrate
12 changes: 12 additions & 0 deletions bin/migrate-create
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

var program = require('commander')
var path = require('path')
var dotenv = require('dotenv')
var log = require('../lib/log')
var registerCompiler = require('../lib/register-compiler')
var pkg = require('../package.json')
Expand All @@ -17,10 +18,21 @@ program
.option('-t, --template-file [filePath]', 'Set path to template file to use for new migrations', path.join(__dirname, '..', 'lib', 'template.js'))
.option('-e, --extention [extention]', 'Use the given extention to create the file', '.js')
.option('-g, --generator <name>', 'A template generator function', path.join(__dirname, '..', 'lib', 'template-generator'))
.option('--env [name]', 'Use dotenv to load an environment file')
.arguments('<name>')
.action(create)
.parse(process.argv)

// Setup environment
if (program.env) {
var e = dotenv.config({
path: typeof program.env === 'string' ? program.env : '.env'
})
if (e && e.error instanceof Error) {
throw e.error
}
}

var _name
function create (name) {
// Name provided?
Expand Down
12 changes: 12 additions & 0 deletions bin/migrate-down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
var program = require('commander')
var path = require('path')
var minimatch = require('minimatch')
var dotenv = require('dotenv')
var migrate = require('../')
var runMigrations = require('../lib/migrate')
var log = require('../lib/log')
Expand All @@ -20,11 +21,22 @@ program
.option('--migrations-dir <dir>', 'Change the migrations directory name', 'migrations')
.option('--matches <glob>', 'A glob pattern to filter migration files', '*')
.option('--compiler <ext:module>', 'Use the given module to compile files')
.option('--env [name]', 'Use dotenv to load an environment file')
.parse(process.argv)

// Change the working dir
process.chdir(program.chdir)

// Setup environment
if (program.env) {
var e = dotenv.config({
path: typeof program.env === 'string' ? program.env : '.env'
})
if (e && e.error instanceof Error) {
throw e.error
}
}

// Load compiler
if (program.compiler) {
registerCompiler(program.compiler)
Expand Down
12 changes: 12 additions & 0 deletions bin/migrate-init
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

var program = require('commander')
var mkdirp = require('mkdirp')
var dotenv = require('dotenv')
var path = require('path')
var log = require('../lib/log')
var pkg = require('../package.json')
Expand All @@ -14,11 +15,22 @@ program
.option('-s, --store <store>', 'Set the migrations store', path.join(__dirname, '..', 'lib', 'file-store'))
.option('--migrations-dir <dir>', 'Change the migrations directory name', 'migrations')
.option('-c, --chdir [dir]', 'Change the working directory', process.cwd())
.option('--env [name]', 'Use dotenv to load an environment file')
.parse(process.argv)

// Change the working dir
process.chdir(program.chdir)

// Setup environment
if (program.env) {
var e = dotenv.config({
path: typeof program.env === 'string' ? program.env : '.env'
})
if (e && e.error instanceof Error) {
throw e.error
}
}

// Setup store
var Store = require(program.store)
var store = new Store(program.stateFile)
Expand Down
12 changes: 12 additions & 0 deletions bin/migrate-list
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var program = require('commander')
var path = require('path')
var dateFormat = require('dateformat')
var minimatch = require('minimatch')
var dotenv = require('dotenv')
var migrate = require('../')
var log = require('../lib/log')
var registerCompiler = require('../lib/register-compiler')
Expand All @@ -21,6 +22,7 @@ program
.option('--migrations-dir <dir>', 'Change the migrations directory name', 'migrations')
.option('--matches <glob>', 'A glob pattern to filter migration files', '*')
.option('--compiler <ext:module>', 'Use the given module to compile files')
.option('--env [name]', 'Use dotenv to load an environment file')
.parse(process.argv)

// Check clean flag, exit if NODE_ENV === 'production' and force not specified
Expand All @@ -32,6 +34,16 @@ if (program.clean && process.env.NODE_ENV === 'production' && !program.force) {
// Change the working dir
process.chdir(program.chdir)

// Setup environment
if (program.env) {
var e = dotenv.config({
path: typeof program.env === 'string' ? program.env : '.env'
})
if (e && e.error instanceof Error) {
throw e.error
}
}

// Load compiler
if (program.compiler) {
registerCompiler(program.compiler)
Expand Down
18 changes: 15 additions & 3 deletions bin/migrate-up
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
var program = require('commander')
var path = require('path')
var minimatch = require('minimatch')
var dotenv = require('dotenv')
var migrate = require('../')
var runMigrations = require('../lib/migrate')
var log = require('../lib/log')
Expand All @@ -23,8 +24,22 @@ program
.option('--migrations-dir <dir>', 'Change the migrations directory name', 'migrations')
.option('--matches <glob>', 'A glob pattern to filter migration files', '*')
.option('--compiler <ext:module>', 'Use the given module to compile files')
.option('--env [name]', 'Use dotenv to load an environment file')
.parse(process.argv)

// Change the working dir
process.chdir(program.chdir)

// Setup environment
if (program.env) {
var e = dotenv.config({
path: typeof program.env === 'string' ? program.env : '.env'
})
if (e && e.error instanceof Error) {
throw e.error
}
}

// Check clean flag, exit if NODE_ENV === 'production' and force not specified
if (program.clean && process.env.NODE_ENV === 'production' && !program.force) {
log.error('error', 'Cowardly refusing to clean while node environment set to production, use --force to continue.')
Expand All @@ -37,9 +52,6 @@ if (program.init && process.env.NODE_ENV === 'production' && !program.force) {
process.exit(1)
}

// Change the working dir
process.chdir(program.chdir)

// Load compiler
if (program.compiler) {
registerCompiler(program.compiler)
Expand Down
1 change: 1 addition & 0 deletions examples/env/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DB=contributors
1 change: 1 addition & 0 deletions examples/env/.foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DB=foo
11 changes: 11 additions & 0 deletions examples/env/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Environment Example

```
$ migrate up --env
$ migrate down --env
$ cat .db # should see table of `contributors`

$ migrate up --env .foo
$ migrate down --env .foo
$ cat .db # should see table of `foo`
```
44 changes: 44 additions & 0 deletions examples/env/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict'
var fs = require('fs')

module.exports = {
loaded: false,
tables: {},
table: function (name) {
this.tables[name] = []
this.save()
},
removeTable: function (name) {
delete this.tables[name]
this.save()
},
insert: function (table, value) {
this.tables[table].push(value)
this.save()
},
remove: function (table, value) {
this.tables[table].splice(this.tables[table].indexOf(value), 1)
this.save()
},
save: function () {
fs.writeFileSync('.db', JSON.stringify(this))
},
load: function () {
if (this.loaded) return this
var json
try {
json = JSON.parse(fs.readFileSync('.db', 'utf8'))
} catch (e) {
// ignore
return this
}
this.loaded = true
this.tables = json.tables
return this
},
toJSON: function () {
return {
tables: this.tables
}
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"chalk": "^1.1.3",
"commander": "^2.9.0",
"dateformat": "^2.0.0",
"dotenv": "^4.0.0",
"inherits": "^2.0.3",
"minimatch": "^3.0.3",
"mkdirp": "^0.5.1",
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/env/env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DB=pets
21 changes: 20 additions & 1 deletion test/integration.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
/* global describe, it, beforeEach */
/* global describe, it, beforeEach, afterEach */
const path = require('path')
const assert = require('assert')
const rimraf = require('rimraf')
const mkdirp = require('mkdirp')
const run = require('./util/run')
const db = require('./util/db')

// Paths
const TMP_DIR = path.join(__dirname, 'fixtures', 'tmp')
const ENV_DIR = path.join(__dirname, 'fixtures', 'env')

function reset () {
rimraf.sync(path.join(ENV_DIR, '.migrate'))
rimraf.sync(TMP_DIR)
mkdirp.sync(TMP_DIR)
db.nuke()
}

describe('integration tests', function () {
beforeEach(reset)
afterEach(reset)

it('should warn when the migrations are run out of order', function (done) {
run.init(TMP_DIR, [], function (err, out, code) {
Expand Down Expand Up @@ -94,4 +99,18 @@ describe('integration tests', function () {
})
})
})

it('should load the enviroment file when passed --env', function (done) {
run.up(ENV_DIR, ['--env', 'env'], function (err, out, code) {
assert(!err)
assert.equal(code, 0)
assert(out.indexOf('error') === -1)
run.down(ENV_DIR, ['--env', 'env'], function (err, out, code) {
assert(!err)
assert.equal(code, 0)
assert(out.indexOf('error') === -1)
done()
})
})
})
})