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

Commit a3d1712

Browse files
AuHaudaviddias
andcommitted
fix: apply review suggestions
Co-Authored-By: David Dias <[email protected]>
1 parent 813f2aa commit a3d1712

File tree

2 files changed

+68
-83
lines changed

2 files changed

+68
-83
lines changed

README.md

+67-83
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ This framework:
4949
* Defines migrations API
5050
* Executes and reports migrations in both directions: forward and backward
5151
* Simplifies creation of new migrations
52+
* Works on the browser too!
5253

5354
## Install
5455

@@ -89,11 +90,61 @@ if(repoVersion < migrations.getLatestMigrationVersion()){
8990

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

92-
**For tools that build on top of `js-ipfs` and run mainly in the browser environment, be aware that disabling automatic
93-
migrations leaves the user with no way to run the migrations because there is no CLI in the browser. In such
94-
a case, you should provide a way to trigger migrations manually.**
93+
## API
94+
95+
### `.migrate(path, {toVersion, ignoreLock, repoOptions, onProgress, isDryRun}) -> Promise<void>`
96+
97+
Executes a forward migration to a specific version, or to the latest version if a specific version is not specified.
98+
99+
**Arguments:**
100+
101+
* `path` (string, mandatory) - path to the repo to be migrated
102+
* `options` (object, optional) - options for the migration
103+
* `options.toVersion` (int, optional) - version to which the repo should be migrated. Defaults to the latest migration version.
104+
* `options.ignoreLock` (bool, optional) - if true will not lock the repo when applying migrations. Use with caution.
105+
* `options.repoOptions` (object, optional) - options that are passed to migrations, that use them to construct the datastore. (options are the same as for IPFSRepo).
106+
* `options.onProgress` (function, optional) - callback that is called after finishing execution of each migration to report progress.
107+
* `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.
108+
109+
#### `onProgress(migration, counter, totalMigrations)`
110+
111+
Signature of the progress callback.
112+
113+
**Arguments:**
114+
* `migration` (object) - object of migration that just successfully finished running. See [Architecture of migrations](#architecture-of-migrations) for details.
115+
* `counter` (int) - index of current migration.
116+
* `totalMigrations` (int) - total count of migrations that will be run.
117+
118+
### `.revert(path, toVersion, {ignoreLock, options, onProgress, isDryRun}) -> Promise<void>`
119+
120+
Executes backward migration to a specific version.
121+
122+
**Arguments:**
123+
124+
* `path` (string, mandatory) - path to the repo to be reverted
125+
* `toVersion` (int, mandatory) - version to which the repo should be reverted to.
126+
* `options` (object, optional) - options for the reversion
127+
* `options.ignoreLock` (bool, optional) - if true will not lock the repo when applying migrations. Use with caution.
128+
* `options.options` (object, optional) - options that are passed to migrations, that use them to construct the datastore. (options are the same as for IPFSRepo).
129+
* `options.onProgress` (function, optional) - callback that is called after finishing execution of each migration to report progress.
130+
* `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.
131+
132+
### `getLatestMigrationVersion() -> int`
95133

96-
### Writing migration
134+
Return the version of the latest migration.
135+
136+
## CLI
137+
138+
The CLI is a NodeJS binary named `jsipfs-repo-migrations`.
139+
It has several commands:
140+
141+
* `migrate` - performs forward/backward migration to specific or latest version.
142+
* `status` - check repo for migrations that should be run.
143+
* `add` - bootstraps new migration.
144+
145+
For further details see the `--help` pages.
146+
147+
## Creating a new migration
97148

98149
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:
99150

@@ -106,7 +157,7 @@ If your migration has several parts, it should be fail-proof enough that if one
106157
are reverted before propagating the error. If possible then the outcome should be consistent repo so it migration could
107158
be run again.
108159

109-
#### Architecture of migrations
160+
### Architecture of a migration
110161

111162
All migrations are placed in the `/migrations` folder. Each folder there represents one migration that follows the migration
112163
API.
@@ -122,7 +173,7 @@ Each migration must follow this API. It must export an object in its `index.js`
122173
* `migrate` (function) - Function that performs the migration (see signature of this function below)
123174
* `revert` (function) - If defined then this function will revert the migration to the previous version. Otherwise it is assumed that it is not possible to revert this migration.
124175

125-
##### `migrate(repoPath, isBrowser)`
176+
#### `.migrate(repoPath, isBrowser)`
126177

127178
_Do not confuse this function with the `require('ipfs-repo-migrations').migrate()` function that drives the whole migration process!_
128179

@@ -131,7 +182,7 @@ Arguments:
131182
* `options` (object, optional) - object containing `IPFSRepo` options, that should be used to construct a datastore instance.
132183
* `isBrowser` (bool) - indicates if the migration is run in a browser environment (as opposed to NodeJS)
133184

134-
##### `revert(repoPath, isBrowser)`
185+
#### `.revert(repoPath, isBrowser)`
135186

136187
_Do not confuse this function with the `require('ipfs-repo-migrations').revert()` function that drives the whole backward migration process!_
137188

@@ -140,7 +191,7 @@ Arguments:
140191
* `options` (object, optional) - object containing `IPFSRepo` options, that should be used to construct the datastore instance.
141192
* `isBrowser` (bool) - indicates if the migration is run in a browser environment (as opposed to NodeJS)
142193

143-
#### Browser vs. NodeJS environments
194+
### Browser vs. NodeJS environments
144195

145196
The migration might need to distinguish in which environment it runs (browser vs. NodeJS). For this reason there is an argument
146197
`isBrowser` passed to migrations functions. But with simple migrations it should not be necessary to distinguish between
@@ -157,37 +208,23 @@ There are currently two main datastore implementations:
157208
So with simple migrations you shouldn't worry about the difference between `datastore-fs` and `datastore-level`
158209
and by default use the `datastore-fs` package (as the replace mechanism does not work vice versa).
159210

160-
#### Guidelines
211+
### Guidelines
161212

162213
The recommended way to write a new migration is to first bootstrap a dummy migration using the CLI:
163214

164215
```sh
165-
> jsipfs-migrations add
216+
> npm run new-migration
166217
```
167218

168219
A new folder is created with the bootstrapped migration. You can then simply fill in the required fields and
169220
write the rest of the migration!
170221

171-
#### Migration's dependencies
172-
173-
The size of the `js-ipfs` bundle is crucial for distribution in a browser environment, so dependency management of all related
174-
packages is important.
175-
176-
If a migration needs to depend on some package, this dependency should be declared in the root's `package.json`. The author
177-
of the migration should be thoughtful about adding dependencies that would significantly increase the size of the final bundle.
178-
179-
Most of the migration's dependencies will most likely overlap with `js-ipfs`'s dependencies and hence should not introduce
180-
any significant overhead, but it is necessary to keep the versions of these dependencies in sync with `js-ipfs`. For this
181-
reason migrations should be well tested to ensure correct behaviour over dependency updates.
182-
An update of some dependency could introduce breaking change. In such a case the next steps should be discussed with a broader
183-
audience.
184-
185-
#### Integration with js-ipfs
222+
### Integration with js-ipfs
186223

187224
When a new migration is created, the repo version in [`js-ipfs-repo`](https://github.com/ipfs/js-ipfs-repo) should be updated with the new version,
188225
together with updated version of this package. Then the updated version should be propagated to `js-ipfs`.
189226

190-
#### Tests
227+
### Tests
191228

192229
If a migration affects any of the following functionality, it must provide tests for the following functions
193230
to work under the version of the repo that it migrates to:
@@ -199,15 +236,15 @@ If a migration affects any of the following functionality, it must provide tests
199236
Every migration must have test coverage. Tests for migrations should be placed in the `/test/migrations/` folder. Most probably
200237
you will have to plug the tests into `browser.js`/`node.js` if they require specific bootstrapping on each platform.
201238

202-
#### Empty migrations
239+
### Empty migrations
203240

204241
For interop with go-ipfs it might be necessary just to bump a version of a repo without any actual
205242
modification as there might not be any changes needed in the JS implementation. For that purpose you can create an "empty migration".
206243

207244
The easiest way to do so is with the CLI:
208245

209246
```sh
210-
> jsipfs-migrations add --empty
247+
> npm run new-migration -- --empty
211248
```
212249

213250
This will create an empty migration with the next version.
@@ -218,62 +255,9 @@ This will create an empty migration with the next version.
218255
| -----------------: |:----------------:|
219256
| 7 | v0.0.0 - latest |
220257

258+
## Developer
221259

222-
## API
223-
224-
### `migrate(path, {toVersion, ignoreLock, repoOptions, onProgress, isDryRun}) -> Promise<void>`
225-
226-
Executes a forward migration to a specific version, or to the latest version if a specific version is not specified.
227-
228-
**Arguments:**
229-
230-
* `path` (string, mandatory) - path to the repo to be migrated
231-
* `options` (object, optional) - options for the migration
232-
* `options.toVersion` (int, optional) - version to which the repo should be migrated. Defaults to the latest migration version.
233-
* `options.ignoreLock` (bool, optional) - if true will not lock the repo when applying migrations. Use with caution.
234-
* `options.repoOptions` (object, optional) - options that are passed to migrations, that use them to construct the datastore. (options are the same as for IPFSRepo).
235-
* `options.onProgress` (function, optional) - callback that is called after finishing execution of each migration to report progress.
236-
* `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.
237-
238-
#### `onProgress(migration, counter, totalMigrations)`
239-
240-
Signature of the progress callback.
241-
242-
**Arguments:**
243-
* `migration` (object) - object of migration that just successfully finished running. See [Architecture of migrations](#architecture-of-migrations) for details.
244-
* `counter` (int) - index of current migration.
245-
* `totalMigrations` (int) - total count of migrations that will be run.
246-
247-
### `revert(path, toVersion, {ignoreLock, options, onProgress, isDryRun}) -> Promise<void>`
248-
249-
Executes backward migration to a specific version.
250-
251-
**Arguments:**
252-
253-
* `path` (string, mandatory) - path to the repo to be reverted
254-
* `toVersion` (int, mandatory) - version to which the repo should be reverted to.
255-
* `options` (object, optional) - options for the reversion
256-
* `options.ignoreLock` (bool, optional) - if true will not lock the repo when applying migrations. Use with caution.
257-
* `options.options` (object, optional) - options that are passed to migrations, that use them to construct the datastore. (options are the same as for IPFSRepo).
258-
* `options.onProgress` (function, optional) - callback that is called after finishing execution of each migration to report progress.
259-
* `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.
260-
261-
### `getLatestMigrationVersion() -> int`
262-
263-
Return the version of the latest migration.
264-
265-
## CLI
266-
267-
The CLI is a NodeJS binary named `jsipfs-repo-migrations`.
268-
It has several commands:
269-
270-
* `migrate` - performs forward/backward migration to specific or latest version.
271-
* `status` - check repo for migrations that should be run.
272-
* `add` - bootstraps new migration.
273-
274-
For further details see the `--help` pages.
275-
276-
## Versioning
260+
### Module versioning notes
277261

278262
In order to have good overview of what version of package contains what kind of migrations, this package follows this versioning schema: `0.<versionOfLastMigration>.<patches>`.
279263

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"url": "https://github.com/ipfs/js-ipfs-repo-migrations.git"
3333
},
3434
"scripts": {
35+
"new-migration": "./src/cli.js add",
3536
"test": "aegir test",
3637
"test:node": "aegir test --target node",
3738
"test:browser": "aegir test --target browser",

0 commit comments

Comments
 (0)