Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.

Commit 725f821

Browse files
authored
fix: require passing repo options to migrator (#31)
Adds the changes released in `1.0.1` to master. BREAKING CHANGES: - `migrator.migrate(path, version, opts)` has changed to `migrator.migrate(path, repoOpts, version, opts)` - `migrator.revert(path, version, opts)` has changed to `migrator.revert(path, repoOpts, version, opts)`
1 parent 6f4acf7 commit 725f821

23 files changed

+386
-676
lines changed

.travis.yml

-7
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,6 @@ jobs:
4343
firefox: latest
4444
script: npx aegir test -t browser -- --browsers FirefoxHeadless
4545

46-
- stage: test
47-
name: sharness
48-
os:
49-
- linux
50-
- osx
51-
script: cd ./test/sharness && make
52-
5346
- stage: test
5447
name: electron-main
5548
os: osx

README.md

+14-22
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ This package is inspired by the [go-ipfs repo migration tool](https://github.com
2828
- [Use in a browser with browserify, webpack or any other bundler](#use-in-a-browser-with-browserify-webpack-or-any-other-bundler)
2929
- [Usage](#usage)
3030
- [API](#api)
31-
- [`.migrate(path, toVersion, {ignoreLock, repoOptions, onProgress, isDryRun}) -> Promise<void>`](#migratepath-toversion-ignorelock-repooptions-onprogress-isdryrun---promisevoid)
31+
- [`.migrate(path, repoOptions, toVersion, {ignoreLock, onProgress, isDryRun}) -> Promise<void>`](#migratepath-repooptions-toversion-ignorelock-onprogress-isdryrun---promisevoid)
3232
- [`onProgress(migration, counter, totalMigrations)`](#onprogressmigration-counter-totalmigrations)
33-
- [`.revert(path, toVersion, {ignoreLock, repoOptions, onProgress, isDryRun}) -> Promise<void>`](#revertpath-toversion-ignorelock-repooptions-onprogress-isdryrun---promisevoid)
33+
- [`.revert(path, repoOptions, toVersion, {ignoreLock, onProgress, isDryRun}) -> Promise<void>`](#revertpath-repooptions-toversion-ignorelock-onprogress-isdryrun---promisevoid)
3434
- [`getLatestMigrationVersion() -> int`](#getlatestmigrationversion---int)
35-
- [CLI](#cli)
3635
- [Creating a new migration](#creating-a-new-migration)
3736
- [Architecture of a migration](#architecture-of-a-migration)
3837
- [`.migrate(repoPath, repoOptions)`](#migraterepopath-repooptions)
@@ -50,7 +49,6 @@ This package is inspired by the [go-ipfs repo migration tool](https://github.com
5049

5150
## Background
5251

53-
5452
As js-ipfs evolves and new technologies, algorithms and data structures are incorporated it is necessary to
5553
enable users to transition between versions. Different versions of js-ipfs may expect a different IPFS repo structure or content (see: [IPFS repo spec](https://github.com/ipfs/specs/blob/master/REPO.md), [JS implementation](https://github.com/ipfs/js-ipfs-repo) ).
5654
So the IPFS repo is versioned, and this package provides a framework to create migrations to transition
@@ -93,28 +91,33 @@ const migrations = require('ipfs-repo-migrations')
9391
const repoPath = 'some/repo/path'
9492
const currentRepoVersion = 7
9593
const latestVersion = migrations.getLatestMigrationVersion()
94+
const repoOptions = {
95+
... // the same storage backend/storage options passed to `ipfs-repo`
96+
}
9697

9798
if(currentRepoVersion < latestVersion){
9899
// Old repo! Lets migrate to latest version!
99-
await migrations.migrate(repoPath, latestVersion)
100+
await migrations.migrate(repoPath, latestVersion, {
101+
repoOptions
102+
})
100103
}
101104
```
102105

103106
To migrate your repository using the CLI, see the [how to run migrations](./run.md) tutorial.
104107

105108
## API
106109

107-
### `.migrate(path, toVersion, {ignoreLock, repoOptions, onProgress, isDryRun}) -> Promise<void>`
110+
### `.migrate(path, repoOptions, toVersion, {ignoreLock, onProgress, isDryRun}) -> Promise<void>`
108111

109112
Executes a forward migration to a specific version, or to the latest version if a specific version is not specified.
110113

111114
**Arguments:**
112115

113116
* `path` (string, mandatory) - path to the repo to be migrated
117+
* `repoOptions` (object, mandatory) - options that are passed to migrations, that use them to construct the datastore. (options are the same as for IPFSRepo).
114118
* `toVersion` (int, mandatory) - version to which the repo should be migrated.
115119
* `options` (object, optional) - options for the migration
116120
* `options.ignoreLock` (bool, optional) - if true will not lock the repo when applying migrations. Use with caution.
117-
* `options.repoOptions` (object, optional) - options that are passed to migrations, that use them to construct the datastore. (options are the same as for IPFSRepo).
118121
* `options.onProgress` (function, optional) - callback that is called after finishing execution of each migration to report progress.
119122
* `options.isDryRun` (bool, optional) - flag that indicates if it is a dry run that should give the same output as running a migration but without making any actual changes.
120123

@@ -127,35 +130,24 @@ Signature of the progress callback.
127130
* `counter` (int) - index of current migration.
128131
* `totalMigrations` (int) - total count of migrations that will be run.
129132

130-
### `.revert(path, toVersion, {ignoreLock, repoOptions, onProgress, isDryRun}) -> Promise<void>`
133+
### `.revert(path, repoOptions, toVersion, {ignoreLock, onProgress, isDryRun}) -> Promise<void>`
131134

132135
Executes backward migration to a specific version.
133136

134137
**Arguments:**
135138

136139
* `path` (string, mandatory) - path to the repo to be reverted
140+
* `repoOptions` (object, mandatory) - options that are passed to migrations, that use them to construct the datastore. (options are the same as for IPFSRepo).
137141
* `toVersion` (int, mandatory) - version to which the repo should be reverted to.
138142
* `options` (object, optional) - options for the reversion
139143
* `options.ignoreLock` (bool, optional) - if true will not lock the repo when applying migrations. Use with caution.
140-
* `options.repoOptions` (object, optional) - options that are passed to migrations, that use them to construct the datastore. (options are the same as for IPFSRepo).
141144
* `options.onProgress` (function, optional) - callback that is called after finishing execution of each migration to report progress.
142145
* `options.isDryRun` (bool, optional) - flag that indicates if it is a dry run that should give the same output as running a migration but without making any actual changes.
143146

144147
### `getLatestMigrationVersion() -> int`
145148

146149
Return the version of the latest migration.
147150

148-
## CLI
149-
150-
The CLI is a NodeJS binary named `jsipfs-repo-migrations`.
151-
It has several commands:
152-
153-
* `migrate` - performs forward/backward migration to specific or latest version.
154-
* `status` - check repo for migrations that should be run.
155-
* `add` - bootstraps new migration.
156-
157-
For further details see the `--help` pages.
158-
159151
## Creating a new migration
160152

161153
Migrations are one of those things that can be extremely painful on users. At the end of the day, we want users never to have to think about it. The process should be:
@@ -174,8 +166,8 @@ be run again.
174166
All migrations are placed in the `/migrations` folder. Each folder there represents one migration that follows the migration
175167
API.
176168

177-
All migrations are collected in `/migrations/index.js`, which should not be edited manually. It is regenerated on
178-
every run of `jsipfs-migrations add` (manual changes should follow the same style of modifications).
169+
All migrations are collected in `/migrations/index.js`, which should not be edited manually.
170+
179171
**The order of migrations is important and migrations must be sorted in ascending order**.
180172

181173
Each migration must follow this API. It must export an object in its `index.js` that has following properties:

package.json

+1-7
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
"./src/repo/lock.js": "./src/repo/lock-memory.js",
2323
"datastore-fs": "datastore-level"
2424
},
25-
"bin": {
26-
"jsipfs-migrations": "./src/cli.js"
27-
},
2825
"repository": {
2926
"type": "git",
3027
"url": "https://github.com/ipfs/js-ipfs-repo-migrations.git"
@@ -45,7 +42,6 @@
4542
},
4643
"dependencies": {
4744
"cbor": "^5.0.2",
48-
"chalk": "^4.0.0",
4945
"cids": "^1.0.0",
5046
"datastore-core": "^2.0.0",
5147
"datastore-fs": "^2.0.0",
@@ -60,9 +56,7 @@
6056
"proper-lockfile": "^4.1.1",
6157
"protons": "^2.0.0",
6258
"uint8arrays": "^1.0.0",
63-
"varint": "^5.0.0",
64-
"yargs": "^15.3.1",
65-
"yargs-promise": "^1.1.0"
59+
"varint": "^5.0.0"
6660
},
6761
"devDependencies": {
6862
"aegir": "^25.0.0",

src/cli.js

-62
This file was deleted.

src/commands.js

-165
This file was deleted.

src/errors.js

+15
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,18 @@ class InvalidValueError extends Error {
6060

6161
InvalidValueError.code = 'ERR_INVALID_VALUE'
6262
exports.InvalidValueError = InvalidValueError
63+
64+
/**
65+
* Exception raised when config is not passed.
66+
*/
67+
class MissingRepoOptionsError extends Error {
68+
constructor (message) {
69+
super(message)
70+
this.name = 'MissingRepoOptionsError'
71+
this.code = 'ERR_MISSING_REPO_OPTIONS'
72+
this.message = message
73+
}
74+
}
75+
76+
MissingRepoOptionsError.code = 'ERR_MISSING_REPO_OPTIONS'
77+
exports.MissingRepoOptionsError = MissingRepoOptionsError

0 commit comments

Comments
 (0)