Skip to content

Commit

Permalink
Create context per run (#419)
Browse files Browse the repository at this point in the history
This adds a "lazy" option to the `context` and `migrations` options, which allows for:

1) more customisation in child classes
2) it's a less dangerous flow if two call sites try to call `up` at the same time and do things like mutating `context`, which in theory is possible now

Breaking change to the `beta` tag: `beforeAll`/`afterAll` are replaced by `beforeCommand`/`afterCommand`, which also run on `executed` and `pending` commands too now. For hooks that should only run before migrations are applied/reverted, use:

```js
umzug.on('beforeCommand', ev => {
  if (ev.command !== 'up' && ev.command !== 'down') {
    return
  }
  // ...whatever you were doing in beforeAll/afterAll before
})
```

Easiest to view change without whitespace: https://github.com/sequelize/umzug/pull/419/files?w=1
  • Loading branch information
mmkal authored Dec 15, 2020
1 parent d4c0b45 commit 4d2d757
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 115 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,8 @@ Umzug is an [emittery event emitter](https://www.npmjs.com/package/emittery). Ea

These events run at the beginning and end of `up` and `down` calls. They'll receive an object containing a `context` property:

- `beforeAll` - Before any of the migrations are run.
- `afterAll` - After all the migrations have been executed. Note: this will always run, even if migrations throw an error.
- `beforeCommand` - Before any command (`'up' | 'down' | 'executed' | 'pending'`) is run.
- `afterCommand` - After any command (`'up' | 'down' | 'executed' | 'pending'`) is run. Note: this will always run, even if the command throws an error.

The [`FileLocker` class](./src/file-locker.ts) uses `beforeAll` and `afterAll` to implement a simple filesystem-based locking mechanism.

Expand Down
6 changes: 3 additions & 3 deletions src/file-locker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ export class FileLocker {
locker.attachTo(umzug);
}

/** Attach `beforeAll` and `afterAll` events to an umzug instance */
/** Attach lock handlers to `beforeCommand` and `afterCommand` events on an umzug instance */
attachTo(umzug: Umzug): void {
umzug.on('beforeAll', async () => this.getLock());
umzug.on('afterAll', async () => this.releaseLock());
umzug.on('beforeCommand', async () => this.getLock());
umzug.on('afterCommand', async () => this.releaseLock());
}

private async readFile(filepath: string): Promise<string | undefined> {
Expand Down
10 changes: 5 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ export type Promisable<T> = T | PromiseLike<T>;
export type LogFn = (message: Record<string, unknown>) => void;

/** Constructor options for the Umzug class */
export interface UmzugOptions<Ctx = unknown> {
export interface UmzugOptions<Ctx extends {} = Record<string, unknown>> {
/** The migrations that the Umzug instance should perform */
migrations: InputMigrations<Ctx>;
/** A logging function. Pass `console` to use stdout, or pass in your own logger. Pass `undefined` explicitly to disable logging. */
logger: Record<'info' | 'warn' | 'error' | 'debug', LogFn> | undefined;
/** The storage implementation. By default, `JSONStorage` will be used */
storage?: UmzugStorage<Ctx>;
/** An optional context object, which will be passed to each migration function, if defined */
context?: Ctx;
context?: Ctx | (() => Ctx);
/** Options for file creation */
create?: {
/**
Expand Down Expand Up @@ -83,7 +83,7 @@ export type GlobInputMigrations<T> = {
export type InputMigrations<T> =
| GlobInputMigrations<T>
| Array<RunnableMigration<T>>
| ((context: T) => Promisable<Array<RunnableMigration<T>>>);
| ((context: T) => Promisable<InputMigrations<T>>);

/** A function which takes a migration name, path and context, and returns an object with `up` and `down` functions. */
export type Resolver<T> = (params: MigrationParams<T>) => RunnableMigration<T>;
Expand Down Expand Up @@ -144,6 +144,6 @@ export interface UmzugEvents<Ctx> {
migrated: MigrationParams<Ctx>;
reverting: MigrationParams<Ctx>;
reverted: MigrationParams<Ctx>;
beforeAll: { context: Ctx };
afterAll: { context: Ctx };
beforeCommand: { command: string; context: Ctx };
afterCommand: { command: string; context: Ctx };
}
Loading

0 comments on commit 4d2d757

Please sign in to comment.