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

Commit

Permalink
fix: pass repo options when migration error occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Aug 15, 2020
1 parent f412607 commit 267e718
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ This package is inspired by the [go-ipfs repo migration tool](https://github.com
- [Usage](#usage)
- [API](#api)
- [`.migrate(path, repoOptions, toVersion, {ignoreLock, onProgress, isDryRun}) -> Promise<void>`](#migratepath-repooptions-toversion-ignorelock-onprogress-isdryrun---promisevoid)
- [`onProgress(versionFrom, versionTo, percent, message)`](#onprogressversionfrom-versionto-percent-message)
- [`onProgress(version, percent, message)`](#onprogressversion-percent-message)
- [`.revert(path, repoOptions, toVersion, {ignoreLock, onProgress, isDryRun}) -> Promise<void>`](#revertpath-repooptions-toversion-ignorelock-onprogress-isdryrun---promisevoid)
- [`getLatestMigrationVersion() -> int`](#getlatestmigrationversion---int)
- [Creating a new migration](#creating-a-new-migration)
Expand Down Expand Up @@ -118,10 +118,10 @@ Executes a forward migration to a specific version, or to the latest version if
* `toVersion` (int, mandatory) - version to which the repo should be migrated.
* `options` (object, optional) - options for the migration
* `options.ignoreLock` (bool, optional) - if true will not lock the repo when applying migrations. Use with caution.
* `options.onProgress` (function, optional) - callback that is called after finishing execution of each migration to report progress.
* `options.onProgress` (function, optional) - callback that is called during each migration to report progress.
* `options.isDryRun` (bool, optional) - flag that indicates if it is a dry run that should give the same output as running a migration but without making any actual changes.

#### `onProgress(versionFrom, versionTo, percent, message)`
#### `onProgress(version, percent, message)`

Signature of the progress callback.

Expand All @@ -141,7 +141,7 @@ Executes backward migration to a specific version.
* `toVersion` (int, mandatory) - version to which the repo should be reverted to.
* `options` (object, optional) - options for the reversion
* `options.ignoreLock` (bool, optional) - if true will not lock the repo when applying migrations. Use with caution.
* `options.onProgress` (function, optional) - callback that is called after finishing execution of each migration to report progress.
* `options.onProgress` (function, optional) - callback that is called during each migration to report progress.
* `options.isDryRun` (bool, optional) - flag that indicates if it is a dry run that should give the same output as running a migration but without making any actual changes.

### `getLatestMigrationVersion() -> int`
Expand Down
9 changes: 8 additions & 1 deletion migrations/migration-8/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ async function process (repoPath, repoOptions, onProgress, keyFunction) {
let blockCount

if (onProgress) {
blockCount = await length(blockstore.query({ keysOnly: true }))
blockCount = await length(blockstore.query({
keysOnly: true,
filters: [({ key }) => {
const newKey = keyFunction(key)

return newKey.toString() !== key.toString()
}]
}))
}

try {
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ async function migrate (path, repoOptions, toVersion, { ignoreLock = false, onPr
let progressCallback

if (onProgress) { // eslint-disable-line max-depth
progressCallback = (percent, message) => onProgress(currentVersion, migration.version, percent.toFixed(2), message)
progressCallback = (percent, message) => onProgress(migration.version, percent.toFixed(2), message)
}

await migration.migrate(path, repoOptions, progressCallback)
}
} catch (e) {
const lastSuccessfullyMigratedVersion = migration.version - 1
log(`An exception was raised during execution of migration. Setting the repo's version to last successfully migrated version: ${lastSuccessfullyMigratedVersion}`)
await repoVersion.setVersion(path, lastSuccessfullyMigratedVersion)
await repoVersion.setVersion(path, lastSuccessfullyMigratedVersion, repoOptions)

e.message = `During migration to version ${migration.version} exception was raised: ${e.message}`
throw e
Expand Down Expand Up @@ -203,7 +203,7 @@ async function revert (path, repoOptions, toVersion, { ignoreLock = false, onPro
let progressCallback

if (onProgress) { // eslint-disable-line max-depth
progressCallback = (percent, message) => onProgress(currentVersion, migration.version, percent.toFixed(2), message)
progressCallback = (percent, message) => onProgress(migration.version, percent.toFixed(2), message)
}

await migration.revert(path, repoOptions, progressCallback)
Expand Down
4 changes: 2 additions & 2 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ describe('index.js', () => {
await expect(migrator.revert('/some/path', repoOptions, 2, options))
.to.eventually.be.fulfilled()

expect(options.onProgress.getCall(0).calledWith(4, 3, '50.00', 'hello')).to.be.true()
expect(options.onProgress.getCall(0).calledWith(3, '50.00', 'hello')).to.be.true()
})

it('should unlock repo when error is thrown', async () => {
Expand Down Expand Up @@ -454,7 +454,7 @@ describe('index.js', () => {
await expect(migrator.migrate('/some/path', repoOptions, 4, options))
.to.eventually.be.fulfilled()

expect(options.onProgress.getCall(0).calledWith(2, 3, '50.00', 'hello')).to.be.true()
expect(options.onProgress.getCall(0).calledWith(3, '50.00', 'hello')).to.be.true()
})

it('should unlock repo when error is thrown', async () => {
Expand Down

0 comments on commit 267e718

Please sign in to comment.