Skip to content

Commit

Permalink
fix: open datastores after migration (#255)
Browse files Browse the repository at this point in the history
Otherwise they can mess up migrations
  • Loading branch information
achingbrain authored Aug 15, 2020
1 parent 6166e1f commit 712ed2a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
5 changes: 4 additions & 1 deletion .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ module.exports = {
webpack: {
node: {
// this is needed until level stops using node buffers in browser code
Buffer: true
Buffer: true,

// needed by binary-parse-stream
stream: true
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"debug": "^4.1.0",
"err-code": "^2.0.0",
"interface-datastore": "^2.0.0",
"ipfs-repo-migrations": "^5.0.1",
"ipfs-repo-migrations": "^5.0.2",
"ipfs-utils": "^2.3.1",
"ipld-block": "^0.10.0",
"it-map": "^1.0.2",
Expand Down
2 changes: 1 addition & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict'

module.exports = {
repoVersion: 8
repoVersion: 9
}
20 changes: 11 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ class IpfsRepo {
await this._checkInitialized()
this.lockfile = await this._openLock(this.path)
log('acquired repo.lock')

const isCompatible = await this.version.check(constants.repoVersion)

if (!isCompatible) {
if (await this._isAutoMigrationEnabled()) {
await this._migrate(constants.repoVersion)
} else {
throw new ERRORS.InvalidRepoVersionError('Incompatible repo versions. Automatic migrations disabled. Please migrate the repo manually.')
}
}

log('creating datastore')
this.datastore = backends.create('datastore', pathJoin(this.path, 'datastore'), this.options)
await this.datastore.open()
Expand All @@ -125,15 +136,6 @@ class IpfsRepo {
this.pins = backends.create('pins', pathJoin(this.path, 'pins'), this.options)
await this.pins.open()

const isCompatible = await this.version.check(constants.repoVersion)
if (!isCompatible) {
if (await this._isAutoMigrationEnabled()) {
await this._migrate(constants.repoVersion)
} else {
throw new ERRORS.InvalidRepoVersionError('Incompatible repo versions. Automatic migrations disabled. Please migrate the repo manually.')
}
}

this.closed = false
log('all opened')
} catch (err) {
Expand Down
5 changes: 0 additions & 5 deletions test/interop-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ module.exports = (repo) => {
expect(values.map((value) => value.data.length)).to.eql([2659, 12783])
})

it('reads pin set from the datastore', async () => {
const val = await repo.datastore.get(new Key('/local/pins'))
expect(mh.toB58String(val)).to.equal('QmYAuyf2LzMba65NnhxLtGJxixKNUev9qYSu4MYM88hdwK')
})

it('reads DHT records from the datastore', async () => {
const val = await repo.datastore.get(new Key('/AHE5I5B7TY'))
expect(uint8ArrayToString(val, 'base16')).to.eql('0a0601c9d4743f9e12097465737476616c75651a2212201d22e2a5e140e5cd20d88fc59cd560f4887c7d9acf938ddb24d7207eac40fd2f')
Expand Down
4 changes: 2 additions & 2 deletions test/repo-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ module.exports = (repo) => {

describe('version', () => {
afterEach(async () => {
await repo.version.set(8)
await repo.version.set(9)
})

it('get version', async () => {
const version = await repo.version.get()
expect(version).to.equal(8)
expect(version).to.equal(9)
})

it('set version', async () => {
Expand Down

0 comments on commit 712ed2a

Please sign in to comment.