Skip to content

Commit

Permalink
soft lock on down
Browse files Browse the repository at this point in the history
  • Loading branch information
daveboulard committed Oct 14, 2020
1 parent b0d2a83 commit 6ef54c9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/actions/down.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ module.exports = async (db, client) => {
throw new Error("Could not migrate down, a lock is in place.");
}

try {
await lock.activate(db);
} catch(err) {
throw new Error(`Could not create a lock: ${err.message}`);
}

if (lastAppliedItem) {
try {
const migration = await migrationsDir.loadMigration(lastAppliedItem.fileName);
Expand All @@ -31,6 +37,7 @@ module.exports = async (db, client) => {
}

} catch (err) {
await lock.clear(db);
throw new Error(
`Could not migrate down ${lastAppliedItem.fileName}: ${err.message}`
);
Expand All @@ -45,5 +52,6 @@ module.exports = async (db, client) => {
}
}

await lock.clear(db);
return downgraded;
};
23 changes: 23 additions & 0 deletions test/actions/down.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ describe("down", () => {
}

return {
insertOne: sinon.stub().returns(Promise.resolve()),
createIndex: sinon.stub().returns(Promise.resolve()),
find: sinon.stub().returns(findStub),
deleteMany: sinon.stub().returns(Promise.resolve()),
}
}

Expand Down Expand Up @@ -209,6 +211,14 @@ describe("down", () => {
expect(items).to.deep.equal(["20160609113225-last_migration.js"]);
});

it("should lock if feature is enabled", async() => {
await down(db);
expect(changelogLockCollection.createIndex.called).to.equal(true);
expect(changelogLockCollection.find.called).to.equal(true);
expect(changelogLockCollection.insertOne.called).to.equal(true);
expect(changelogLockCollection.deleteMany.called).to.equal(true);
});

it("should ignore lock if feature is disabled", async() => {
config.read = sinon.stub().returns({
changelogCollectionName: "changelog",
Expand All @@ -227,6 +237,19 @@ describe("down", () => {
expect(changelogLockCollection.find.called).to.equal(false);
});

it("should yield an error when unable to create a lock", async() => {
changelogLockCollection.insertOne.returns(Promise.reject(new Error("Kernel panic")));

try {
await down(db);
expect.fail("Error was not thrown");
} catch (err) {
expect(err.message).to.deep.equal(
"Could not create a lock: Kernel panic"
);
}
});

it("should yield an error when changelog is locked", async() => {
const findStub = {
toArray: () => {
Expand Down

0 comments on commit 6ef54c9

Please sign in to comment.