Skip to content

Commit

Permalink
fix(mysql): close database connections gracefully when using Bun
Browse files Browse the repository at this point in the history
  • Loading branch information
joakimbeng committed Jun 24, 2024
1 parent cf620a1 commit 67b174a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-yaks-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@emigrate/mysql': patch
---

Unreference all connections when run using Bun, to not keep the process open unnecessarily long
4 changes: 2 additions & 2 deletions packages/mysql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
},
"devDependencies": {
"@emigrate/tsconfig": "workspace:*",
"@types/bun": "1.0.5",
"bun-types": "1.0.26"
"@types/bun": "1.1.2",
"bun-types": "1.1.8"
},
"volta": {
"extends": "../../package.json"
Expand Down
40 changes: 22 additions & 18 deletions packages/mysql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,37 @@ export type MysqlLoaderOptions = {
connection: ConnectionOptions | string;
};

const getConnection = async (connection: ConnectionOptions | string) => {
if (typeof connection === 'string') {
const uri = new URL(connection);
const getConnection = async (options: ConnectionOptions | string) => {
let connection: Connection;

if (typeof options === 'string') {
const uri = new URL(options);

// client side connectTimeout is unstable in mysql2 library
// it throws an error you can't catch and crashes node
// best to leave this at 0 (disabled)
uri.searchParams.set('connectTimeout', '0');
uri.searchParams.set('multipleStatements', 'true');

return createConnection(uri.toString());
connection = await createConnection(uri.toString());
} else {
connection = await createConnection({
...options,
// client side connectTimeout is unstable in mysql2 library
// it throws an error you can't catch and crashes node
// best to leave this at 0 (disabled)
connectTimeout: 0,
multipleStatements: true,
});
}

return createConnection({
...connection,
// client side connectTimeout is unstable in mysql2 library
// it throws an error you can't catch and crashes node
// best to leave this at 0 (disabled)
connectTimeout: 0,
multipleStatements: true,
});
if (process.isBun) {
// @ts-expect-error the connection is not in the types but it's there
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
connection.connection.stream.unref();
}

return connection;
};

const getPool = (connection: PoolOptions | string) => {
Expand Down Expand Up @@ -354,12 +364,6 @@ export const createMysqlLoader = ({ connection }: MysqlLoaderOptions): LoaderPlu
const contents = await fs.readFile(migration.filePath, 'utf8');
const conn = await getConnection(connection);

if (process.isBun) {
// @ts-expect-error the connection is not in the types but it's there
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
conn.connection.stream.unref();
}

try {
await conn.query(contents);
} finally {
Expand Down
27 changes: 23 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 67b174a

Please sign in to comment.