Fix sequencing of database closes

Co-authored-by: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com>
This commit is contained in:
automated-signal
2024-02-26 18:57:12 -06:00
committed by GitHub
parent a24bdbbd9e
commit 9908b161db

View File

@@ -642,25 +642,18 @@ async function initialize({
}
async function close(): Promise<void> {
globalReadonlyInstance?.close();
globalReadonlyInstance = undefined;
// SQLLite documentation suggests that we run `PRAGMA optimize` right
// before closing the database connection.
globalWritableInstance?.pragma('optimize');
globalWritableInstance?.close();
globalWritableInstance = undefined;
globalReadonlyInstance?.close();
globalReadonlyInstance = undefined;
}
async function removeDB(): Promise<void> {
if (globalWritableInstance) {
try {
globalWritableInstance.close();
} catch (error) {
logger.error('removeDB: Failed to close database:', error.stack);
}
globalWritableInstance = undefined;
}
if (globalReadonlyInstance) {
try {
globalReadonlyInstance.close();
@@ -669,6 +662,14 @@ async function removeDB(): Promise<void> {
}
globalReadonlyInstance = undefined;
}
if (globalWritableInstance) {
try {
globalWritableInstance.close();
} catch (error) {
logger.error('removeDB: Failed to close database:', error.stack);
}
globalWritableInstance = undefined;
}
if (!databaseFilePath) {
throw new Error(
'removeDB: Cannot erase database without a databaseFilePath!'