fix(mysql): only unreference connections in a Bun environment as it has problems with Node for some reason

This commit is contained in:
Joakim Carlstein 2024-02-12 13:30:08 +01:00 committed by Joakim Carlstein
parent 98e3ed5c1b
commit 17feb2d2c2
4 changed files with 26 additions and 9 deletions

View file

@ -0,0 +1,5 @@
---
'@emigrate/mysql': patch
---
Only unreference connections in a Bun environment as it crashes Node for some reason, without even throwing an error that is

View file

@ -43,7 +43,9 @@
"mysql2": "3.6.5" "mysql2": "3.6.5"
}, },
"devDependencies": { "devDependencies": {
"@emigrate/tsconfig": "workspace:*" "@emigrate/tsconfig": "workspace:*",
"@types/bun": "1.0.5",
"bun-types": "1.0.26"
}, },
"volta": { "volta": {
"extends": "../../package.json" "extends": "../../package.json"

View file

@ -171,11 +171,13 @@ export const createMysqlStorage = ({ table = defaultTable, connection }: MysqlSt
async initializeStorage() { async initializeStorage() {
const pool = getPool(connection); const pool = getPool(connection);
if (process.isBun) {
pool.on('connection', (connection) => { pool.on('connection', (connection) => {
// @ts-expect-error stream is not in the types but it's there // @ts-expect-error stream is not in the types but it's there
// eslint-disable-next-line @typescript-eslint/no-unsafe-call // eslint-disable-next-line @typescript-eslint/no-unsafe-call
connection.stream.unref(); connection.stream.unref();
}); });
}
await pool.query('SELECT 1'); await pool.query('SELECT 1');
@ -274,9 +276,11 @@ export const createMysqlLoader = ({ connection }: MysqlLoaderOptions): LoaderPlu
const contents = await fs.readFile(migration.filePath, 'utf8'); const contents = await fs.readFile(migration.filePath, 'utf8');
const conn = await getConnection(connection); const conn = await getConnection(connection);
if (process.isBun) {
// @ts-expect-error the connection is not in the types but it's there // @ts-expect-error the connection is not in the types but it's there
// eslint-disable-next-line @typescript-eslint/no-unsafe-call // eslint-disable-next-line @typescript-eslint/no-unsafe-call
conn.connection.stream.unref(); conn.connection.stream.unref();
}
try { try {
await conn.query(contents); await conn.query(contents);

6
pnpm-lock.yaml generated
View file

@ -130,6 +130,12 @@ importers:
'@emigrate/tsconfig': '@emigrate/tsconfig':
specifier: workspace:* specifier: workspace:*
version: link:../tsconfig version: link:../tsconfig
'@types/bun':
specifier: 1.0.5
version: 1.0.5
bun-types:
specifier: 1.0.26
version: 1.0.26
packages/plugin-generate-js: packages/plugin-generate-js:
dependencies: dependencies: