1
1
'use strict'
2
2
3
3
const migrations = require ( '../migrations' )
4
- const repo_version = require ( './repo/version' )
5
- const repo_lock = require ( './repo/lock' )
4
+ const repoVersion = require ( './repo/version' )
5
+ const repoLock = require ( './repo/lock' )
6
+ const isBrowser = require ( './option-node' )
7
+ const errors = require ( './errors' )
6
8
const debug = require ( 'debug' )
7
9
8
10
const log = debug ( 'js-ipfs-repo-migrations:migrator' )
9
11
10
- exports . getLatestMigrationVersion = getLatestMigrationVersion
11
12
12
13
/**
13
14
* Returns the version of latest migration.
14
15
*
15
- * @returns int
16
+ * @returns { int }
16
17
*/
17
18
function getLatestMigrationVersion ( ) {
18
19
return migrations [ migrations . length - 1 ] . version
19
20
}
21
+ exports . getLatestMigrationVersion = getLatestMigrationVersion
20
22
21
- async function migrate ( store , toVersion , progressCb , isDryRun ) {
23
+ /**
24
+ * Main function to execute forward migrations.
25
+ * It acquire lock on the provided path before doing any migrations.
26
+ *
27
+ * Signature of the progress callback is: function(migrationObject: object, currentMigrationNumber: int, totalMigrationsCount: int)
28
+ *
29
+ * @param {string } path - Path to initialized (!) JS-IPFS repo
30
+ * @param {int|undefined } toVersion - Version to which the repo should be migrated, if undefined repo will be migrated to the latest version.
31
+ * @param {function|undefined } progressCb - Callback which will be called after each executed migration to report progress
32
+ * @param {boolean|undefined } isDryRun - Allows to simulate the execution of the migrations without any effect.
33
+ * @returns {Promise<void> }
34
+ */
35
+ async function migrate ( path , toVersion , progressCb , isDryRun ) {
22
36
if ( toVersion && ( ! Number . isInteger ( toVersion ) || toVersion <= 0 ) ) {
23
37
throw new Error ( 'Version has to be positive integer!' )
24
38
}
25
39
toVersion = toVersion || getLatestMigrationVersion ( )
26
40
27
- const currentVersion = await repo_version . getVersion ( store )
41
+ const currentVersion = await repoVersion . getVersion ( path )
28
42
29
43
let lock
30
- if ( ! isDryRun ) lock = await repo_lock . lock ( currentVersion , store . path )
44
+ if ( ! isDryRun ) lock = await repoLock . lock ( currentVersion , path )
31
45
32
46
if ( currentVersion === toVersion ) {
33
47
log ( 'Nothing to migrate, skipping migrations.' )
@@ -44,7 +58,7 @@ async function migrate(store, toVersion, progressCb, isDryRun) {
44
58
log ( `Migrating version ${ migration . version } ` )
45
59
if ( ! isDryRun ) {
46
60
try {
47
- await migration . migrate ( store )
61
+ await migration . migrate ( path , isBrowser )
48
62
} catch ( e ) {
49
63
e . message = `During migration to version ${ migration . version } exception was raised: ${ e . message } `
50
64
throw e
@@ -55,16 +69,26 @@ async function migrate(store, toVersion, progressCb, isDryRun) {
55
69
}
56
70
}
57
71
58
- if ( ! isDryRun ) await repo_version . setVersion ( store , toVersion || getLatestMigrationVersion ( ) )
72
+ if ( ! isDryRun ) await repoVersion . setVersion ( path , toVersion || getLatestMigrationVersion ( ) )
59
73
log ( 'All migrations successfully migrated ' , toVersion !== undefined ? `to version ${ toVersion } !` : 'to latest version!' )
60
74
61
75
if ( ! isDryRun ) await lock . close ( )
62
- await store . close ( )
63
76
}
64
-
65
77
exports . migrate = migrate
66
78
67
- async function revert ( store , toVersion , progressCb , isDryRun ) {
79
+ /**
80
+ * Main function to execute backward migration (reversion).
81
+ * It acquire lock on the provided path before doing any migrations.
82
+ *
83
+ * Signature of the progress callback is: function(migrationObject: object, currentMigrationNumber: int, totalMigrationsCount: int)
84
+ *
85
+ * @param {string } path - Path to initialized (!) JS-IPFS repo
86
+ * @param {int } toVersion - Version to which the repo will be reverted.
87
+ * @param {function|undefined } progressCb - Callback which will be called after each reverted migration to report progress
88
+ * @param {boolean|undefined } isDryRun - Allows to simulate the execution of the reversion without any effect.
89
+ * @returns {Promise<void> }
90
+ */
91
+ async function revert ( path , toVersion , progressCb , isDryRun ) {
68
92
if ( ! toVersion ) {
69
93
throw new Error ( 'When reverting migrations, you have to specify to which version to revert!' )
70
94
}
@@ -73,19 +97,19 @@ async function revert(store, toVersion, progressCb, isDryRun) {
73
97
throw new Error ( 'Version has to be positive integer!' )
74
98
}
75
99
76
- const currentVersion = await repo_version . getVersion ( store )
100
+ const currentVersion = await repoVersion . getVersion ( path )
77
101
if ( currentVersion === toVersion ) {
78
102
log ( 'Nothing to revert, skipping reverting.' )
79
103
return
80
104
}
81
105
82
106
let { reversible, problematicMigration} = verifyReversibility ( currentVersion , toVersion )
83
107
if ( ! reversible ) {
84
- throw new Error ( `Migration version ${ problematicMigration } is not possible to revert! Cancelling reversion.` )
108
+ throw new errors . NonReversibleMigration ( `Migration version ${ problematicMigration } is not possible to revert! Cancelling reversion.` )
85
109
}
86
110
87
111
let lock
88
- if ( ! isDryRun ) lock = await repo_lock . lock ( currentVersion , store . path )
112
+ if ( ! isDryRun ) lock = await repoLock . lock ( currentVersion , path )
89
113
let counter = 0 , totalMigrations = currentVersion - toVersion
90
114
const reversedMigrationArray = migrations . reverse ( )
91
115
for ( let migration of reversedMigrationArray ) {
@@ -98,7 +122,7 @@ async function revert(store, toVersion, progressCb, isDryRun) {
98
122
log ( `Reverting migration version ${ migration . version } ` )
99
123
if ( ! isDryRun ) {
100
124
try {
101
- await migration . revert ( store )
125
+ await migration . revert ( path , isBrowser )
102
126
} catch ( e ) {
103
127
e . message = `During reversion to version ${ migration . version } exception was raised: ${ e . message } `
104
128
throw e
@@ -109,15 +133,20 @@ async function revert(store, toVersion, progressCb, isDryRun) {
109
133
}
110
134
}
111
135
112
- if ( ! isDryRun ) await repo_version . setVersion ( store , toVersion )
136
+ if ( ! isDryRun ) await repoVersion . setVersion ( path , toVersion )
113
137
log ( `All migrations successfully reverted to version ${ toVersion } !` )
114
138
115
139
if ( ! isDryRun ) await lock . close ( )
116
- await store . close ( )
117
140
}
118
-
119
141
exports . revert = revert
120
142
143
+ /**
144
+ * Function checks if all migrations in given range supports reversion.
145
+ *
146
+ * @param {int } fromVersion
147
+ * @param {int } toVersion
148
+ * @returns {object }
149
+ */
121
150
function verifyReversibility ( fromVersion , toVersion ) {
122
151
const reversedMigrationArray = migrations . reverse ( )
123
152
for ( let migration of reversedMigrationArray ) {
0 commit comments