Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.

Commit 076f300

Browse files
committed
fix: validate presence for all migrations in a range
1 parent 6ecba01 commit 076f300

File tree

2 files changed

+53
-22
lines changed

2 files changed

+53
-22
lines changed

src/index.js

+14-20
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ async function migrate (path, toVersion, { ignoreLock = false, repoOptions, onPr
6161
throw new errors.InvalidValueError('Version has to be positive integer!')
6262
}
6363

64-
if (toVersion > getLatestMigrationVersion(migrations)) {
65-
throw new errors.InvalidValueError('The ipfs-repo-migrations package does not have migration for version: ' + toVersion)
66-
}
67-
6864
const currentVersion = await repoVersion.getVersion(path)
6965

7066
if (currentVersion === toVersion) {
@@ -76,6 +72,8 @@ async function migrate (path, toVersion, { ignoreLock = false, repoOptions, onPr
7672
throw new errors.InvalidValueError(`Current repo's version (${currentVersion}) is higher then toVersion (${toVersion}), you probably wanted to revert it?`)
7773
}
7874

75+
verifyAvailableMigrations(migrations, currentVersion, toVersion)
76+
7977
let lock
8078
if (!isDryRun && !ignoreLock) lock = await repoLock.lock(currentVersion, path)
8179

@@ -158,10 +156,7 @@ async function revert (path, toVersion, { ignoreLock = false, repoOptions, onPro
158156
throw new errors.InvalidValueError(`Current repo's version (${currentVersion}) is lower then toVersion (${toVersion}), you probably wanted to migrate it?`)
159157
}
160158

161-
const reversibility = verifyReversibility(migrations, currentVersion, toVersion)
162-
if (!reversibility.reversible) {
163-
throw new errors.NonReversibleMigrationError(`It is not possible to revert to version ${toVersion} because migration version ${reversibility.version} is not reversible. Cancelling reversion.`)
164-
}
159+
verifyAvailableMigrations(migrations, toVersion, currentVersion, true)
165160

166161
let lock
167162
if (!isDryRun && !ignoreLock) lock = await repoLock.lock(currentVersion, path)
@@ -207,32 +202,31 @@ async function revert (path, toVersion, { ignoreLock = false, repoOptions, onPro
207202
exports.revert = revert
208203

209204
/**
210-
* Function checks if all migrations in given range supports reversion.
211-
* fromVersion > toVersion
205+
* Function checks if all migrations in given range are available.
212206
*
213207
* @param {array} migrations
214208
* @param {int} fromVersion
215209
* @param {int} toVersion
216-
* @returns {object}
210+
* @param {boolean} checkReversibility - Will additionally checks if all the migrations in the range are reversible
211+
* @returns {void}
217212
*/
218-
function verifyReversibility (migrations, fromVersion, toVersion) {
213+
function verifyAvailableMigrations (migrations, fromVersion, toVersion, checkReversibility = false) {
219214
let migrationCounter = 0
220215
for (const migration of migrations) {
221-
if (migration.version > fromVersion) {
216+
if (migration.version > toVersion) {
222217
break
223218
}
224219

225-
if (migration.version > toVersion) {
226-
if (!migration.revert) return { reversible: false, version: migration.version }
220+
if (migration.version > fromVersion) {
221+
if (checkReversibility && !migration.revert) {
222+
throw new errors.NonReversibleMigrationError(`It is not possible to revert to version ${fromVersion} because migration version ${migration.version} is not reversible. Cancelling reversion.`)
223+
}
227224

228225
migrationCounter++
229226
}
230227
}
231228

232-
if (migrationCounter !== (fromVersion - toVersion)) {
233-
throw new errors.NonReversibleMigrationError(`There are not enough migrations to perform the reversion!
234-
Expected ${(fromVersion - toVersion)} migrations, but there are only ${migrationCounter} migrations.`)
229+
if (migrationCounter !== (toVersion - fromVersion)) {
230+
throw new errors.InvalidValueError(`The ipfs-repo-migrations package does not have all migration to migrate from version ${fromVersion} to ${toVersion}`)
235231
}
236-
237-
return { reversible: true, version: undefined }
238232
}

test/index.spec.js

+39-2
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,45 @@ describe('index.js', () => {
285285
)
286286
})
287287

288-
it('should error if migrations does not exist', () => {
289-
const options = createOptions()
288+
it('should verify that all migrations are available', () => {
289+
const options = {
290+
migrations: [
291+
{
292+
version: 3,
293+
migrate: sinon.stub().resolves(),
294+
revert: sinon.stub().resolves()
295+
},
296+
{
297+
version: 4,
298+
migrate: sinon.stub().resolves(),
299+
revert: sinon.stub().resolves()
300+
}
301+
]
302+
}
303+
304+
getVersionStub.returns(1)
305+
306+
return expect(migrator.migrate('/some/path', 3, options))
307+
.to.eventually.be.rejectedWith(errors.InvalidValueError).with.property('code', errors.InvalidValueError.code)
308+
})
309+
310+
it('should verify that all migrations are available', () => {
311+
const options = {
312+
migrations: [
313+
{
314+
version: 3,
315+
migrate: sinon.stub().resolves(),
316+
revert: sinon.stub().resolves()
317+
},
318+
{
319+
version: 4,
320+
migrate: sinon.stub().resolves(),
321+
revert: sinon.stub().resolves()
322+
}
323+
]
324+
}
325+
326+
getVersionStub.returns(3)
290327

291328
return expect(migrator.migrate('/some/path', 5, options))
292329
.to.eventually.be.rejectedWith(errors.InvalidValueError).with.property('code', errors.InvalidValueError.code)

0 commit comments

Comments
 (0)