Skip to content

Commit c0eb64f

Browse files
committed
fix: handle MySQL mirror connection failure gracefully in v1 prepareDb
V1's prepareDb() crashed with ECONNREFUSED when the MySQL mirror sidecar wasn't ready, causing the entire CADT process to exit. V2's prepareV2Db() already handled this gracefully with a try-catch. This aligns V1 with V2's behavior: log the error and continue with the main SQLite database. The mirror will reconnect on the next sync cycle via safeMirrorDbHandler. Also adds the missing connection.end() call in a finally block to prevent connection leaks on any failure path.
1 parent 5be9063 commit c0eb64f

1 file changed

Lines changed: 20 additions & 11 deletions

File tree

src/database/index.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,29 @@ export const prepareDb = async () => {
165165
getConfig().MIRROR_DB.DB_HOST &&
166166
getConfig().MIRROR_DB.DB_HOST !== ''
167167
) {
168-
const connection = await mysql.createConnection({
169-
host: getConfig().MIRROR_DB.DB_HOST,
170-
port: 3306,
171-
user: getConfig().MIRROR_DB.DB_USERNAME,
172-
password: getConfig().MIRROR_DB.DB_PASSWORD,
173-
});
168+
try {
169+
const connection = await mysql.createConnection({
170+
host: getConfig().MIRROR_DB.DB_HOST,
171+
port: 3306,
172+
user: getConfig().MIRROR_DB.DB_USERNAME,
173+
password: getConfig().MIRROR_DB.DB_PASSWORD,
174+
});
174175

175-
await connection.query(
176-
`CREATE DATABASE IF NOT EXISTS \`${getConfig().MIRROR_DB.DB_NAME}\`;`,
177-
);
176+
try {
177+
await connection.query(
178+
`CREATE DATABASE IF NOT EXISTS \`${getConfig().MIRROR_DB.DB_NAME}\`;`,
179+
);
180+
} finally {
181+
await connection.end();
182+
}
178183

179-
const db = new Sequelize(config[mirrorConfig]);
184+
const db = new Sequelize(config[mirrorConfig]);
180185

181-
await checkForMigrations(db);
186+
await checkForMigrations(db);
187+
} catch (error) {
188+
// Non-fatal: mirror DB failure should not block main database startup
189+
logger.error('[v1]: Error setting up MySQL mirror database:', error);
190+
}
182191
} else if (mirrorConfig == 'mirrorTest') {
183192
await checkForMigrations(sequelizeMirror);
184193
}

0 commit comments

Comments
 (0)