Skip to content

Commit

Permalink
Integration of ipfs-repo-migrations
Browse files Browse the repository at this point in the history
The integration respect config's setting repoDisableAutoMigration that
defines if migrations should be automatically applied or not.

License: MIT
Signed-off-by: Adam Uhlir <uhlir.a@gmail.com>
  • Loading branch information
AuHau committed Jun 27, 2019
1 parent 5044a30 commit 78f69d7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions package-list.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

"Repo",
["ipfs/js-ipfs-repo", "ipfs-repo"],
["AuHau/js-ipfs-repo-migrations", "ipfs-repo-migrations"],

"Exchange",
["ipfs/js-ipfs-block-service", "ipfs-block-service"],
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"ipfs-mfs": "~0.11.4",
"ipfs-multipart": "~0.1.0",
"ipfs-repo": "~0.26.6",
"ipfs-repo-migrations": "AuHau/js-ipfs-repo-migrations#dev",
"ipfs-unixfs": "~0.1.16",
"ipfs-unixfs-exporter": "~0.37.6",
"ipfs-unixfs-importer": "~0.39.9",
Expand Down
25 changes: 25 additions & 0 deletions src/core/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,31 @@ module.exports = (self) => {
cb(null, true)
})
},
// If migrations are enabled check & migrate repo
(repoOpened, cb) => {
// There is non-initialized repo => no migrations needed
if(!repoOpened){
return cb(null, repoOpened)
}

self.config.get('repoDisableAutoMigration')
.catch(err => {
if (!err.message.match('does not exist')) {
return Promise.reject(err)
}

// Option not found, but default value is false, lets continue
return false
})
.then(repoDisableAutoMigration => {
if (!repoDisableAutoMigration) {
self.log('checking for migrations')
return self.repo.migrate()
}
})
.then(() => cb(null, repoOpened))
.catch(cb)
},
(repoOpened, cb) => {
// Init with existing initialized, opened, repo
if (repoOpened) {
Expand Down
21 changes: 21 additions & 0 deletions src/core/components/repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

const promisify = require('promisify-es6')
const repoVersion = require('ipfs-repo').repoVersion
const log = require('debug')('ipfs:repo')
const migrator = require('ipfs-repo-migrations')

module.exports = function repo (self) {
return {
Expand Down Expand Up @@ -38,6 +40,25 @@ module.exports = function repo (self) {
})
}),

migrate: async function tryMigrateRepo () {
// Reads the repo version from datastore, not from the ipfs-repo package
const currentRepoVersion = await migrator.getCurrentRepoVersion(self._repo.path)

if (currentRepoVersion >= repoVersion) {
if (currentRepoVersion > repoVersion) {
log('Your repo\'s version is higher then this version of js-ipfs require! You should revert it.')
}

return // Nothing to migrate
}

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

return migrator.migrate(self._repo.path, repoVersion, true, self._repo.options)
},

gc: promisify((options, callback) => {
if (typeof options === 'function') {
callback = options
Expand Down
1 change: 1 addition & 0 deletions src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const s = superstruct({
const configSchema = s({
repo: optional(s('object|string')),
repoOwner: 'boolean?',
repoDisableAutoMigration: 'boolean?',
preload: s({
enabled: 'boolean?',
addresses: optional(s(['multiaddr'])),
Expand Down

0 comments on commit 78f69d7

Please sign in to comment.