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

Commit 267e718

Browse files
committed
fix: pass repo options when migration error occurs
1 parent f412607 commit 267e718

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ This package is inspired by the [go-ipfs repo migration tool](https://github.com
2929
- [Usage](#usage)
3030
- [API](#api)
3131
- [`.migrate(path, repoOptions, toVersion, {ignoreLock, onProgress, isDryRun}) -> Promise<void>`](#migratepath-repooptions-toversion-ignorelock-onprogress-isdryrun---promisevoid)
32-
- [`onProgress(versionFrom, versionTo, percent, message)`](#onprogressversionfrom-versionto-percent-message)
32+
- [`onProgress(version, percent, message)`](#onprogressversion-percent-message)
3333
- [`.revert(path, repoOptions, toVersion, {ignoreLock, onProgress, isDryRun}) -> Promise<void>`](#revertpath-repooptions-toversion-ignorelock-onprogress-isdryrun---promisevoid)
3434
- [`getLatestMigrationVersion() -> int`](#getlatestmigrationversion---int)
3535
- [Creating a new migration](#creating-a-new-migration)
@@ -118,10 +118,10 @@ Executes a forward migration to a specific version, or to the latest version if
118118
* `toVersion` (int, mandatory) - version to which the repo should be migrated.
119119
* `options` (object, optional) - options for the migration
120120
* `options.ignoreLock` (bool, optional) - if true will not lock the repo when applying migrations. Use with caution.
121-
* `options.onProgress` (function, optional) - callback that is called after finishing execution of each migration to report progress.
121+
* `options.onProgress` (function, optional) - callback that is called during each migration to report progress.
122122
* `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.
123123

124-
#### `onProgress(versionFrom, versionTo, percent, message)`
124+
#### `onProgress(version, percent, message)`
125125

126126
Signature of the progress callback.
127127

@@ -141,7 +141,7 @@ Executes backward migration to a specific version.
141141
* `toVersion` (int, mandatory) - version to which the repo should be reverted to.
142142
* `options` (object, optional) - options for the reversion
143143
* `options.ignoreLock` (bool, optional) - if true will not lock the repo when applying migrations. Use with caution.
144-
* `options.onProgress` (function, optional) - callback that is called after finishing execution of each migration to report progress.
144+
* `options.onProgress` (function, optional) - callback that is called during each migration to report progress.
145145
* `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.
146146

147147
### `getLatestMigrationVersion() -> int`

migrations/migration-8/index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,14 @@ async function process (repoPath, repoOptions, onProgress, keyFunction) {
3939
let blockCount
4040

4141
if (onProgress) {
42-
blockCount = await length(blockstore.query({ keysOnly: true }))
42+
blockCount = await length(blockstore.query({
43+
keysOnly: true,
44+
filters: [({ key }) => {
45+
const newKey = keyFunction(key)
46+
47+
return newKey.toString() !== key.toString()
48+
}]
49+
}))
4350
}
4451

4552
try {

src/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ async function migrate (path, repoOptions, toVersion, { ignoreLock = false, onPr
100100
let progressCallback
101101

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

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

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

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

209209
await migration.revert(path, repoOptions, progressCallback)

test/index.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ describe('index.js', () => {
265265
await expect(migrator.revert('/some/path', repoOptions, 2, options))
266266
.to.eventually.be.fulfilled()
267267

268-
expect(options.onProgress.getCall(0).calledWith(4, 3, '50.00', 'hello')).to.be.true()
268+
expect(options.onProgress.getCall(0).calledWith(3, '50.00', 'hello')).to.be.true()
269269
})
270270

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

457-
expect(options.onProgress.getCall(0).calledWith(2, 3, '50.00', 'hello')).to.be.true()
457+
expect(options.onProgress.getCall(0).calledWith(3, '50.00', 'hello')).to.be.true()
458458
})
459459

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

0 commit comments

Comments
 (0)