@@ -6,7 +6,9 @@ const path = require('path')
6
6
const debug = require ( 'debug' )
7
7
const Big = require ( 'bignumber.js' )
8
8
const errcode = require ( 'err-code' )
9
+ const migrator = require ( 'ipfs-repo-migrations' )
9
10
11
+ const constants = require ( './constants' )
10
12
const backends = require ( './backends' )
11
13
const version = require ( './version' )
12
14
const config = require ( './config' )
@@ -26,7 +28,6 @@ const lockers = {
26
28
fs : require ( './lock' )
27
29
}
28
30
29
- const repoVersion = require ( './constants' ) . repoVersion
30
31
31
32
/**
32
33
* IpfsRepo implements all required functionality to read and write to an ipfs repo.
@@ -64,7 +65,7 @@ class IpfsRepo {
64
65
await this . _openRoot ( )
65
66
await this . config . set ( buildConfig ( config ) )
66
67
await this . spec . set ( buildDatastoreSpec ( config ) )
67
- await this . version . set ( repoVersion )
68
+ await this . version . set ( constants . repoVersion )
68
69
}
69
70
70
71
/**
@@ -92,6 +93,17 @@ class IpfsRepo {
92
93
this . blocks = await blockstore ( blocksBaseStore , this . options . storageBackendOptions . blocks )
93
94
log ( 'creating keystore' )
94
95
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
+
95
107
this . closed = false
96
108
log ( 'all opened' )
97
109
} catch ( err ) {
@@ -176,7 +188,7 @@ class IpfsRepo {
176
188
[ config ] = await Promise . all ( [
177
189
this . config . exists ( ) ,
178
190
this . spec . exists ( ) ,
179
- this . version . check ( repoVersion )
191
+ this . version . exists ( )
180
192
] )
181
193
} catch ( err ) {
182
194
if ( err . code === 'ERR_NOT_FOUND' ) {
@@ -239,7 +251,7 @@ class IpfsRepo {
239
251
* @return {Object }
240
252
*/
241
253
async stat ( options ) {
242
- options = Object . assign ( { } , { human : false } , options )
254
+ options = Object . assign ( { } , { human : false } , options )
243
255
let storageMax , blocks , version , datastore , keys
244
256
[ storageMax , blocks , version , datastore , keys ] = await Promise . all ( [
245
257
this . _storageMaxStat ( ) ,
@@ -264,6 +276,40 @@ class IpfsRepo {
264
276
}
265
277
}
266
278
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
+
267
313
async _storageMaxStat ( ) {
268
314
try {
269
315
const max = await this . config . get ( 'Datastore.StorageMax' )
@@ -284,7 +330,7 @@ class IpfsRepo {
284
330
. plus ( block . key . _buf . byteLength )
285
331
}
286
332
287
- return { count, size }
333
+ return { count, size}
288
334
}
289
335
}
290
336
@@ -298,7 +344,7 @@ async function getSize (queryFn) {
298
344
}
299
345
300
346
module . exports = IpfsRepo
301
- module . exports . repoVersion = repoVersion
347
+ module . exports . repoVersion = constants . repoVersion
302
348
module . exports . errors = ERRORS
303
349
304
350
function buildOptions ( _options ) {
0 commit comments