Skip to content

Commit b7b7468

Browse files
committed
Tweaks
License: MIT Signed-off-by: Adam Uhlir <[email protected]>
1 parent b5bcbd8 commit b7b7468

File tree

6 files changed

+26
-39
lines changed

6 files changed

+26
-39
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ Arguments:
137137

138138
* `path` (string, mandatory): the path for this repo
139139
* `options` (object, optional): may contain the following values
140+
* `autoMigrate` (bool, defaults to `true`): controls automatic migrations of repository.
140141
* `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"`.
141142
* `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):
142143
* `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()`)

src/default-options-browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// Default configuration for a repo in the browser
44
module.exports = {
5-
disableAutoMigration: false,
5+
autoMigrate: true,
66
lock: 'memory',
77
storageBackends: {
88
root: require('datastore-level'),

src/default-options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// Default configuration for a repo in node.js
44
module.exports = {
5-
disableAutoMigration: false,
5+
autoMigrate: true,
66
lock: 'fs',
77
storageBackends: {
88
root: require('datastore-fs'),

src/index.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const ERRORS = require('./errors')
2222
const log = debug('repo')
2323

2424
const noLimit = Number.MAX_SAFE_INTEGER
25+
const AUTO_MIGRATE_CONFIG_KEY = 'repoAutoMigrate'
2526

2627
const lockers = {
2728
memory: require('./lock-memory'),
@@ -93,10 +94,9 @@ class IpfsRepo {
9394
log('creating keystore')
9495
this.keys = backends.create('keys', path.join(this.path, 'keys'), this.options)
9596

96-
if (!await this.version.check(constants.repoVersion)) {
97-
log('Something is fishy')
98-
if (!this.options.disableAutoMigration) {
99-
log('Let see what')
97+
const isCompatible = await this.version.check(constants.repoVersion)
98+
if (!isCompatible) {
99+
if (await this._isAutoMigrationEnabled()) {
100100
await this._migrate(constants.repoVersion)
101101
} else {
102102
throw new ERRORS.InvalidRepoVersionError('Incompatible repo versions. Automatic migrations disabled. Please migrate the repo manually.')
@@ -275,38 +275,34 @@ class IpfsRepo {
275275
}
276276
}
277277

278-
async _migrate (toVersion) {
279-
let disableMigrationsConfig
278+
async _isAutoMigrationEnabled () {
279+
let autoMigrateConfig
280280
try {
281-
disableMigrationsConfig = await this.config.get('repoDisableAutoMigration')
281+
autoMigrateConfig = await this.config.get(AUTO_MIGRATE_CONFIG_KEY)
282282
} catch (e) {
283283
if (e.code === ERRORS.NotFoundError.code) {
284-
disableMigrationsConfig = false
284+
autoMigrateConfig = true
285285
} else {
286286
throw e
287287
}
288288
}
289+
log(`optin: ${this.options.autoMigrate}; config: ${autoMigrateConfig}`)
289290

290-
if (disableMigrationsConfig) {
291-
throw new ERRORS.InvalidRepoVersionError('Incompatible repo versions. Automatic migrations disabled. Please migrate the repo manually.')
292-
}
291+
return autoMigrateConfig && this.options.autoMigrate
292+
}
293293

294+
async _migrate (toVersion) {
294295
const currentRepoVersion = await this.version.get()
295-
log(currentRepoVersion)
296296

297297
if (currentRepoVersion > toVersion) {
298298
throw new ERRORS.InvalidRepoVersionError('Your repo\'s version is higher then this version of js-ipfs-repo require! You have to revert it.')
299299
}
300300

301301
if (currentRepoVersion === toVersion) {
302-
log('Nothing to migrate')
303302
return
304303
}
305304

306-
if (toVersion > migrator.getLatestMigrationVersion()) {
307-
throw new Error('The ipfs-repo-migrations package does not have migration for version: ' + toVersion)
308-
}
309-
305+
log('migrating to version ' + toVersion)
310306
return migrator.migrate(this.path, { toVersion: toVersion, ignoreLock: true, repoOptions: this.options })
311307
}
312308

test/migrations-test.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module.exports = (createTempRepo) => {
5757
expect(migrateStub.called).to.be.true()
5858
})
5959

60-
it('should not migrate when option disableAutoMigration is true', async () => {
60+
it('should not migrate when option autoMigrate is false', async () => {
6161
migrateStub.resolves()
6262
repoVersionStub.resolves(8)
6363
getLatestMigrationVersionStub.returns(9)
@@ -66,7 +66,7 @@ module.exports = (createTempRepo) => {
6666
await repo.close()
6767

6868
const newOpts = Object.assign({}, repo.options)
69-
newOpts.disableAutoMigration = true
69+
newOpts.autoMigrate = false
7070
const newRepo = new IPFSRepo(repo.path, newOpts)
7171

7272
expect(migrateStub.called).to.be.false()
@@ -80,12 +80,12 @@ module.exports = (createTempRepo) => {
8080
expect(migrateStub.called).to.be.false()
8181
})
8282

83-
it('should not migrate when config option repoDisableAutoMigration is true', async () => {
83+
it('should not migrate when config option repoAutoMigrate is false', async () => {
8484
migrateStub.resolves()
8585
repoVersionStub.resolves(8)
8686
getLatestMigrationVersionStub.returns(9)
8787

88-
await repo.config.set('repoDisableAutoMigration', true)
88+
await repo.config.set('repoAutoMigrate', false)
8989
await repo.version.set(7)
9090
await repo.close()
9191

@@ -122,25 +122,15 @@ module.exports = (createTempRepo) => {
122122

123123
expect(migrateStub.called).to.be.false()
124124

125-
await repo.open()
126-
127-
expect(migrateStub.called).to.be.false()
128-
})
129-
130-
it('should throw error if ipfs-repo-migrations does not contain expected migration', async () => {
131-
migrateStub.resolves()
132-
repoVersionStub.value(8)
133-
getLatestMigrationVersionStub.returns(7)
134-
135-
await repo.version.set(7)
136-
await repo.close()
137-
138125
try {
139126
await repo.open()
140-
throw Error('Should throw')
127+
throw Error('Should throw error')
141128
} catch (e) {
142-
expect(e.message).to.include('package does not have migration')
129+
expect(migrateStub.called).to.be.false()
130+
expect(e.code).to.equal(errors.InvalidRepoVersionError.code)
143131
}
132+
133+
expect(migrateStub.called).to.be.false()
144134
})
145135
})
146136
}

test/options-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function noop () {}
6666

6767
function expectedRepoOptions () {
6868
const options = {
69-
disableAutoMigration: false,
69+
autoMigrate: true,
7070
lock: process.browser ? 'memory' : 'fs',
7171
storageBackends: {
7272
// packages are exchanged to browser-compatible

0 commit comments

Comments
 (0)