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

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 May 20, 2019
1 parent 9bd9984 commit 192edf2
Show file tree
Hide file tree
Showing 5 changed files with 44 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 @@ -118,6 +118,7 @@
"ipfs-mfs": "~0.10.2",
"ipfs-multipart": "~0.1.0",
"ipfs-repo": "~0.26.5",
"ipfs-repo-migrations": "AuHau/js-ipfs-repo-migrations#dev",
"ipfs-unixfs": "~0.1.16",
"ipfs-unixfs-exporter": "~0.36.1",
"ipfs-unixfs-importer": "~0.38.5",
Expand Down
20 changes: 20 additions & 0 deletions src/core/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ module.exports = (self) => {
cb(null, true)
})
},
// If migrations are enabled check & migrate repo
(repoOpened, cb) => {
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 (repoOpened && !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 192edf2

Please sign in to comment.