Separate calls in sql channel

This commit is contained in:
Fedor Indutny 2024-08-12 12:54:24 -07:00 committed by GitHub
parent 85b130a12d
commit 9a98ae0a4e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 90 additions and 45 deletions

View file

@ -350,7 +350,6 @@ export const DataReader: ServerReadableInterface = {
export const DataWriter: ServerWritableInterface = {
close: closeWritable,
removeDB,
removeIndexedDBFiles,
createOrUpdateIdentityKey,
@ -629,20 +628,29 @@ function openAndMigrateDatabase(
// If that fails, we try to open the database with 3.x compatibility to extract the
// user_version (previously stored in schema_version, blown away by cipher_migrate).
db = new SQL(filePath) as WritableDB;
keyDatabase(db, key);
try {
keyDatabase(db, key);
// https://www.zetetic.net/blog/2018/11/30/sqlcipher-400-release/#compatability-sqlcipher-4-0-0
db.pragma('cipher_compatibility = 3');
migrateSchemaVersion(db);
db.close();
// https://www.zetetic.net/blog/2018/11/30/sqlcipher-400-release/#compatability-sqlcipher-4-0-0
db.pragma('cipher_compatibility = 3');
migrateSchemaVersion(db);
db.close();
// After migrating user_version -> schema_version, we reopen database, because we can't
// migrate to the latest ciphers after we've modified the defaults.
db = new SQL(filePath) as WritableDB;
keyDatabase(db, key);
// After migrating user_version -> schema_version, we reopen database, because
// we can't migrate to the latest ciphers after we've modified the defaults.
db = new SQL(filePath) as WritableDB;
keyDatabase(db, key);
db.pragma('cipher_migrate');
switchToWAL(db);
db.pragma('cipher_migrate');
switchToWAL(db);
} catch (error) {
try {
db.close();
} catch {
// Best effort
}
throw error;
}
return db;
}
@ -659,8 +667,17 @@ function openAndSetUpSQLCipher(
const db = openAndMigrateDatabase(filePath, key, readonly);
// Because foreign key support is not enabled by default!
db.pragma('foreign_keys = ON');
try {
// Because foreign key support is not enabled by default!
db.pragma('foreign_keys = ON');
} catch (error) {
try {
db.close();
} catch {
// Best effort
}
throw error;
}
return db;
}
@ -750,13 +767,7 @@ function closeWritable(db: WritableDB): void {
db.close();
}
function removeDB(db: WritableDB): void {
try {
db.close();
} catch (error) {
logger.error('removeDB: Failed to close database:', error.stack);
}
export function removeDB(): void {
if (!databaseFilePath) {
throw new Error(
'removeDB: Cannot erase database without a databaseFilePath!'