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

Commit e48efad

Browse files
authored
fix: migrate empty repos (#35)
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.
1 parent 4ef8bc7 commit e48efad

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

migrations/migration-9/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ const { cidToKey, PIN_DS_KEY, PinTypes } = require('./utils')
1111
const length = require('it-length')
1212

1313
async function pinsToDatastore (blockstore, datastore, pinstore, onProgress) {
14+
if (!await datastore.has(PIN_DS_KEY)) {
15+
return
16+
}
17+
1418
const mh = await datastore.get(PIN_DS_KEY)
1519
const cid = new CID(mh)
1620
const pinRootBuf = await blockstore.get(cidToKey(cid))

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ async function migrate (path, repoOptions, toVersion, { ignoreLock = false, onPr
107107
}
108108
} catch (e) {
109109
const lastSuccessfullyMigratedVersion = migration.version - 1
110+
110111
log(`An exception was raised during execution of migration. Setting the repo's version to last successfully migrated version: ${lastSuccessfullyMigratedVersion}`)
111112
await repoVersion.setVersion(path, lastSuccessfullyMigratedVersion, repoOptions)
112113

113-
e.message = `During migration to version ${migration.version} exception was raised: ${e.message}`
114-
throw e
114+
throw new Error(`During migration to version ${migration.version} exception was raised: ${e.stack || e.message || e}`)
115115
}
116116

117117
log(`Migrating to version ${migration.version} finished`)

test/migrations/migration-8-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-env mocha */
2+
/* eslint-disable max-nested-callbacks */
23
'use strict'
34

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

93+
describe('empty repo', () => {
94+
describe('forwards', () => {
95+
it('should migrate pins forward', async () => {
96+
await migration.migrate(dir, repoOptions, () => {})
97+
})
98+
})
99+
100+
describe('backwards', () => {
101+
it('should migrate pins backward', async () => {
102+
await migration.revert(dir, repoOptions, () => {})
103+
})
104+
})
105+
})
106+
92107
it('should migrate blocks forward', async () => {
93108
await bootstrapBlocks(dir, false, repoOptions)
94109
await migration.migrate(dir, repoOptions, () => {})

test/migrations/migration-9-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ module.exports = (setup, cleanup, repoOptions) => {
121121
await cleanup(dir)
122122
})
123123

124+
describe('empty repo', () => {
125+
describe('forwards', () => {
126+
it('should migrate pins forward', async () => {
127+
await migration.migrate(dir, repoOptions, () => {})
128+
})
129+
})
130+
131+
describe('backwards', () => {
132+
it('should migrate pins backward', async () => {
133+
await migration.revert(dir, repoOptions, () => {})
134+
})
135+
})
136+
})
137+
124138
Object.keys(pinsets).forEach(title => {
125139
const pinset = pinsets[title]
126140
const pinned = {}

0 commit comments

Comments
 (0)