-
Notifications
You must be signed in to change notification settings - Fork 194
feat: Add migration loader strategies with SQL grouping and docs #1582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Shinigami92
merged 8 commits into
salsita:main
from
peturv:feature/migration-loader-strategies
Mar 20, 2026
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1acb99e
Implementing loader strategies
peturv 51fcdd6
Fixes for linting errors. Adjustment of import of RunnerOption in nod…
peturv 467b9dd
Use correct sorting strategy for filenames.
peturv 6f93c26
Use a single jiti instance.
peturv 681054b
Ensure migration file sorting is uniform.
peturv b2ae8bc
Minor fixes: Avoid exhausting file handles.
peturv 74614b7
Preflight/Linting fixes.
peturv 70a7d2d
Post-review adjustments. No functional changes.
peturv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| # Migration Loading Strategies | ||
|
|
||
| `migrationLoaderStrategies` lets you control how migration files are loaded based on file extension. | ||
|
|
||
| This is useful when you need custom loading behavior, or when you want SQL files to use the new grouped `.up.sql` / `.down.sql` strategy. | ||
|
|
||
| ## Default Behavior | ||
|
|
||
| If `migrationLoaderStrategies` is not provided, the loader uses built-in defaults: | ||
|
|
||
| - `.sql` files use the legacy SQL loader (`legacySql`) | ||
| - `.js` and `.ts` files use the default loader (`default`) | ||
| - unsupported extensions fall back to `default` | ||
|
|
||
| This keeps existing behavior intact. | ||
|
|
||
| ## Configuration Shape | ||
|
|
||
| ```ts | ||
| type MigrationLoader = (filePaths: string[]) => Promise<MigrationUnit[]>; | ||
|
|
||
| interface MigrationLoaderStrategy { | ||
| extensions: string[]; | ||
| loader: MigrationLoader | 'default' | 'legacySql' | 'sql'; | ||
| } | ||
| ``` | ||
|
|
||
| ## Example: Use Grouped SQL Loader | ||
|
|
||
| This enables grouping `*.up.sql` and `*.down.sql` into one migration unit: | ||
|
|
||
| ```ts | ||
| import { runner } from 'node-pg-migrate'; | ||
|
|
||
| await runner({ | ||
| databaseUrl: process.env.DATABASE_URL!, | ||
| dir: 'migrations', | ||
| direction: 'up', | ||
| migrationsTable: 'pgmigrations', | ||
| migrationLoaderStrategies: [{ extensions: ['.sql'], loader: 'sql' }], | ||
| }); | ||
| ``` | ||
|
|
||
| With this configuration: | ||
|
|
||
| - `001_init.up.sql` + `001_init.down.sql` are treated as one migration (`001_init`) | ||
| - The migration `id` is normalized to the equivalent `.sql` form (`001_init.up.sql` / `001_init.down.sql` -> `001_init.sql`). This means you can switch from a single `001_init.sql` migration to split `.up/.down` files (or vice versa) without creating a second entry in `migrationsTable`. | ||
| - `001_init.sql` still works as a single-file SQL migration | ||
| - mixing `001_init.sql` with `001_init.up.sql` / `001_init.down.sql` throws an error | ||
|
|
||
| ## Example: Custom Loader | ||
|
|
||
| You can provide a loader function directly: | ||
|
|
||
| ```ts | ||
| import type { MigrationLoader } from 'node-pg-migrate'; | ||
| import { runner } from 'node-pg-migrate'; | ||
|
|
||
| const customLoader: MigrationLoader = async (filePaths) => { | ||
| // map files to migration units | ||
| return []; | ||
| }; | ||
|
|
||
| await runner({ | ||
| databaseUrl: process.env.DATABASE_URL!, | ||
| dir: 'migrations', | ||
| direction: 'up', | ||
| migrationsTable: 'pgmigrations', | ||
| migrationLoaderStrategies: [ | ||
| { extensions: ['.sql'], loader: 'sql' }, | ||
| { extensions: ['.mjs'], loader: customLoader }, | ||
| ], | ||
| }); | ||
| ``` | ||
|
|
||
| ## Strategy Matching Rules | ||
|
|
||
| - Extension matching is case-insensitive | ||
| - Each strategy handles one or more extensions | ||
| - If no strategy matches an extension, the `default` loader is used | ||
|
|
||
| ## Legacy SQL migrations | ||
|
|
||
| ### Why it exists | ||
|
|
||
| The legacy SQL loader has been supported for a long time, even when it was less visible in the docs. | ||
|
|
||
| Common use cases include: | ||
|
|
||
| - onboarding an existing project by importing an initial schema dump as the first migration | ||
| - keeping specific advanced migrations as pure SQL when that is cleaner than a builder-based migration | ||
|
|
||
| So if your team already relies on plain `.sql` files, that workflow is still supported. | ||
|
|
||
| ### Markers and default fallback | ||
|
|
||
| The classic SQL template uses marker comments: | ||
|
|
||
| ```sql | ||
| -- Up Migration | ||
|
|
||
| -- Down Migration | ||
| ``` | ||
|
|
||
| Behavior for a single `.sql` file: | ||
|
|
||
| - when both markers are present, `up` and `down` sections are extracted | ||
| - when no markers are present, the full file is treated as an `up` migration | ||
| - if there is no `down` section, there is no actionable `down` migration |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.