v3.0.0
Major release with some breaking changes since v2.x, see migration guide here: https://github.com/sequelize/umzug#upgrading-from-v2x
Several new features, including a new built-in CLI, typescript support, templating, improved events, logging and error messages, and more.
Find usage examples under https://github.com/sequelize/umzug/tree/master/examples
Migration guide at time of writing copied here for covenience:
Upgrading from v2.x
The Umzug class should be imported as a named import, i.e. import { Umzug } from 'umzug'
.
The MigrationMeta
type, which is returned by umzug.executed()
and umzug.pending()
, no longer has a file
property - it has a name
and optional path
- since migrations are not necessarily bound to files on the file system.
The migrations.glob
parameter replaces path
, pattern
and traverseDirectories
. It can be used, in combination with cwd
and ignore
to do much more flexible file lookups. See https://npmjs.com/package/glob for more information on the syntax.
The migrations.resolve
parameter replaces customResolver
. Explicit support for wrap
and nameFormatter
has been removed - these can be easily implemented in a resolve
function.
The constructor option logging
is replaced by logger
to allow for warn
and error
messages in future. NodeJS's global console
object can be passed to this. To disable logging, replace logging: false
with logger: undefined
.
Breaking change to storages: remove string parameter (#429) b6414ba
- Custom storage implementations must update
logMigration(name) { ... }
tologMigration({ name }) { ...}
. Likewise withunlogMigration
. This is to allow receivingcontext
andpath
properties in the same arg object.
Note that this may break external storage implementations too. To adapt, you can just modify or extend thelogMigration
andunlogMigration
implementations (something likelogMigration: ({ name }) => oldStorage.logMigration(name)
).
Events have moved from the default nodejs EventEmitter
to emittery. It has better design for async code, a less bloated API surface and strong types. But, it doesn't allow passing multiple arguments to callbacks, so listeners have to change slightly, as well as .addListener(...)
and .removeListener(...)
no longer being supported (.on(...)
and .off(...)
should now be used):
Before:
umzug.on('migrating', (name, m) => console.log({ name, path: m.path }))
After:
umzug.on('migrating', ev => console.log({ name: ev.name, path: ev.path }))
The Umzug#execute
method is removed. Use Umzug#up
or Umzug#down
.
The options for Umguz#up
and Umzug#down
have changed:
umzug.up({ to: 'some-name' })
andumzug.down({ to: 'some-name' })
are still valid.umzug.up({ from: '...' })
andumzug.down({ from: '...' })
are no longer supported. To run migrations out-of-order (which is not generally recommended), you can explicitly useumzug.up({ migrations: ['...'] })
andumzug.down({ migrations: ['...'] })
.- name matches must be exact.
umzug.up({ to: 'some-n' })
will no longer match a migration calledsome-name
. umzug.down({ to: 0 })
is still valid butumzug.up({ to: 0 })
is not.umzug.up({ migrations: ['m1', 'm2'] })
is still valid but the shorthandumzug.up(['m1', 'm2'])
has been removed.umzug.down({ migrations: ['m1', 'm2'] })
is still valid but the shorthandumzug.down(['m1', 'm2'])
has been removed.umzug.up({ migrations: ['m1', 'already-run'] })
will throw an error, ifalready-run
is not found in the list of pending migrations.umzug.down({ migrations: ['m1', 'has-not-been-run'] })
will throw an error, ifhas-not-been-run
is not found in the list of executed migrations.umzug.up({ migrations: ['m1', 'm2'], rerun: 'ALLOW' })
will re-apply migrationsm1
andm2
even if they've already been run.umzug.up({ migrations: ['m1', 'm2'], rerun: 'SKIP' })
will skip migrationsm1
andm2
if they've already been run.umzug.down({ migrations: ['m1', 'm2'], rerun: 'ALLOW' })
will "revert" migrationsm1
andm2
even if they've never been run.umzug.down({ migrations: ['m1', 'm2'], rerun: 'SKIP' })
will skip reverting migrationsm1
andm2
if they haven't been run or are already reverted.umzug.up({ migrations: ['m1', 'does-not-exist', 'm2'] })
will throw an error if the migration name is not found. Note that the error will be thrown and no migrations run unless all migration names are found - whether or notrerun: 'ALLOW'
is added.
The context
parameter replaces params
, and is passed in as a property to migration functions as an options object, alongs side name
and path
. This means the signature for migrations, which in v2 was (context) => Promise<void>
, has changed slightly in v3, to ({ name, path, context }) => Promise<void>
.
Handling existing v2-format migrations
The resolve
function can also be used to upgrade your umzug version to v3 when you have existing v2-compatible migrations:
const { Umzug } = require('umzug');
const umzug = new Umzug({
migrations: {
glob: 'migrations/umzug-v2-format/*.js',
resolve: ({name, path, context}) => {
// Adjust the migration from the new signature to the v2 signature, making easier to upgrade to v3
const migration = require(path)
return { name, up: async () => migration.up(context), down: async () => migration.down(context) }
}
},
context: sequelize.getQueryInterface(),
logger: console,
});
Similarly, you no longer need migrationSorting
, you can instantiate a new Umzug
instance to manipulate migration lists directly:
const { Umzug } = require('umzug');
const parent = new Umzug({
migrations: { glob: 'migrations/**/*.js' },
context: sequelize.getQueryInterface(),
})
const umzug = new Umzug({
...parent.options,
migrations: ctx => (await parent.migrations()).sort((a, b) => b.path.localeCompare(a.path))
})
👇 full, generated changelog
What's Changed
- feat: add format function by @jaulz in #196
- Super refactor by @papb in #206
- Fix typo by @rockers7414 in #207
- TypeScript rewrite by @papb in #209
- chore(lint): prettier by @mmkal in #213
- chore: add v3 notice to readme by @mmkal in #214
- Support custom sorting function by @rockers7414 in #208
- refactor: jest by @mmkal in #215
- chore: editorconfig by @mmkal in #218
- test: convert storage tests to typescript by @mmkal in #217
- chore: use localeCompare for string comparison by @mmkal in #219
- feat: memory storage by @mmkal in #220
- test: port legacy-tests to jest by @mmkal in #221
- fix: make types allow { to: 0 } by @mmkal in #223
- fix: workaround sequelize types in tests by @mmkal in #226
- test: events by @mmkal in #225
- test: code coverage by @mmkal in #229
- fix: to: undefined shouldn't be like to: 0 by @mmkal in #231
- fix: sequelize latest by @mmkal in #232
- Configure Renovate by @renovate in #234
- Update README.md by @luwol03 in #321
- feat: v3 api by @mmkal in #325
- feat: allow skipping re-runs by @mmkal in #342
- docs: add example for multiple glob dirs by @mmkal in #343
- fix(deps): update dependency fs-jetpack to v3 by @renovate in #260
- fix: keep extension in migration name by @mmkal in #354
- chore(renovate): group dev dependencies by @mmkal in #356
- docs: beta vs stable package install instructions by @mmkal in #350
- fix: import sequelize as a type by @mmkal in #349
- Pass name, path, context to up/down functions by @mmkal in #355
- fix(deps): update dependency fs-jetpack to v4 by @renovate in #365
- chore: turn on typescript strictNullChecks by @mmkal in #368
- feat: return migration meta from up/down by @mmkal in #367
- fix: remove sequelize type dependency completely by @mmkal in #370
- Completed v2 migration snippet by @MichielDeMey in #380
- Separate out glob input type by @mmkal in #385
- fix: require ts optimistically by @mmkal in #388
- Support
step
in up and down options by @mmkal in #386 - Log json-able objects instead of strings by @mmkal in #393
- Typed async events by @mmkal in #394
- Add beforeAll/afterAll events + file locking by @mmkal in #397
- Pass context to storage methods by @mmkal in #398
- Command-line interface by @mmkal in #389
- Add examples folder by @mmkal in #411
- Move types into their own file by @mmkal in #413
- Add bundling example by @mmkal in #415
- Use verror to wrap migration errors by @mmkal in #416
- Create context per run by @mmkal in #419
- fix(deps): update dependency type-fest to ~0.20.0 by @renovate in #399
- fix(deps): update dependency fs-jetpack to ~4.1.0 by @renovate in #396
- Switch prod deps to caret by @mmkal in #421
- fix(deps): update dependency emittery to ^0.8.0 by @renovate in #428
- Add prod tsconfig for lib output by @mmkal in #430
- Breaking change (to storages): remove string parameter by @mmkal in #429
- Support tsconfigs with esModuleInterop by @mmkal in #438
- Fix create migration command by @rediska1114 in #449
- chore(deps-dev): bump lodash from 4.17.20 to 4.17.21 by @dependabot in #457
- fix(deps): update dependency type-fest to ^0.21.0 by @renovate in #443
- Run CI on pull request by @mmkal in #465
- Add ability to retrieve context asynchronously before the migrations run by @alefi in #453
- fix(deps): update dependency type-fest to v1 by @renovate in #463
- Add context to migrator._types by @adrienduchemin in #464
- docs: fix code example by @rouanw in #489
- fix #491. Fix MongoStorage
unlogMigration
by @husa in #492 - Add documentation for configuring Umzug migration parameters by @trentprynn in #493
- Update umzug.mjs by @cellulosa in #509
- Drop support for node <12 by @mmkal in #511
- Bump sqlite3 version to avoid node-gyp error by @mmkal in #516
- fix(deps): update dependency emittery to ^0.10.0 by @renovate in #481
- fix(deps): update dependency type-fest to v2 by @renovate in #517
- Remove
.extend(...)
in favour of constructor by @mmkal in #523 - [renovate] separate lint dependencies by @mmkal in #526
- Sequelize v6 by @mmkal in #527
- avoid sqlite3 vuln by @mmkal in #529
- Get readme ready for v3 release by @mmkal in #530
New Contributors
- @jaulz made their first contribution in #196
- @papb made their first contribution in #206
- @rockers7414 made their first contribution in #207
- @renovate made their first contribution in #234
- @luwol03 made their first contribution in #321
- @MichielDeMey made their first contribution in #380
- @dependabot made their first contribution in #417
- @rediska1114 made their first contribution in #449
- @alefi made their first contribution in #453
- @adrienduchemin made their first contribution in #464
- @rouanw made their first contribution in #489
- @husa made their first contribution in #492
- @trentprynn made their first contribution in #493
- @cellulosa made their first contribution in #509
Full Changelog: v2.3.0...v3.0.0