Skip to content

Commit af11b82

Browse files
committed
feat: make WAL and busy_timeout configurable for drift connections
1 parent 56ba7b5 commit af11b82

3 files changed

Lines changed: 27 additions & 10 deletions

File tree

packages/data/app_database/lib/src/connection/native.dart

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import 'package:drift/drift.dart';
44
import 'package:drift/native.dart';
55
import 'package:path/path.dart' show join;
66

7-
DatabaseConnection createAppDatabaseConnection(String? path, String name, {bool logStatements = false}) {
7+
DatabaseConnection createAppDatabaseConnection(
8+
String? path,
9+
String name, {
10+
bool logStatements = false,
11+
bool isWalEnabled = true,
12+
int? busyTimeoutMilliseconds = 5000,
13+
}) {
814
return DatabaseConnection.delayed(
915
Future.sync(() async {
1016
final databasePath = join(path ?? '', name);
@@ -15,15 +21,14 @@ DatabaseConnection createAppDatabaseConnection(String? path, String name, {bool
1521
setup: (database) {
1622
// Enables Write-Ahead Logging (WAL) mode.
1723
// In default mode (DELETE journal), readers block writers and writers block readers.
18-
// WAL allows simultaneous readers and writers, which is crucial for
19-
// concurrent access from the UI isolate and Background isolate.
20-
database.execute('PRAGMA journal_mode=WAL;');
24+
if (isWalEnabled) {
25+
database.execute('PRAGMA journal_mode=WAL;');
26+
}
2127

2228
// Sets a timeout (in milliseconds) for waiting when the database is locked.
23-
// By default, SQLite throws an error immediately if the DB is busy (SQLITE_BUSY).
24-
// Setting a timeout allows the driver to automatically retry the operation
25-
// for up to 5 seconds before failing, handling short-lived locks gracefully.
26-
database.execute('PRAGMA busy_timeout=5000;');
29+
if (busyTimeoutMilliseconds != null) {
30+
database.execute('PRAGMA busy_timeout=$busyTimeoutMilliseconds;');
31+
}
2732
},
2833
);
2934

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import 'package:drift/drift.dart';
22

3-
DatabaseConnection createAppDatabaseConnection(String? path, String name, {bool logStatements = false}) {
3+
DatabaseConnection createAppDatabaseConnection(
4+
String? path,
5+
String name, {
6+
bool logStatements = false,
7+
bool isWalEnabled = true,
8+
int? busyTimeoutMilliseconds = 5000,
9+
}) {
410
throw UnsupportedError('No suitable database connection implementation was found on this platform.');
511
}

packages/data/app_database/lib/src/connection/web.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import 'package:drift/drift.dart';
22
import 'package:drift/wasm.dart';
33

4-
DatabaseConnection createAppDatabaseConnection(String? path, String name, {bool logStatements = false}) {
4+
DatabaseConnection createAppDatabaseConnection(
5+
String? path,
6+
String name, {
7+
bool logStatements = false,
8+
bool isWalEnabled = true,
9+
int? busyTimeoutMilliseconds = 5000,
10+
}) {
511
assert(path == null || path.isEmpty, 'path is not supported on web');
612

713
return DatabaseConnection.delayed(

0 commit comments

Comments
 (0)