fix(electron): enable WAL mode for sqlite (#8336)

fix AF-1015
This commit is contained in:
pengx17
2024-09-25 04:06:03 +00:00
parent e6feb17ac7
commit e839947dd5
6 changed files with 36 additions and 3 deletions

View File

@@ -141,6 +141,18 @@ export class SQLiteAdapter {
} }
} }
async checkpoint() {
try {
if (!this.db) {
logger.warn(`${this.path} is not connected`);
return;
}
await this.db.checkpoint();
} catch (error) {
logger.error('checkpoint', error);
}
}
async getUpdatesCount(docId?: string) { async getUpdatesCount(docId?: string) {
try { try {
if (!this.db) { if (!this.db) {

View File

@@ -120,6 +120,10 @@ export class WorkspaceSQLiteDB {
} }
return null; return null;
}; };
async checkpoint() {
await this.adapter.checkpoint();
}
} }
export async function openWorkspaceDatabase( export async function openWorkspaceDatabase(

View File

@@ -77,6 +77,7 @@ export async function saveDBFileAs(
): Promise<SaveDBFileResult> { ): Promise<SaveDBFileResult> {
try { try {
const db = await ensureSQLiteDB('workspace', workspaceId); const db = await ensureSQLiteDB('workspace', workspaceId);
await db.checkpoint(); // make sure all changes (WAL) are written to db
const fakedResult = getFakedResult(); const fakedResult = getFakedResult();
const ret = const ret =

View File

@@ -98,8 +98,6 @@ export const registerUpdater = async () => {
channel: buildType, channel: buildType,
}); });
logger.debug('auto-updater feed config', feedUrl);
autoUpdater.setFeedURL(feedUrl); autoUpdater.setFeedURL(feedUrl);
// register events for checkForUpdates // register events for checkForUpdates

View File

@@ -30,6 +30,11 @@ export declare class SqliteConnection {
get isClose(): boolean get isClose(): boolean
static validate(path: string): Promise<ValidationResult> static validate(path: string): Promise<ValidationResult>
migrateAddDocId(): Promise<void> migrateAddDocId(): Promise<void>
/**
* Flush the WAL file to the database file.
* See https://www.sqlite.org/pragma.html#pragma_wal_checkpoint:~:text=PRAGMA%20schema.wal_checkpoint%3B
*/
checkpoint(): Promise<void>
} }
export interface BlobRow { export interface BlobRow {

View File

@@ -53,7 +53,7 @@ impl SqliteConnection {
let sqlite_options = SqliteConnectOptions::new() let sqlite_options = SqliteConnectOptions::new()
.filename(&path) .filename(&path)
.foreign_keys(false) .foreign_keys(false)
.journal_mode(sqlx::sqlite::SqliteJournalMode::Off); .journal_mode(sqlx::sqlite::SqliteJournalMode::Wal);
let pool = SqlitePoolOptions::new() let pool = SqlitePoolOptions::new()
.max_connections(4) .max_connections(4)
.connect_lazy_with(sqlite_options); .connect_lazy_with(sqlite_options);
@@ -490,6 +490,19 @@ impl SqliteConnection {
} }
} }
/**
* Flush the WAL file to the database file.
* See https://www.sqlite.org/pragma.html#pragma_wal_checkpoint:~:text=PRAGMA%20schema.wal_checkpoint%3B
*/
#[napi]
pub async fn checkpoint(&self) -> napi::Result<()> {
sqlx::query("PRAGMA wal_checkpoint(FULL);")
.execute(&self.pool)
.await
.map_err(anyhow::Error::from)?;
Ok(())
}
pub async fn migrate_add_doc_id_index(&self) -> napi::Result<()> { pub async fn migrate_add_doc_id_index(&self) -> napi::Result<()> {
// ignore errors // ignore errors
match sqlx::query("CREATE INDEX IF NOT EXISTS idx_doc_id ON updates(doc_id);") match sqlx::query("CREATE INDEX IF NOT EXISTS idx_doc_id ON updates(doc_id);")