-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathmigrate-down
executable file
·78 lines (67 loc) · 2.25 KB
/
migrate-down
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env node
// vim: set ft=javascript:
'use strict'
const program = require('commander')
const path = require('path')
const minimatch = require('minimatch')
const dotenv = require('dotenv')
const migrate = require('../')
const runMigrations = require('../lib/migrate')
const log = require('../lib/log')
const registerCompiler = require('../lib/register-compiler')
const pkg = require('../package.json')
program
.version(pkg.version)
.usage('[options] <name>')
.option('-c, --chdir <dir>', 'Change the working directory', process.cwd())
.option('-f, --state-file <path>', 'Set path to state file', '.migrate')
.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('--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')
.option('-F, --force', 'Force through the command, ignoring warnings')
.parse(process.argv)
// Change the working dir
process.chdir(program.chdir)
// Setup environment
if (program.env) {
const 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)
}
// Setup store
if (program.store[0] === '.') program.store = path.join(process.cwd(), program.store)
const StoreImport = require(program.store)
const Store = StoreImport.default || StoreImport
const store = new Store(program.stateFile)
// Load in migrations
migrate.load({
stateStore: store,
migrationsDirectory: program.migrationsDir,
filterFunction: minimatch.filter(program.matches),
ignoreMissing: program.force
}, function (err, set) {
if (err) {
log.error('error', err)
process.exit(1)
}
set.on('migration', function (migration, direction) {
log(direction, migration.title)
})
runMigrations(set, 'down', program.args[0], function (err) {
if (err) {
log.error('error', err)
process.exit(1)
}
log('migration', 'complete')
process.exit(0)
})
})