Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 192edf2

Browse files
committed
integration of ipfs-repo-migrations
The integration respect config's setting repoDisableAutoMigration that defines if migrations should be automatically applied or not. License: MIT Signed-off-by: Adam Uhlir <[email protected]>
1 parent 9bd9984 commit 192edf2

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

package-list.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
"Repo",
2020
["ipfs/js-ipfs-repo", "ipfs-repo"],
21+
["AuHau/js-ipfs-repo-migrations", "ipfs-repo-migrations"],
2122

2223
"Exchange",
2324
["ipfs/js-ipfs-block-service", "ipfs-block-service"],

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
"ipfs-mfs": "~0.10.2",
119119
"ipfs-multipart": "~0.1.0",
120120
"ipfs-repo": "~0.26.5",
121+
"ipfs-repo-migrations": "AuHau/js-ipfs-repo-migrations#dev",
121122
"ipfs-unixfs": "~0.1.16",
122123
"ipfs-unixfs-exporter": "~0.36.1",
123124
"ipfs-unixfs-importer": "~0.38.5",

src/core/boot.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ module.exports = (self) => {
2727
cb(null, true)
2828
})
2929
},
30+
// If migrations are enabled check & migrate repo
31+
(repoOpened, cb) => {
32+
self.config.get('repoDisableAutoMigration')
33+
.catch(err => {
34+
if (!err.message.match('does not exist')) {
35+
return Promise.reject(err)
36+
}
37+
38+
// Option not found, but default value is false, lets continue
39+
return false
40+
})
41+
.then(repoDisableAutoMigration => {
42+
if (repoOpened && !repoDisableAutoMigration) {
43+
self.log('checking for migrations')
44+
return self.repo.migrate()
45+
}
46+
})
47+
.then(() => cb(null, repoOpened))
48+
.catch(cb)
49+
},
3050
(repoOpened, cb) => {
3151
// Init with existing initialized, opened, repo
3252
if (repoOpened) {

src/core/components/repo.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const promisify = require('promisify-es6')
44
const repoVersion = require('ipfs-repo').repoVersion
5+
const log = require('debug')('ipfs:repo')
6+
const migrator = require('ipfs-repo-migrations')
57

68
module.exports = function repo (self) {
79
return {
@@ -38,6 +40,25 @@ module.exports = function repo (self) {
3840
})
3941
}),
4042

43+
migrate: async function tryMigrateRepo () {
44+
// Reads the repo version from datastore, not from the ipfs-repo package
45+
const currentRepoVersion = await migrator.getCurrentRepoVersion(self._repo.path)
46+
47+
if (currentRepoVersion >= repoVersion) {
48+
if (currentRepoVersion > repoVersion) {
49+
log('Your repo\'s version is higher then this version of js-ipfs require! You should revert it.')
50+
}
51+
52+
return // Nothing to migrate
53+
}
54+
55+
if (repoVersion > migrator.getLatestMigrationVersion()) {
56+
throw new Error('The ipfs-repo-migrations package does not have migration for version: ' + repoVersion)
57+
}
58+
59+
return migrator.migrate(self._repo.path, repoVersion, true, self._repo.options)
60+
},
61+
4162
gc: promisify((options, callback) => {
4263
if (typeof options === 'function') {
4364
callback = options

src/core/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const s = superstruct({
2727
const configSchema = s({
2828
repo: optional(s('object|string')),
2929
repoOwner: 'boolean?',
30+
repoDisableAutoMigration: 'boolean?',
3031
preload: s({
3132
enabled: 'boolean?',
3233
addresses: optional(s(['multiaddr'])),

0 commit comments

Comments
 (0)