Skip to content

Commit

Permalink
fix(electron): enable WAL mode for sqlite (#8336)
Browse files Browse the repository at this point in the history
  • Loading branch information
pengx17 committed Sep 25, 2024
1 parent e6feb17 commit e839947
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 3 deletions.
12 changes: 12 additions & 0 deletions packages/frontend/apps/electron/src/helper/db/db-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
try {
if (!this.db) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ export class WorkspaceSQLiteDB {
}
return null;
};

async checkpoint() {
await this.adapter.checkpoint();
}
}

export async function openWorkspaceDatabase(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export async function saveDBFileAs(
): Promise<SaveDBFileResult> {
try {
const db = await ensureSQLiteDB('workspace', workspaceId);
await db.checkpoint(); // make sure all changes (WAL) are written to db
const fakedResult = getFakedResult();

const ret =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ export const registerUpdater = async () => {
channel: buildType,
});

logger.debug('auto-updater feed config', feedUrl);

autoUpdater.setFeedURL(feedUrl);

// register events for checkForUpdates
Expand Down
5 changes: 5 additions & 0 deletions packages/frontend/native/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export declare class SqliteConnection {
get isClose(): boolean
static validate(path: string): Promise<ValidationResult>
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 {
Expand Down
15 changes: 14 additions & 1 deletion packages/frontend/native/src/sqlite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl SqliteConnection {
let sqlite_options = SqliteConnectOptions::new()
.filename(&path)
.foreign_keys(false)
.journal_mode(sqlx::sqlite::SqliteJournalMode::Off);
.journal_mode(sqlx::sqlite::SqliteJournalMode::Wal);
let pool = SqlitePoolOptions::new()
.max_connections(4)
.connect_lazy_with(sqlite_options);
Expand Down Expand Up @@ -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<()> {
// ignore errors
match sqlx::query("CREATE INDEX IF NOT EXISTS idx_doc_id ON updates(doc_id);")
Expand Down

0 comments on commit e839947

Please sign in to comment.