Skip to content

Commit

Permalink
Refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
dolezel committed Jun 25, 2018
1 parent c4272f2 commit ed5a350
Showing 1 changed file with 52 additions and 40 deletions.
92 changes: 52 additions & 40 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const lock = async db => {
}
};

const getRunMigrations = async (db, options) => {
const ensureMigrationsTable = async (db, options) => {
try {
const schema = getMigrationTableSchema(options);
const { migrationsTable } = options;
Expand All @@ -99,20 +99,24 @@ const getRunMigrations = async (db, options) => {
template`CREATE TABLE "${fullTableName}" ( ${idColumn} SERIAL PRIMARY KEY, ${nameColumn} varchar(255) NOT NULL, ${runOnColumn} timestamp NOT NULL)`
);
}

if (!options.noLock) {
await lock(db, options);
}

return await db.column(
template`SELECT ${nameColumn} FROM "${fullTableName}" ORDER BY ${runOnColumn}, ${idColumn}`,
nameColumn
);
} catch (err) {
throw new Error(`Unable to fetch migrations: ${err.stack}`);
throw new Error(`Unable to ensure migrations table: ${err.stack}`);
}
};

const getRunMigrations = async (db, options) => {
const schema = getMigrationTableSchema(options);
const { migrationsTable } = options;
const fullTableName = {
schema,
name: migrationsTable
};
return db.column(
template`SELECT ${nameColumn} FROM "${fullTableName}" ORDER BY ${runOnColumn}, ${idColumn}`,
nameColumn
);
};

const getMigrationsToRun = (options, runNames, migrations) => {
if (options.direction === "down") {
const downMigrations = runNames
Expand Down Expand Up @@ -150,6 +154,28 @@ const getMigrationsToRun = (options, runNames, migrations) => {
);
};

const checkOrder = (runNames, migrations) => {
const len = Math.min(runNames.length, migrations.length);
for (let i = 0; i < len; i += 1) {
const runName = runNames[i];
const migrationName = migrations[i].name;
if (runName !== migrationName) {
throw new Error(
`Not run migration ${migrationName} is preceding already run migration ${runName}`
);
}
}
};

const runMigrations = (toRun, direction) =>
toRun.reduce(
(promise, migration) =>
promise.then(
() => (direction === "up" ? migration.applyUp() : migration.applyDown())
),
Promise.resolve()
);

export default async options => {
const log = options.log || console.log;
const db = Db(options.databaseUrl, log);
Expand All @@ -165,21 +191,20 @@ export default async options => {
`CREATE SCHEMA IF NOT EXISTS "${options.migrationsSchema}"`
);
}

await ensureMigrationsTable(db, options);

if (!options.noLock) {
await lock(db, options);
}

const [migrations, runNames] = await Promise.all([
loadMigrationFiles(db, options, log),
getRunMigrations(db, options)
]);

if (options.checkOrder) {
const len = Math.min(runNames.length, migrations.length);
for (let i = 0; i < len; i += 1) {
const runName = runNames[i];
const migrationName = migrations[i].name;
if (runName !== migrationName) {
throw new Error(
`Not run migration ${migrationName} is preceding already run migration ${runName}`
);
}
}
checkOrder(runNames, migrations);
}

const toRun = getMigrationsToRun(options, runNames, migrations);
Expand All @@ -197,29 +222,16 @@ export default async options => {

if (options.singleTransaction) {
await db.query("BEGIN");
}

try {
await toRun.reduce(
(promise, migration) =>
promise.then(
() =>
options.direction === "up"
? migration.applyUp()
: migration.applyDown()
),
Promise.resolve()
);

if (options.singleTransaction) {
try {
await runMigrations(toRun, options.direction);
await db.query("COMMIT");
}
} catch (err) {
if (options.singleTransaction) {
} catch (err) {
log("> Rolling back attempted migration ...");
await db.query("ROLLBACK");
throw err;
}
throw err;
} else {
await runMigrations(toRun, options.direction);
}
} finally {
db.close();
Expand Down

0 comments on commit ed5a350

Please sign in to comment.