Skip to content
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
5 changes: 5 additions & 0 deletions packages/sqlite_async/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.14.4

- Native: Add the `NativeSqliteOpenFactory.beforeOpen` method, which can be overridden to configure
SQLite asynchronously before opening databases.

## 0.14.3

- Include identifier of mutexes when a navigator lock attempt is aborted.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ final class NativeSqliteDatabaseImpl extends SqliteDatabaseImpl {

static Future<SqliteConnectionPool> _openNativePool(
NativeSqliteOpenFactory openFactory,
) {
) async {
await openFactory.beforeOpen();

// We want to open pools asynchronously since running pragma statements as
// part of openFactory.open might do IO. openAsync spawn a temporary isolate
// for that.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:sqlite3/sqlite3.dart' as sqlite;

import '../common/abstract_open_factory.dart';
Expand All @@ -9,6 +11,17 @@ import '../common/abstract_open_factory.dart';
base class NativeSqliteOpenFactory extends InternalOpenFactory {
NativeSqliteOpenFactory({required super.path, super.sqliteOptions});

/// A (potentially asynchronous) hook to invoke before opening databases for
/// a pool.
///
/// This does nothing by default, but can be overridden to apply global
/// SQLite configuration options before databases are opened, e.g. to set
/// [sqlite.CommonSqlite3.tempDirectory].
///
/// This method is invoked in the main isolate, not the background isolate
/// responsible for opening connections.
FutureOr<void> beforeOpen() {}

@override
List<String> pragmaStatements(SqliteOpenOptions options) {
List<String> statements = [];
Expand Down
20 changes: 20 additions & 0 deletions packages/sqlite_async/test/native/basic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,15 @@ void main() {
),
);
});

test('invokes beforeOpen callback on factories', () async {
final factoy = _BeforeSetupHook(path: path);
final db = SqliteDatabase.withFactory(factoy);
expect(factoy.didCallBeforeOpen, isFalse);
await db.initialize();

expect(factoy.didCallBeforeOpen, isTrue);
});
});
}

Expand All @@ -381,3 +390,14 @@ final class _InvalidPragmaOnOpenFactory extends NativeSqliteOpenFactory {
];
}
}

final class _BeforeSetupHook extends NativeSqliteOpenFactory {
var didCallBeforeOpen = false;

_BeforeSetupHook({required super.path});

@override
void beforeOpen() {
didCallBeforeOpen = true;
}
}