-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
migrate.js
304 lines (273 loc) · 9.56 KB
/
migrate.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Migrate
// -------
"use strict";
var fs = require('fs');
var path = require('path');
var _ = require('lodash');
var mkdirp = require('mkdirp');
var Promise = require('./promise').Promise;
// The new migration we're performing, typically called from the `knex.migrate`
// interface on the main `knex` object. Passes the `knex` instance performing
// the migration.
var Migrate = function(instance) {
this.knex = instance;
};
Migrate.prototype = {
// Initializes the migration, taking an optional `config` object,
// for things like the `tableName`.
init: function(config) {
if (!this._init) {
this.config = _.defaults(config || {}, {
extension: 'js',
tableName: 'knex_migrations',
directory: process.cwd() + '/migrations'
});
if (this.config.directory.indexOf('./') === 0) {
this.config.directory = path.resolve(process.cwd(), this.config.directory);
}
this._init = Promise.all([
this.ensureFolder(config),
this.ensureTable(config)
]).bind(this);
}
return this._init;
},
// Ensures that the proper table has been created,
// dependent on the migration config settings.
ensureTable: function(config) {
var migration = this;
return this.knex.schema.hasTable(config.tableName)
.then(function(exists) {
if (!exists) return migration.createMigrationTable(config.tableName);
});
},
// Ensures a folder for the migrations exist, dependent on the
// migration config settings.
ensureFolder: function(config) {
return Promise.promisify(fs.stat, fs)(config.directory)
.catch(function() {
return Promise.promisify(mkdirp)(config.directory);
});
},
// Create the migration table, if it doesn't already exist.
createMigrationTable: function(tableName) {
return this.knex.schema.createTable(tableName, function(t) {
t.increments();
t.string('name');
t.integer('batch');
t.dateTime('migration_time');
});
},
// Migrates to the latest configuration.
latest: function(config) {
return this.init(config)
.then(this.migrationData)
.tap(validateMigrationList)
.spread(function(all, completed) {
return this.runBatch(_.difference(all, completed), 'up');
})
.bind();
},
// Rollback the last "batch" of migrations that were run.
rollback: function(config) {
return this.init(config)
.then(this.migrationData)
.tap(validateMigrationList)
.then(this.getLastBatch)
.then(function(migrations) {
return this.runBatch(_.pluck(migrations, 'name'), 'down');
})
.bind();
},
// Run a batch of current migrations, in sequence.
runBatch: function(migrations, direction) {
return Promise.map(migrations, validateMigrationStructure(this))
.bind(this)
.then(function(migrations) {
return this.latestBatchNumber().then(function(batchNo) {
if (direction === 'up') batchNo++;
return batchNo;
}).then(this.waterfallBatch(migrations, direction));
});
},
// Retrieves and returns the current migration version
// we're on, as a promise. If there aren't any migrations run yet,
// return "none" as the value for the `currentVersion`.
currentVersion: function(config) {
return this.listCompleted(config).then(function(completed) {
var val = _.chain(completed).map(function(value) {
return value.split('_')[0];
}).max().value();
return (val === -Infinity ? 'none' : val);
});
},
// Creates a new migration, with a given name.
make: function(name, config) {
if (!name) Promise.rejected(new Error('A name must be specified for the generated migration'));
return this.init(config)
.then(this.generateStubTemplate)
.then(this.writeNewMigration(name));
},
// Lists all available migration versions, as a sorted array.
listAll: function(config) {
return this.init(config)
.then(function() {
return Promise.promisify(fs.readdir, fs)(this.config.directory);
})
.then(function(migrations) {
var ext = this.config.extension;
return _.filter(migrations, function (value) {
return value.indexOf(ext, value.length - ext.length) !== -1;
}).sort();
});
},
// Lists all migrations that have been completed for the current db, as an array.
listCompleted: function(config) {
return this.init(config)
.then(function() {
return this.knex(this.config.tableName).orderBy('id').select('name');
})
.then(function(migrations) {
return _.pluck(migrations, 'name');
})
.bind();
},
// Gets the migration list from the specified migration directory,
// as well as the list of completed migrations to check what
// should be run.
migrationData: function() {
return Promise.all([
this.listAll(),
this.listCompleted()
]);
},
// Generates the stub template for the current migration, returning a compiled template.
generateStubTemplate: function() {
var stubPath = this.config.stub || path.join(__dirname, 'stub', this.config.extension + '.stub');
return Promise.promisify(fs.readFile, fs)(stubPath).then(function(stub) {
return _.template(stub.toString(), null, {variable: 'd'});
});
},
// Write a new migration to disk, using the config and generated filename,
// passing any `variables` given in the config to the template.
writeNewMigration: function(name) {
var config = this.config;
return function(tmpl) {
if (name[0] === '-') name = name.slice(1);
var filename = yyyymmddhhmmss() + '_' + name + '.' + config.extension;
return Promise.promisify(fs.writeFile, fs)(
path.join(config.directory, filename),
tmpl(config.variables || {})
).yield(filename);
};
},
// Get the last batch of migrations, by name, ordered by insert id
// in reverse order.
getLastBatch: function() {
var knex = this.knex;
var tableName = this.config.tableName;
return this.knex(tableName)
.where('batch', function() {
this.select(knex.raw('MAX(batch)')).from(tableName);
})
.orderBy('id', 'desc');
},
// Returns the latest batch number.
latestBatchNumber: function() {
return this.knex(this.config.tableName)
.max('batch as batchNo').then(function(obj) {
return (obj[0].batchNo || 0);
});
},
// Runs a batch of `migrations` in a specified `direction`,
// saving the appropriate database information as the migrations are run.
waterfallBatch: function(migrations, direction) {
var knex = this.knex;
var tableName = this.config.tableName;
var current = Promise.fulfilled().bind({failed: false, failedOn: 0});
var log = [];
return function(batchNo) {
_.each(migrations, function(migration, i) {
var name = migration[0];
migration = migration[1];
// We're going to run each of the migrations in the current "up"
current = current.then(function() {
return migration[direction](knex, Promise);
}).then(function() {
log.push(name);
if (direction === 'up') {
return knex(tableName).insert({
name: name,
batch: batchNo,
migration_time: new Date()
});
}
if (direction === 'down') {
return knex(tableName).where({name: name}).del();
}
});
});
return current.yield([batchNo, log]);
};
}
};
// Validates some migrations by requiring and checking for an `up` and `down` function,
// returning an array with the `item` and `migration` require.
function validateMigrationStructure(migrator) {
return function(item) {
var migration = require(migrator.config.directory + '/' + item);
if (!_.isFunction(migration.up) || !_.isFunction(migration.down)) {
throw new Error('Invalid migration: ' + item + ' must have both an up and down function');
}
return [item, migration];
};
}
// Validates that migrations are present in the appropriate directories.
function validateMigrationList(all, completed) {
var diff = _.difference(completed, all);
if (!_.isEmpty(diff)) {
throw new Error(
'The migration directory is corrupt, the following files are missing: ' + diff.join(', ')
);
}
}
// Gets the current migration.
var getMigration = function(all, version, config) {
var found = _.find(all, function(item) {
item.indexOf(version) === 0;
});
if (!found) throw new Error('Unable to locate the specified migration ' + version);
return path.join(config.directory, found);
};
// Parse the version, which really only needs to be the
// timestamp of the migration we wish to migrate to.
var parseVersion = function(version) {
if (version !== 'latest') {
version = version.slice(0, 14);
if (version.length !== 14) {
throw new Error('Invalid migration provided');
}
}
return version;
};
// Get a date object in the correct format, without requiring
// a full out library like "moment.js".
var yyyymmddhhmmss = function() {
var d = new Date();
return d.getFullYear().toString() +
padDate(d.getMonth() + 1) +
padDate(d.getDate()) +
padDate(d.getHours()) +
padDate(d.getMinutes()) +
padDate(d.getSeconds());
};
// Ensure that we have 2 places for each of the date segments
var padDate = function(segment) {
segment = segment.toString();
return segment[1] ? segment : '0' + segment;
};
// Dasherize the string.
var dasherize = function(str) {
return str.replace(/([A-Z])/g, '-$1').replace(/[-_\s]+/g, '-').toLowerCase();
};
module.exports = Migrate;