/
db-migrate
executable file
·155 lines (132 loc) · 3.56 KB
/
db-migrate
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env node
var optimist = require('optimist');
var fs = require('fs');
var config = require('../lib/config.js');
var index = require('../index');
var log = require('../lib/log');
var util = require('util');
var path = require('path');
var assert = require('assert');
process.on('uncaughtException', function(err) {
log.error(err);
process.exit(1);
});
var argv = optimist
.default({
verbose: false,
config: process.cwd() + '/database.json',
'migrations-dir': process.cwd() + '/migrations' })
.usage('Usage: db-migrate [up|down|create] migrationName [options]')
.describe('env', 'The environment to run the migrations under (dev, test, prod).')
.alias('e', 'env')
.string('e')
.describe('migrations-dir', 'The directory containing your migration files.')
.alias('m', 'migrations-dir')
.string('m')
.describe('count', 'Max number of migrations to run.')
.alias('c', 'count')
.string('c')
.describe('verbose', 'Verbose mode.')
.alias('v', 'verbose')
.boolean('v')
.alias('h', 'help')
.alias('h', '?')
.boolean('h')
.describe('config', 'Location of the database.json file.')
.string('config')
.argv;
if (argv.help || argv._.length == 0) {
optimist.showHelp();
process.exit(1);
}
global.verbose = argv.verbose;
function createMigrationDir(dir, callback) {
fs.stat(dir, function(err, stat) {
if (err) {
fs.mkdir(dir, callback);
} else {
callback();
}
});
}
function loadConfig() {
config.load(argv.config, argv.env);
if(verbose) {
log.info("Using config:", config.getCurrent());
}
}
function executeCreate() {
if(argv._.length == 0) {
log.error("'migrationName' is required.");
optimist.showHelp();
process.exit(1);
}
createMigrationDir(argv['migrations-dir'], function(err) {
if (err) {
log.error('Failed to create migration directory at ', argv['migrations-dir'], err);
process.exit(1);
}
argv.title = argv._.shift();
index.createMigration(argv.title, function(err, migration) {
assert.ifError(err);
log.info(util.format('Created migration at %s', migration.path));
});
})
}
function executeUp() {
if(!argv.count) {
argv.count = Number.MAX_VALUE;
}
index.connect(config.getCurrent(), function(err, migrator) {
assert.ifError(err);
migrator.migrationsDir = path.resolve(argv['migrations-dir']);
migrator.driver.createMigrationsTable(function(err) {
assert.ifError(err);
migrator.up(argv, onComplete.bind(this, migrator));
});
});
}
function executeDown() {
if(!argv.destination && !argv.count) {
argv.count = 1;
}
index.connect(config.getCurrent(), function(err, migrator) {
assert.ifError(err);
migrator.migrationsDir = path.resolve(argv['migrations-dir']);
migrator.driver.createMigrationsTable(function(err) {
assert.ifError(err);
migrator.down(argv, onComplete.bind(this, migrator));
});
});
}
function onComplete(migrator, err) {
migrator.driver.close();
assert.ifError(err);
log.info('Done');
}
function run() {
var action = argv._.shift();
switch(action) {
case 'create':
executeCreate();
break;
case 'up':
case 'down':
loadConfig();
if(argv._.length > 0) {
argv.destination = argv._.shift().toString();
}
if(action == 'up') {
executeUp();
} else {
executeDown();
}
break;
default:
log.error('Invalid Action: Must be [up|down|create].');
optimist.showHelp();
process.exit(1);
break;
}
}
run();