Skip to content

Allow users to override initial migration to fix bundling issues #99

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on my best guess, because the latest Node version gave me big diffs in the package-lock file that I didn't want to include in this PR.

13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,19 @@ Rather than this:
- 0002_simple_migration.sql
```

### Bundling postgres-migrations

If you are bundling this package with for example Vite/Rollup, you might get the error `ReferenceError: __dirname is not defined` when trying to run migrations.

To work around this, you need to:

1. Download the initial migration from [this link](https://github.com/ThomWright/postgres-migrations/blob/master/src/migrations/0_create-migrations-table.sql) and put it in *your* migrations folder.
2. Make sure the name of the file is exactly the same as the original: `0_create-migrations-table.sql`

The library will then detect the presence of the file and skip including the original file using `__dirname`. To make sure you copied the file correctly, verify that the `hash` for the `create-migrations-table` migration in the `migrations` table is `e18db593bcde2aca2a408c4d1100f6abba2195df`.

See [this issue](https://github.com/ThomWright/postgres-migrations/issues/79) for additional details.

## Useful resources

[Stack Overflow: How We Do Deployment - 2016 Edition (Database Migrations)](http://nickcraver.com/blog/2016/05/03/stack-overflow-how-we-do-deployment-2016-edition/#database-migrations)
Expand Down
1 change: 1 addition & 0 deletions ava.config.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
extensions: ["ts"],
require: ["ts-node/register"],
timeout: '120s'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had issues with Docker tests timing out and for some reason ava just marks the tests as OK if it times out. 🤷

}
9 changes: 8 additions & 1 deletion src/files-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,17 @@ export const loadMigrationFiles = async (
}

const migrationFiles = [
path.join(__dirname, "migrations/0_create-migrations-table.sql"),
...fileNames.map((fileName) => path.resolve(directory, fileName)),
].filter(isValidFile)

// Add the initial migration if the user hasn't added it to their migrations/ folder themselves
// This works around issues with bundling this library due to usage of __dirname
if (!fileNames.includes("0_create-migrations-table.sql")) {
migrationFiles.push(
path.join(__dirname, "migrations/0_create-migrations-table.sql"),
)
}

const unorderedMigrations = await Promise.all(
migrationFiles.map(loadMigrationFile),
)
Expand Down