Skip to content

feat: Add optional context to migrations - #3

Merged
sandrinodm merged 4 commits into
sandrinodm:mainfrom
FoxxMD:context
May 16, 2026
Merged

feat: Add optional context to migrations#3
sandrinodm merged 4 commits into
sandrinodm:mainfrom
FoxxMD:context

Conversation

@FoxxMD

@FoxxMD FoxxMD commented May 13, 2026

Copy link
Copy Markdown
Contributor
  • Implements an optional, typed second argument to up/down that is passed through apply/rollback
  • Allows users to provide their own context for migration code
  • Update docs to provide context example

Closes #2

Should remain backwards compatible. Here is the added docs example that provides an example of how this would be used:


A second argument can be provided to migration file functions in order to supply your own context. Context should be passed in apply or rollback.

To enforce type correctness, type the Migrator and use satisfies in your migration files:

import type { SqliteDatabase, Migration } from 'sqlite-up';

interface MyContext = {
  foo: string
}

export const up: Migration<MyContext>['up'] = async (db: SqliteDatabase, ctx: MyContext): Promise<void> => {
  // Migration code here
  // context is passed as ctx
};

export const down: Migration<MyContext>['down'] = async (db: SqliteDatabase, ctx: MyContext): Promise<void> => {
  // Rollback code here
  // context is passed as ctx
};

Later, when using the migrator...

import { DatabaseSync } from 'node:sqlite';
import { Migrator } from 'sqlite-up';

async function main() {
  const db = new DatabaseSync('myapp.db');

  const migrator = new Migrator<MyContext>({
    db,
    migrationsDir: './migrations',
  });

  const result = await migrator.apply({foo: 'bar'});
}

main().catch(console.error);

FoxxMD added 3 commits May 13, 2026 14:23
* Implements an optional, typed second argument to up/down that is passed through apply/rollback
* Allows users to provide their own context for migration code
* Update docs to provide context example

Closes sandrinodm#2
FoxxMD added a commit to FoxxMD/multi-scrobbler that referenced this pull request May 13, 2026
@sandrinodm

Copy link
Copy Markdown
Owner

Very useful, thanks @FoxxMD!

@sandrinodm
sandrinodm merged commit 136a907 into sandrinodm:main May 16, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Providing full DatabaseSync object

2 participants