Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using async/await #282

Merged
merged 2 commits into from
Jun 26, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Refactored
dolezel committed Jun 25, 2018
commit ed5a350446139411ffc1f599455276df3e34131e
92 changes: 52 additions & 40 deletions lib/runner.js
Original file line number Diff line number Diff line change
@@ -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;
@@ -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
@@ -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);
@@ -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);
@@ -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();