Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Adam Uhlir <adam@uhlir.dev>
  • Loading branch information
AuHau committed Jul 23, 2019
1 parent b5bcbd8 commit b7b7468
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 39 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ Arguments:

* `path` (string, mandatory): the path for this repo
* `options` (object, optional): may contain the following values
* `autoMigrate` (bool, defaults to `true`): controls automatic migrations of repository.
* `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"`.
* `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):
* `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()`)
Expand Down
2 changes: 1 addition & 1 deletion src/default-options-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Default configuration for a repo in the browser
module.exports = {
disableAutoMigration: false,
autoMigrate: true,
lock: 'memory',
storageBackends: {
root: require('datastore-level'),
Expand Down
2 changes: 1 addition & 1 deletion src/default-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Default configuration for a repo in node.js
module.exports = {
disableAutoMigration: false,
autoMigrate: true,
lock: 'fs',
storageBackends: {
root: require('datastore-fs'),
Expand Down
30 changes: 13 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const ERRORS = require('./errors')
const log = debug('repo')

const noLimit = Number.MAX_SAFE_INTEGER
const AUTO_MIGRATE_CONFIG_KEY = 'repoAutoMigrate'

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

if (!await this.version.check(constants.repoVersion)) {
log('Something is fishy')
if (!this.options.disableAutoMigration) {
log('Let see what')
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.')
Expand Down Expand Up @@ -275,38 +275,34 @@ class IpfsRepo {
}
}

async _migrate (toVersion) {
let disableMigrationsConfig
async _isAutoMigrationEnabled () {
let autoMigrateConfig
try {
disableMigrationsConfig = await this.config.get('repoDisableAutoMigration')
autoMigrateConfig = await this.config.get(AUTO_MIGRATE_CONFIG_KEY)
} catch (e) {
if (e.code === ERRORS.NotFoundError.code) {
disableMigrationsConfig = false
autoMigrateConfig = true
} else {
throw e
}
}
log(`optin: ${this.options.autoMigrate}; config: ${autoMigrateConfig}`)

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

async _migrate (toVersion) {
const currentRepoVersion = await this.version.get()
log(currentRepoVersion)

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

if (currentRepoVersion === toVersion) {
log('Nothing to migrate')
return
}

if (toVersion > migrator.getLatestMigrationVersion()) {
throw new Error('The ipfs-repo-migrations package does not have migration for version: ' + toVersion)
}

log('migrating to version ' + toVersion)
return migrator.migrate(this.path, { toVersion: toVersion, ignoreLock: true, repoOptions: this.options })
}

Expand Down
28 changes: 9 additions & 19 deletions test/migrations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module.exports = (createTempRepo) => {
expect(migrateStub.called).to.be.true()
})

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

const newOpts = Object.assign({}, repo.options)
newOpts.disableAutoMigration = true
newOpts.autoMigrate = false
const newRepo = new IPFSRepo(repo.path, newOpts)

expect(migrateStub.called).to.be.false()
Expand All @@ -80,12 +80,12 @@ module.exports = (createTempRepo) => {
expect(migrateStub.called).to.be.false()
})

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

await repo.config.set('repoDisableAutoMigration', true)
await repo.config.set('repoAutoMigrate', false)
await repo.version.set(7)
await repo.close()

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

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

await repo.open()

expect(migrateStub.called).to.be.false()
})

it('should throw error if ipfs-repo-migrations does not contain expected migration', async () => {
migrateStub.resolves()
repoVersionStub.value(8)
getLatestMigrationVersionStub.returns(7)

await repo.version.set(7)
await repo.close()

try {
await repo.open()
throw Error('Should throw')
throw Error('Should throw error')
} catch (e) {
expect(e.message).to.include('package does not have migration')
expect(migrateStub.called).to.be.false()
expect(e.code).to.equal(errors.InvalidRepoVersionError.code)
}

expect(migrateStub.called).to.be.false()
})
})
}
2 changes: 1 addition & 1 deletion test/options-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function noop () {}

function expectedRepoOptions () {
const options = {
disableAutoMigration: false,
autoMigrate: true,
lock: process.browser ? 'memory' : 'fs',
storageBackends: {
// packages are exchanged to browser-compatible
Expand Down

0 comments on commit b7b7468

Please sign in to comment.