Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading