fix(mysql): unreference all connections so that the process can exit cleanly

In a NodeJS environment it will just work as before, but in a Bun environment it will make the "forced exit" error message disappear and remove the 10 s waiting period when migrations are done.
This commit is contained in:
Joakim Carlstein 2024-02-09 13:10:02 +01:00 committed by Joakim Carlstein
parent e7ec75d9e1
commit 198aa545eb
2 changed files with 15 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'@emigrate/mysql': patch
---
Unreference all connections automatically so that they don't hinder the process from exiting. This is especially needed in Bun environments as it seems to handle sockets differently regarding this matter than NodeJS.

View file

@ -171,6 +171,12 @@ export const createMysqlStorage = ({ table = defaultTable, connection }: MysqlSt
async initializeStorage() {
const pool = getPool(connection);
pool.on('connection', (connection) => {
// @ts-expect-error stream is not in the types but it's there
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
connection.stream.unref();
});
await pool.query('SELECT 1');
try {
@ -268,6 +274,10 @@ export const createMysqlLoader = ({ connection }: MysqlLoaderOptions): LoaderPlu
const contents = await fs.readFile(migration.filePath, 'utf8');
const conn = await getConnection(connection);
// @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 {