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:
parent
e7ec75d9e1
commit
198aa545eb
2 changed files with 15 additions and 0 deletions
5
.changeset/spotty-singers-shake.md
Normal file
5
.changeset/spotty-singers-shake.md
Normal 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.
|
||||||
|
|
@ -171,6 +171,12 @@ export const createMysqlStorage = ({ table = defaultTable, connection }: MysqlSt
|
||||||
async initializeStorage() {
|
async initializeStorage() {
|
||||||
const pool = getPool(connection);
|
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');
|
await pool.query('SELECT 1');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
@ -268,6 +274,10 @@ 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);
|
||||||
|
|
||||||
|
// @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 {
|
try {
|
||||||
await conn.query(contents);
|
await conn.query(contents);
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue