@@ -6,7 +6,9 @@ const path = require('path')
66const debug = require ( 'debug' )
77const Big = require ( 'bignumber.js' )
88const errcode = require ( 'err-code' )
9+ const migrator = require ( 'ipfs-repo-migrations' )
910
11+ const constants = require ( './constants' )
1012const backends = require ( './backends' )
1113const version = require ( './version' )
1214const config = require ( './config' )
@@ -26,7 +28,6 @@ const lockers = {
2628 fs : require ( './lock' )
2729}
2830
29- const repoVersion = require ( './constants' ) . repoVersion
3031
3132/**
3233 * IpfsRepo implements all required functionality to read and write to an ipfs repo.
@@ -64,7 +65,7 @@ class IpfsRepo {
6465 await this . _openRoot ( )
6566 await this . config . set ( buildConfig ( config ) )
6667 await this . spec . set ( buildDatastoreSpec ( config ) )
67- await this . version . set ( repoVersion )
68+ await this . version . set ( constants . repoVersion )
6869 }
6970
7071 /**
@@ -92,6 +93,17 @@ class IpfsRepo {
9293 this . blocks = await blockstore ( blocksBaseStore , this . options . storageBackendOptions . blocks )
9394 log ( 'creating keystore' )
9495 this . keys = backends . create ( 'keys' , path . join ( this . path , 'keys' ) , this . options )
96+
97+ if ( ! await this . version . check ( constants . repoVersion ) ) {
98+ log ( 'Something is fishy' )
99+ if ( ! this . options . disableAutoMigration ) {
100+ log ( 'Let see what' )
101+ await this . _migrate ( constants . repoVersion )
102+ } else {
103+ throw new ERRORS . InvalidRepoVersionError ( 'Incompatible repo versions. Automatic migrations disabled. Please migrate the repo manually.' )
104+ }
105+ }
106+
95107 this . closed = false
96108 log ( 'all opened' )
97109 } catch ( err ) {
@@ -176,7 +188,7 @@ class IpfsRepo {
176188 [ config ] = await Promise . all ( [
177189 this . config . exists ( ) ,
178190 this . spec . exists ( ) ,
179- this . version . check ( repoVersion )
191+ this . version . exists ( )
180192 ] )
181193 } catch ( err ) {
182194 if ( err . code === 'ERR_NOT_FOUND' ) {
@@ -239,7 +251,7 @@ class IpfsRepo {
239251 * @return {Object }
240252 */
241253 async stat ( options ) {
242- options = Object . assign ( { } , { human : false } , options )
254+ options = Object . assign ( { } , { human : false } , options )
243255 let storageMax , blocks , version , datastore , keys
244256 [ storageMax , blocks , version , datastore , keys ] = await Promise . all ( [
245257 this . _storageMaxStat ( ) ,
@@ -264,6 +276,40 @@ class IpfsRepo {
264276 }
265277 }
266278
279+ async _migrate ( toVersion ) {
280+ let disableMigrationsConfig
281+ try {
282+ disableMigrationsConfig = await this . config . get ( 'repoDisableAutoMigration' )
283+ } catch ( e ) {
284+ if ( e . code === ERRORS . NotFoundError . code ) {
285+ disableMigrationsConfig = false
286+ } else {
287+ throw e
288+ }
289+ }
290+
291+ if ( disableMigrationsConfig ) {
292+ throw new ERRORS . InvalidRepoVersionError ( 'Incompatible repo versions. Automatic migrations disabled. Please migrate the repo manually.' )
293+ }
294+
295+ const currentRepoVersion = await this . version . get ( )
296+ log ( currentRepoVersion )
297+ if ( currentRepoVersion >= toVersion ) {
298+ if ( currentRepoVersion > toVersion ) {
299+ log ( 'Your repo\'s version is higher then this version of js-ipfs-repo require! You should revert it.' )
300+ }
301+
302+ log ( 'Nothing to migrate' )
303+ return
304+ }
305+
306+ if ( toVersion > migrator . getLatestMigrationVersion ( ) ) {
307+ throw new Error ( 'The ipfs-repo-migrations package does not have migration for version: ' + toVersion )
308+ }
309+
310+ return migrator . migrate ( this . path , { toVersion : toVersion , ignoreLock : true , repoOptions : this . options } )
311+ }
312+
267313 async _storageMaxStat ( ) {
268314 try {
269315 const max = await this . config . get ( 'Datastore.StorageMax' )
@@ -284,7 +330,7 @@ class IpfsRepo {
284330 . plus ( block . key . _buf . byteLength )
285331 }
286332
287- return { count, size }
333+ return { count, size}
288334 }
289335}
290336
@@ -298,7 +344,7 @@ async function getSize (queryFn) {
298344}
299345
300346module . exports = IpfsRepo
301- module . exports . repoVersion = repoVersion
347+ module . exports . repoVersion = constants . repoVersion
302348module . exports . errors = ERRORS
303349
304350function buildOptions ( _options ) {
0 commit comments