Skip to content

Commit 6166e1f

Browse files
authored
feat: expose onMigrationProgress option (#254)
Allows passing an `onMigrationProgress` option to get notifications of migration progress
1 parent 25e1f9f commit 6166e1f

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ Arguments:
179179
* `path` (string, mandatory): the path for this repo
180180
* `options` (object, optional): may contain the following values
181181
* `autoMigrate` (bool, defaults to `true`): controls automatic migrations of repository.
182+
* `onMigrationProgress` (function(fromVersion, toVersion, percentComplete, message)): callback function to be notified of migration progress
182183
* `lock` ([Lock](#lock) or string *Deprecated*): what type of lock to use. Lock has to be acquired when opening. string can be `"fs"` or `"memory"`.
183184
* `storageBackends` (object, optional): may contain the following values, which should each be a class implementing the [datastore interface](https://github.com/ipfs/interface-datastore#readme):
184185
* `root` (defaults to [`datastore-fs`](https://github.com/ipfs/js-datastore-fs#readme) in Node.js and [`datastore-level`](https://github.com/ipfs/js-datastore-level#readme) in the browser). Defines the back-end type used for gets and puts of values at the root (`repo.set()`, `repo.get()`)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"debug": "^4.1.0",
6565
"err-code": "^2.0.0",
6666
"interface-datastore": "^2.0.0",
67-
"ipfs-repo-migrations": "^4.0.0",
67+
"ipfs-repo-migrations": "^5.0.1",
6868
"ipfs-utils": "^2.3.1",
6969
"ipld-block": "^0.10.0",
7070
"it-map": "^1.0.2",

src/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,16 @@ class IpfsRepo {
334334

335335
if (currentRepoVersion > toVersion) {
336336
log('reverting to version ' + toVersion)
337-
return migrator.revert(this.path, this.options, toVersion, { ignoreLock: true })
337+
return migrator.revert(this.path, this.options, toVersion, {
338+
ignoreLock: true,
339+
onProgress: this.options.onMigrationProgress
340+
})
338341
} else {
339342
log('migrating to version ' + toVersion)
340-
return migrator.migrate(this.path, this.options, toVersion, { ignoreLock: true })
343+
return migrator.migrate(this.path, this.options, toVersion, {
344+
ignoreLock: true,
345+
onProgress: this.options.onMigrationProgress
346+
})
341347
}
342348
}
343349

test/migrations-test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,24 @@ module.exports = (createTempRepo) => {
9494
expect(migrateStub.called).to.be.true()
9595
})
9696

97+
it('should migrate with progress', async () => {
98+
migrateStub.resolves()
99+
repoVersionStub.value(8)
100+
getLatestMigrationVersionStub.returns(9)
101+
102+
await repo.version.set(7)
103+
await repo.close()
104+
105+
expect(migrateStub.called).to.be.false()
106+
107+
repo.options.onMigrationProgress = sinon.stub()
108+
109+
await repo.open()
110+
111+
expect(migrateStub.called).to.be.true()
112+
expect(migrateStub.getCall(0).args[3]).to.have.property('onProgress', repo.options.onMigrationProgress)
113+
})
114+
97115
it('should not migrate when versions matches', async () => {
98116
migrateStub.resolves()
99117
repoVersionStub.value(8)
@@ -125,5 +143,22 @@ module.exports = (createTempRepo) => {
125143
expect(revertStub.called).to.be.true()
126144
expect(migrateStub.called).to.be.false()
127145
})
146+
147+
it('should revert with progress', async () => {
148+
revertStub.resolves()
149+
repoVersionStub.value(8)
150+
151+
await repo.version.set(9)
152+
await repo.close()
153+
154+
expect(revertStub.called).to.be.false()
155+
156+
repo.options.onMigrationProgress = sinon.stub()
157+
158+
await repo.open()
159+
160+
expect(revertStub.called).to.be.true()
161+
expect(revertStub.getCall(0).args[3]).to.have.property('onProgress', repo.options.onMigrationProgress)
162+
})
128163
})
129164
}

0 commit comments

Comments
 (0)