2015-05-09 13:58:18 -04:00
|
|
|
// Migrator
|
|
|
|
// -------
|
2016-05-17 01:01:34 +10:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
import mkdirp from 'mkdirp';
|
2016-08-09 17:23:07 -04:00
|
|
|
import Promise from 'bluebird';
|
2016-05-17 01:01:34 +10:00
|
|
|
import {
|
2018-07-09 08:10:34 -04:00
|
|
|
assign,
|
|
|
|
bind,
|
|
|
|
difference,
|
|
|
|
each,
|
|
|
|
filter,
|
|
|
|
get,
|
|
|
|
isBoolean,
|
|
|
|
isEmpty,
|
|
|
|
isUndefined,
|
|
|
|
map,
|
|
|
|
max,
|
|
|
|
template,
|
|
|
|
} from 'lodash';
|
2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
2018-07-17 09:41:44 +02:00
|
|
|
import {
|
|
|
|
getLockTableName,
|
|
|
|
getLockTableNameWithSchema,
|
|
|
|
getTable,
|
|
|
|
getTableName,
|
|
|
|
} from './table-resolver';
|
|
|
|
import { getSchemaBuilder } from './table-creator';
|
|
|
|
import * as migrationListResolver from './migration-list-resolver';
|
2015-11-16 10:22:01 -06:00
|
|
|
|
2015-12-08 23:28:37 +02:00
|
|
|
function LockError(msg) {
|
2015-11-16 10:22:01 -06:00
|
|
|
this.name = 'MigrationLocked';
|
2015-12-08 23:28:37 +02:00
|
|
|
this.message = msg;
|
2015-11-16 10:22:01 -06:00
|
|
|
}
|
2018-07-17 09:41:44 +02:00
|
|
|
|
2015-11-16 10:22:01 -06:00
|
|
|
inherits(LockError, Error);
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2015-08-30 11:33:12 +03:00
|
|
|
const CONFIG_DEFAULT = Object.freeze({
|
|
|
|
extension: 'js',
|
2018-07-17 09:41:44 +02:00
|
|
|
loadExtensions: migrationListResolver.DEFAULT_LOAD_EXTENSIONS,
|
2015-08-30 11:33:12 +03:00
|
|
|
tableName: 'knex_migrations',
|
2018-04-05 01:19:08 +03:00
|
|
|
schemaName: null,
|
2015-08-30 11:33:12 +03:00
|
|
|
directory: './migrations',
|
2018-07-09 08:10:34 -04:00
|
|
|
disableTransactions: false,
|
2015-08-30 11:33:12 +03:00
|
|
|
});
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
// 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.
|
2015-05-20 11:08:27 -04:00
|
|
|
export default class Migrator {
|
|
|
|
constructor(knex) {
|
2018-04-05 01:19:08 +03:00
|
|
|
this.knex = knex;
|
2015-05-20 11:08:27 -04:00
|
|
|
this.config = this.setConfig(knex.client.config.migrations);
|
2018-04-05 01:19:08 +03:00
|
|
|
|
2017-10-18 03:50:30 -04:00
|
|
|
this._activeMigration = {
|
2018-07-09 08:10:34 -04:00
|
|
|
fileName: null,
|
|
|
|
};
|
2015-05-20 11:08:27 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2015-05-20 11:08:27 -04:00
|
|
|
// Migrators to the latest configuration.
|
|
|
|
latest(config) {
|
|
|
|
this.config = this.setConfig(config);
|
2018-07-17 09:41:44 +02:00
|
|
|
return migrationListResolver
|
|
|
|
.listAllAndCompleted(this.config, this.knex, this._absoluteConfigDir())
|
2015-05-20 11:08:27 -04:00
|
|
|
.tap(validateMigrationList)
|
|
|
|
.spread((all, completed) => {
|
2017-03-27 17:39:08 +03:00
|
|
|
const migrations = difference(all, completed);
|
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
const transactionForAll =
|
|
|
|
!this.config.disableTransactions &&
|
|
|
|
isEmpty(
|
|
|
|
filter(migrations, (name) => {
|
|
|
|
const migration = require(path.join(
|
|
|
|
this._absoluteConfigDir(),
|
|
|
|
name
|
|
|
|
));
|
|
|
|
return !this._useTransaction(migration);
|
|
|
|
})
|
|
|
|
);
|
2017-03-27 17:39:08 +03:00
|
|
|
|
|
|
|
if (transactionForAll) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return this.knex.transaction((trx) =>
|
|
|
|
this._runBatch(migrations, 'up', trx)
|
|
|
|
);
|
|
|
|
} else {
|
2017-03-27 17:39:08 +03:00
|
|
|
return this._runBatch(migrations, 'up');
|
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
});
|
2015-05-20 11:08:27 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2015-05-20 11:08:27 -04:00
|
|
|
// Rollback the last "batch" of migrations that were run.
|
|
|
|
rollback(config) {
|
|
|
|
return Promise.try(() => {
|
|
|
|
this.config = this.setConfig(config);
|
2018-07-17 09:41:44 +02:00
|
|
|
return migrationListResolver
|
|
|
|
.listAllAndCompleted(this.config, this.knex, this._absoluteConfigDir())
|
2015-05-20 11:08:27 -04:00
|
|
|
.tap(validateMigrationList)
|
|
|
|
.then((val) => this._getLastBatch(val))
|
|
|
|
.then((migrations) => {
|
2016-03-02 16:52:32 +01:00
|
|
|
return this._runBatch(map(migrations, 'name'), 'down');
|
2015-05-20 11:08:27 -04:00
|
|
|
});
|
2018-07-09 08:10:34 -04:00
|
|
|
});
|
2015-05-20 11:08:27 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2015-11-18 16:31:54 -06:00
|
|
|
status(config) {
|
|
|
|
this.config = this.setConfig(config);
|
|
|
|
|
|
|
|
return Promise.all([
|
2018-07-09 08:10:34 -04:00
|
|
|
getTable(this.knex, this.config.tableName, this.config.schemaName).select(
|
|
|
|
'*'
|
|
|
|
),
|
2018-07-17 09:41:44 +02:00
|
|
|
migrationListResolver.listAll(
|
|
|
|
this._absoluteConfigDir(),
|
|
|
|
this.config.loadExtensions
|
|
|
|
),
|
2018-07-09 08:10:34 -04:00
|
|
|
]).spread((db, code) => db.length - code.length);
|
2015-11-18 16:31:54 -06:00
|
|
|
}
|
|
|
|
|
2016-03-26 09:01:00 +01:00
|
|
|
// Retrieves and returns the current migration version we're on, as a promise.
|
|
|
|
// If no migrations have been run yet, return "none".
|
2015-05-20 11:08:27 -04:00
|
|
|
currentVersion(config) {
|
|
|
|
this.config = this.setConfig(config);
|
2018-07-17 09:41:44 +02:00
|
|
|
return migrationListResolver
|
|
|
|
.listCompleted(this.config.tableName, this.config.schemaName, this.knex)
|
|
|
|
.then((completed) => {
|
|
|
|
const val = max(map(completed, (value) => value.split('_')[0]));
|
|
|
|
return isUndefined(val) ? 'none' : val;
|
|
|
|
});
|
2015-05-20 11:08:27 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2015-12-13 15:05:31 +02:00
|
|
|
forceFreeMigrationsLock(config) {
|
|
|
|
this.config = this.setConfig(config);
|
2018-07-17 09:41:44 +02:00
|
|
|
const lockTable = getLockTableName(this.config.tableName);
|
2018-07-09 08:10:34 -04:00
|
|
|
return getSchemaBuilder(this.knex, this.config.schemaName)
|
|
|
|
.hasTable(lockTable)
|
|
|
|
.then((exist) => exist && this._freeLock());
|
2015-12-13 15:05:31 +02:00
|
|
|
}
|
|
|
|
|
2015-05-20 11:08:27 -04:00
|
|
|
// Creates a new migration, with a given name.
|
|
|
|
make(name, config) {
|
|
|
|
this.config = this.setConfig(config);
|
2016-10-11 11:00:46 +02:00
|
|
|
if (!name) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return Promise.reject(
|
|
|
|
new Error('A name must be specified for the generated migration')
|
|
|
|
);
|
2016-10-11 11:00:46 +02:00
|
|
|
}
|
|
|
|
|
2015-05-20 11:08:27 -04:00
|
|
|
return this._ensureFolder(config)
|
|
|
|
.then((val) => this._generateStubTemplate(val))
|
|
|
|
.then((val) => this._writeNewMigration(name, val));
|
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-03-26 09:01:00 +01:00
|
|
|
// Ensures a folder for the migrations exist, dependent on the migration
|
|
|
|
// config settings.
|
2015-05-20 11:08:27 -04:00
|
|
|
_ensureFolder() {
|
2016-05-17 01:01:34 +10:00
|
|
|
const dir = this._absoluteConfigDir();
|
2018-07-09 08:10:34 -04:00
|
|
|
return Promise.promisify(fs.stat, { context: fs })(dir).catch(() =>
|
|
|
|
Promise.promisify(mkdirp)(dir)
|
|
|
|
);
|
2015-05-20 11:08:27 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2015-11-16 10:22:01 -06:00
|
|
|
_isLocked(trx) {
|
2018-07-17 09:41:44 +02:00
|
|
|
const tableName = getLockTableName(this.config.tableName);
|
2018-04-05 01:19:08 +03:00
|
|
|
return getTable(this.knex, tableName, this.config.schemaName)
|
2015-11-16 10:22:01 -06:00
|
|
|
.transacting(trx)
|
|
|
|
.forUpdate()
|
|
|
|
.select('*')
|
2018-07-09 08:10:34 -04:00
|
|
|
.then((data) => data[0].is_locked);
|
2015-11-16 10:22:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
_lockMigrations(trx) {
|
2018-07-17 09:41:44 +02:00
|
|
|
const tableName = getLockTableName(this.config.tableName);
|
2018-04-05 01:19:08 +03:00
|
|
|
return getTable(this.knex, tableName, this.config.schemaName)
|
2015-11-16 10:22:01 -06:00
|
|
|
.transacting(trx)
|
|
|
|
.update({ is_locked: 1 });
|
|
|
|
}
|
|
|
|
|
2017-03-27 17:39:08 +03:00
|
|
|
_getLock(trx) {
|
2018-07-09 08:10:34 -04:00
|
|
|
const transact = trx ? (fn) => fn(trx) : (fn) => this.knex.transaction(fn);
|
|
|
|
return transact((trx) => {
|
2015-11-16 10:22:01 -06:00
|
|
|
return this._isLocked(trx)
|
2018-07-09 08:10:34 -04:00
|
|
|
.then((isLocked) => {
|
2015-11-16 10:22:01 -06:00
|
|
|
if (isLocked) {
|
2018-07-09 08:10:34 -04:00
|
|
|
throw new Error('Migration table is already locked');
|
2015-11-16 10:22:01 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(() => this._lockMigrations(trx));
|
2018-07-09 08:10:34 -04:00
|
|
|
}).catch((err) => {
|
2015-12-15 21:43:57 +02:00
|
|
|
throw new LockError(err.message);
|
2015-12-08 23:28:37 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-27 17:39:08 +03:00
|
|
|
_freeLock(trx = this.knex) {
|
2018-07-17 09:41:44 +02:00
|
|
|
const tableName = getLockTableName(this.config.tableName);
|
2018-07-09 08:10:34 -04:00
|
|
|
return getTable(trx, tableName, this.config.schemaName).update({
|
|
|
|
is_locked: 0,
|
|
|
|
});
|
2015-12-08 23:28:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run a batch of current migrations, in sequence.
|
2017-03-27 17:39:08 +03:00
|
|
|
_runBatch(migrations, direction, trx) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return (
|
|
|
|
this._getLock(trx)
|
|
|
|
// When there is a wrapping transaction, some migrations
|
|
|
|
// could have been done while waiting for the lock:
|
2018-07-17 09:41:44 +02:00
|
|
|
.then(
|
|
|
|
() =>
|
|
|
|
trx
|
|
|
|
? migrationListResolver.listCompleted(
|
|
|
|
this.config.tableName,
|
|
|
|
this.config.schemaName,
|
|
|
|
trx
|
|
|
|
)
|
|
|
|
: []
|
|
|
|
)
|
2018-07-09 08:10:34 -04:00
|
|
|
.then((completed) => (migrations = difference(migrations, completed)))
|
|
|
|
.then(() =>
|
|
|
|
Promise.all(
|
|
|
|
map(migrations, bind(this._validateMigrationStructure, this))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.then(() => this._latestBatchNumber(trx))
|
|
|
|
.then((batchNo) => {
|
|
|
|
if (direction === 'up') batchNo++;
|
|
|
|
return batchNo;
|
|
|
|
})
|
|
|
|
.then((batchNo) => {
|
|
|
|
return this._waterfallBatch(batchNo, migrations, direction, trx);
|
|
|
|
})
|
|
|
|
.tap(() => this._freeLock(trx))
|
|
|
|
.catch((error) => {
|
|
|
|
let cleanupReady = Promise.resolve();
|
|
|
|
|
|
|
|
if (error instanceof LockError) {
|
|
|
|
// If locking error do not free the lock.
|
|
|
|
this.knex.client.logger.warn(
|
|
|
|
`Can't take lock to run migrations: ${error.message}`
|
|
|
|
);
|
2018-05-29 17:42:03 +02:00
|
|
|
this.knex.client.logger.warn(
|
2018-07-09 08:10:34 -04:00
|
|
|
'If you are sure migrations are not running you can release the ' +
|
|
|
|
'lock manually by deleting all the rows from migrations lock ' +
|
|
|
|
'table: ' +
|
2018-07-17 09:41:44 +02:00
|
|
|
getLockTableNameWithSchema(
|
|
|
|
this.config.tableName,
|
|
|
|
this.config.schemaName
|
|
|
|
)
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
if (this._activeMigration.fileName) {
|
|
|
|
this.knex.client.logger.warn(
|
|
|
|
`migration file "${this._activeMigration.fileName}" failed`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
this.knex.client.logger.warn(
|
|
|
|
`migration failed with error: ${error.message}`
|
|
|
|
);
|
|
|
|
// If the error was not due to a locking issue, then remove the lock.
|
|
|
|
cleanupReady = this._freeLock(trx);
|
2017-12-08 12:26:14 +01:00
|
|
|
}
|
2017-03-27 17:39:08 +03:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
return cleanupReady.finally(function() {
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
2016-05-17 01:01:34 +10:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-03-26 09:01:00 +01:00
|
|
|
// Validates some migrations by requiring and checking for an `up` and `down`
|
|
|
|
// function.
|
2015-05-20 11:08:27 -04:00
|
|
|
_validateMigrationStructure(name) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const migration = require(path.join(this._absoluteConfigDir(), name));
|
2018-07-09 08:10:34 -04:00
|
|
|
if (
|
|
|
|
typeof migration.up !== 'function' ||
|
|
|
|
typeof migration.down !== 'function'
|
|
|
|
) {
|
|
|
|
throw new Error(
|
|
|
|
`Invalid migration: ${name} must have both an up and down function`
|
|
|
|
);
|
2015-05-20 11:08:27 -04:00
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-03-26 09:01:00 +01:00
|
|
|
// Generates the stub template for the current migration, returning a compiled
|
|
|
|
// template.
|
2015-05-20 11:08:27 -04:00
|
|
|
_generateStubTemplate() {
|
2018-07-09 08:10:34 -04:00
|
|
|
const stubPath =
|
|
|
|
this.config.stub ||
|
2016-05-17 01:01:34 +10:00
|
|
|
path.join(__dirname, 'stub', this.config.extension + '.stub');
|
2018-07-09 08:10:34 -04:00
|
|
|
return Promise.promisify(fs.readFile, { context: fs })(stubPath).then(
|
|
|
|
(stub) => template(stub.toString(), { variable: 'd' })
|
2016-05-17 01:01:34 +10:00
|
|
|
);
|
2015-05-20 11:08:27 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2015-05-20 11:08:27 -04:00
|
|
|
// Write a new migration to disk, using the config and generated filename,
|
|
|
|
// passing any `variables` given in the config to the template.
|
|
|
|
_writeNewMigration(name, tmpl) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const { config } = this;
|
|
|
|
const dir = this._absoluteConfigDir();
|
2015-05-09 13:58:18 -04:00
|
|
|
if (name[0] === '-') name = name.slice(1);
|
2016-05-18 19:59:24 +10:00
|
|
|
const filename = yyyymmddhhmmss() + '_' + name + '.' + config.extension;
|
2018-07-09 08:10:34 -04:00
|
|
|
return Promise.promisify(fs.writeFile, { context: fs })(
|
2015-05-09 13:58:18 -04:00
|
|
|
path.join(dir, filename),
|
|
|
|
tmpl(config.variables || {})
|
|
|
|
).return(path.join(dir, filename));
|
2015-05-20 11:08:27 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-03-26 09:01:00 +01:00
|
|
|
// Get the last batch of migrations, by name, ordered by insert id in reverse
|
|
|
|
// order.
|
2015-05-20 11:08:27 -04:00
|
|
|
_getLastBatch() {
|
2018-04-05 01:19:08 +03:00
|
|
|
const { tableName, schemaName } = this.config;
|
|
|
|
return getTable(this.knex, tableName, schemaName)
|
2015-05-20 11:08:27 -04:00
|
|
|
.where('batch', function(qb) {
|
2018-07-09 08:10:34 -04:00
|
|
|
qb.max('batch').from(getTableName(tableName, schemaName));
|
2015-05-20 11:08:27 -04:00
|
|
|
})
|
|
|
|
.orderBy('id', 'desc');
|
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2015-05-20 11:08:27 -04:00
|
|
|
// Returns the latest batch number.
|
2017-03-27 17:39:08 +03:00
|
|
|
_latestBatchNumber(trx = this.knex) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return trx
|
|
|
|
.from(getTableName(this.config.tableName, this.config.schemaName))
|
|
|
|
.max('batch as max_batch')
|
|
|
|
.then((obj) => obj[0].max_batch || 0);
|
2015-05-20 11:08:27 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-03-26 09:01:00 +01:00
|
|
|
// If transaction config for a single migration is defined, use that.
|
2015-08-30 14:57:55 +03:00
|
|
|
// Otherwise, rely on the common config. This allows enabling/disabling
|
2016-03-26 09:01:00 +01:00
|
|
|
// transaction for a single migration at will, regardless of the common
|
2015-08-30 14:57:55 +03:00
|
|
|
// config.
|
|
|
|
_useTransaction(migration, allTransactionsDisabled) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const singleTransactionValue = get(migration, 'config.transaction');
|
2015-08-30 14:57:55 +03:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
return isBoolean(singleTransactionValue)
|
|
|
|
? singleTransactionValue
|
|
|
|
: !allTransactionsDisabled;
|
2015-08-30 14:57:55 +03:00
|
|
|
}
|
|
|
|
|
2016-03-26 09:01:00 +01:00
|
|
|
// Runs a batch of `migrations` in a specified `direction`, saving the
|
|
|
|
// appropriate database information as the migrations are run.
|
2017-03-27 17:39:08 +03:00
|
|
|
_waterfallBatch(batchNo, migrations, direction, trx) {
|
|
|
|
const trxOrKnex = trx || this.knex;
|
2018-07-09 08:10:34 -04:00
|
|
|
const { tableName, schemaName, disableTransactions } = this.config;
|
2017-03-27 17:39:08 +03:00
|
|
|
const directory = this._absoluteConfigDir();
|
2018-07-09 08:10:34 -04:00
|
|
|
let current = Promise.bind({ failed: false, failedOn: 0 });
|
2016-05-18 19:59:24 +10:00
|
|
|
const log = [];
|
2016-03-02 16:52:32 +01:00
|
|
|
each(migrations, (migration) => {
|
2016-05-18 19:59:24 +10:00
|
|
|
const name = migration;
|
2017-10-18 03:50:30 -04:00
|
|
|
this._activeMigration.fileName = name;
|
2015-05-20 11:08:27 -04:00
|
|
|
migration = require(directory + '/' + name);
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-03-26 09:01:00 +01:00
|
|
|
// We're going to run each of the migrations in the current "up".
|
2018-07-09 08:10:34 -04:00
|
|
|
current = current
|
|
|
|
.then(() => {
|
|
|
|
if (!trx && this._useTransaction(migration, disableTransactions)) {
|
|
|
|
return this._transaction(migration, direction, name);
|
|
|
|
}
|
|
|
|
return warnPromise(migration[direction](trxOrKnex, Promise), name);
|
|
|
|
})
|
2017-03-27 17:39:08 +03:00
|
|
|
.then(() => {
|
|
|
|
log.push(path.join(directory, name));
|
|
|
|
if (direction === 'up') {
|
2018-04-05 01:19:08 +03:00
|
|
|
return trxOrKnex.into(getTableName(tableName, schemaName)).insert({
|
2017-03-27 17:39:08 +03:00
|
|
|
name,
|
|
|
|
batch: batchNo,
|
2018-07-09 08:10:34 -04:00
|
|
|
migration_time: new Date(),
|
2017-03-27 17:39:08 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (direction === 'down') {
|
2018-07-09 08:10:34 -04:00
|
|
|
return trxOrKnex
|
|
|
|
.from(getTableName(tableName, schemaName))
|
|
|
|
.where({ name })
|
|
|
|
.del();
|
2017-03-27 17:39:08 +03:00
|
|
|
}
|
|
|
|
});
|
2018-04-05 01:19:08 +03:00
|
|
|
});
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2015-05-20 11:08:27 -04:00
|
|
|
return current.thenReturn([batchNo, log]);
|
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2015-05-20 11:16:13 -04:00
|
|
|
_transaction(migration, direction, name) {
|
|
|
|
return this.knex.transaction((trx) => {
|
|
|
|
return warnPromise(migration[direction](trx, Promise), name, () => {
|
2018-07-09 08:10:34 -04:00
|
|
|
trx.commit();
|
|
|
|
});
|
|
|
|
});
|
2015-05-20 11:16:13 -04:00
|
|
|
}
|
|
|
|
|
2015-05-20 11:08:27 -04:00
|
|
|
_absoluteConfigDir() {
|
|
|
|
return path.resolve(process.cwd(), this.config.directory);
|
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2015-05-20 11:08:27 -04:00
|
|
|
setConfig(config) {
|
2015-08-30 11:33:12 +03:00
|
|
|
return assign({}, CONFIG_DEFAULT, this.config || {}, config);
|
2015-05-20 11:08:27 -04:00
|
|
|
}
|
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// Validates that migrations are present in the appropriate directories.
|
|
|
|
function validateMigrationList(migrations) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const all = migrations[0];
|
|
|
|
const completed = migrations[1];
|
|
|
|
const diff = difference(completed, all);
|
2016-03-02 16:52:32 +01:00
|
|
|
if (!isEmpty(diff)) {
|
2015-05-09 13:58:18 -04:00
|
|
|
throw new Error(
|
2018-07-09 08:10:34 -04:00
|
|
|
`The migration directory is corrupt, the following files are missing: ${diff.join(
|
|
|
|
', '
|
|
|
|
)}`
|
2015-05-09 13:58:18 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 11:16:13 -04:00
|
|
|
function warnPromise(value, name, fn) {
|
2015-05-09 13:58:18 -04:00
|
|
|
if (!value || typeof value.then !== 'function') {
|
2018-05-29 17:42:03 +02:00
|
|
|
this.knex.client.logger.warn(`migration ${name} did not return a promise`);
|
2018-07-09 08:10:34 -04:00
|
|
|
if (fn && typeof fn === 'function') fn();
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2016-03-26 09:01:00 +01:00
|
|
|
// Ensure that we have 2 places for each of the date segments.
|
2015-05-20 11:08:27 -04:00
|
|
|
function padDate(segment) {
|
2015-05-09 13:58:18 -04:00
|
|
|
segment = segment.toString();
|
2016-05-17 01:01:34 +10:00
|
|
|
return segment[1] ? segment : `0${segment}`;
|
2015-05-20 11:08:27 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-03-26 09:01:00 +01:00
|
|
|
// Get a date object in the correct format, without requiring a full out library
|
|
|
|
// like "moment.js".
|
2015-05-20 11:08:27 -04:00
|
|
|
function yyyymmddhhmmss() {
|
2016-05-17 01:01:34 +10:00
|
|
|
const d = new Date();
|
2018-07-09 08:10:34 -04:00
|
|
|
return (
|
|
|
|
d.getFullYear().toString() +
|
2017-03-27 17:39:08 +03:00
|
|
|
padDate(d.getMonth() + 1) +
|
|
|
|
padDate(d.getDate()) +
|
|
|
|
padDate(d.getHours()) +
|
|
|
|
padDate(d.getMinutes()) +
|
2018-07-09 08:10:34 -04:00
|
|
|
padDate(d.getSeconds())
|
|
|
|
);
|
2015-05-20 11:08:27 -04:00
|
|
|
}
|