From 555b25c79da44f9afad7619850c4246847899774 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Mon, 17 Aug 2020 08:28:51 +0100 Subject: [PATCH] fix: migrate empty repos 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. --- migrations/migration-9/index.js | 4 ++++ src/index.js | 4 ++-- test/migrations/migration-8-test.js | 15 +++++++++++++++ test/migrations/migration-9-test.js | 14 ++++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/migrations/migration-9/index.js b/migrations/migration-9/index.js index 0507711..57d52e9 100644 --- a/migrations/migration-9/index.js +++ b/migrations/migration-9/index.js @@ -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)) diff --git a/src/index.js b/src/index.js index d38e7e9..d5089e7 100644 --- a/src/index.js +++ b/src/index.js @@ -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`) diff --git a/test/migrations/migration-8-test.js b/test/migrations/migration-8-test.js index 4b2f280..1dc120d 100644 --- a/test/migrations/migration-8-test.js +++ b/test/migrations/migration-8-test.js @@ -1,4 +1,5 @@ /* eslint-env mocha */ +/* eslint-disable max-nested-callbacks */ 'use strict' const { expect } = require('aegir/utils/chai') @@ -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, () => {}) diff --git a/test/migrations/migration-9-test.js b/test/migrations/migration-9-test.js index b20fd70..a8a981f 100644 --- a/test/migrations/migration-9-test.js +++ b/test/migrations/migration-9-test.js @@ -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 = {}