From fc480876da101fbc2ba7abe35ec4858984ff1867 Mon Sep 17 00:00:00 2001 From: Stanislav Khromov Date: Sun, 5 Feb 2023 19:11:41 +0100 Subject: [PATCH] Allow users to override initial migration --- .nvmrc | 1 + README.md | 13 +++++++++++++ ava.config.cjs | 1 + src/files-loader.ts | 9 ++++++++- 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 .nvmrc diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 0000000..3cacc0b --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +12 \ No newline at end of file diff --git a/README.md b/README.md index 5247883..8b2ad5a 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/ava.config.cjs b/ava.config.cjs index 07d463e..84139f6 100644 --- a/ava.config.cjs +++ b/ava.config.cjs @@ -1,4 +1,5 @@ module.exports = { extensions: ["ts"], require: ["ts-node/register"], + timeout: '120s' } diff --git a/src/files-loader.ts b/src/files-loader.ts index 013dd2f..474fb47 100644 --- a/src/files-loader.ts +++ b/src/files-loader.ts @@ -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), )