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

Commit

Permalink
fix: migrate empty repos (#35)
Browse files Browse the repository at this point in the history
When new repos are created without content, we should still be able to migrate them.

Also, do not overwrite properties on thrown errors as sometimes they are read-only.
  • Loading branch information
achingbrain committed Aug 17, 2020
1 parent 4ef8bc7 commit e48efad
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
4 changes: 4 additions & 0 deletions migrations/migration-9/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const { cidToKey, PIN_DS_KEY, PinTypes } = require('./utils')
const length = require('it-length')

async function pinsToDatastore (blockstore, datastore, pinstore, onProgress) {
if (!await datastore.has(PIN_DS_KEY)) {
return
}

const mh = await datastore.get(PIN_DS_KEY)
const cid = new CID(mh)
const pinRootBuf = await blockstore.get(cidToKey(cid))
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ async function migrate (path, repoOptions, toVersion, { ignoreLock = false, onPr
}
} catch (e) {
const lastSuccessfullyMigratedVersion = migration.version - 1

log(`An exception was raised during execution of migration. Setting the repo's version to last successfully migrated version: ${lastSuccessfullyMigratedVersion}`)
await repoVersion.setVersion(path, lastSuccessfullyMigratedVersion, repoOptions)

e.message = `During migration to version ${migration.version} exception was raised: ${e.message}`
throw e
throw new Error(`During migration to version ${migration.version} exception was raised: ${e.stack || e.message || e}`)
}

log(`Migrating to version ${migration.version} finished`)
Expand Down
15 changes: 15 additions & 0 deletions test/migrations/migration-8-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-env mocha */
/* eslint-disable max-nested-callbacks */
'use strict'

const { expect } = require('aegir/utils/chai')
Expand Down Expand Up @@ -89,6 +90,20 @@ module.exports = (setup, cleanup, repoOptions) => {
await cleanup(dir)
})

describe('empty repo', () => {
describe('forwards', () => {
it('should migrate pins forward', async () => {
await migration.migrate(dir, repoOptions, () => {})
})
})

describe('backwards', () => {
it('should migrate pins backward', async () => {
await migration.revert(dir, repoOptions, () => {})
})
})
})

it('should migrate blocks forward', async () => {
await bootstrapBlocks(dir, false, repoOptions)
await migration.migrate(dir, repoOptions, () => {})
Expand Down
14 changes: 14 additions & 0 deletions test/migrations/migration-9-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@ module.exports = (setup, cleanup, repoOptions) => {
await cleanup(dir)
})

describe('empty repo', () => {
describe('forwards', () => {
it('should migrate pins forward', async () => {
await migration.migrate(dir, repoOptions, () => {})
})
})

describe('backwards', () => {
it('should migrate pins backward', async () => {
await migration.revert(dir, repoOptions, () => {})
})
})
})

Object.keys(pinsets).forEach(title => {
const pinset = pinsets[title]
const pinned = {}
Expand Down

0 comments on commit e48efad

Please sign in to comment.