Skip to content

Commit 2fa6781

Browse files
committed
Avoid shared cache for verifier
1 parent b2e0c03 commit 2fa6781

File tree

6 files changed

+35
-40
lines changed

6 files changed

+35
-40
lines changed

docs/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ dev_dependencies:
4949
sass_builder: ^2.2.1
5050

5151
dependency_overrides:
52+
# Due to https://github.com/dart-lang/sdk/issues/60553#issuecomment-2818927538
53+
analyzer: '>=7.3.0 <7.4.0'
5254
drift_flutter:
5355
path: ../drift_flutter
5456
drift:

drift_dev/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## 2.26.1-dev
22

33
- Fix `generatedAs` not being generated for versioned schema imports.
4+
- Avoid the use of the shared database cache in the `SchemaVerifier`
5+
implementation.
46

57
## 2.26.0
68

drift_dev/lib/api/migrations_common.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,13 @@ class InitializedSchema<DB extends CommonDatabase> {
146146
/// });
147147
/// ```
148148
DatabaseConnection newConnection() => _createConnection();
149+
150+
/// [CommonDatabase.dispose]s the underlying [rawDatabase] backing the initial
151+
/// schema.
152+
///
153+
/// Not calling this method technically leaks resources, but [rawDatabase] is
154+
/// an in-memory database that also has finalizers closing it when it's not
155+
/// used anymore. Further, unit tests are typically short-lived processes, so
156+
/// forgetting to call [close] does not have terrible side-effects.
157+
void close() => rawDatabase.dispose();
149158
}

drift_dev/lib/src/services/schema/verifier_common.dart

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:math';
2-
31
import 'package:drift/drift.dart';
42
import 'package:drift_dev/api/migrations_common.dart';
53
import 'package:sqlite3/common.dart';
@@ -79,15 +77,14 @@ Expando<List<Input>> expectedSchema = Expando();
7977
abstract base class VerifierImplementation<DB extends CommonDatabase>
8078
implements SchemaVerifier<DB> {
8179
final SchemaInstantiationHelper helper;
82-
final Random _random = Random();
8380

8481
final void Function(DB)? setup;
8582

8683
VerifierImplementation(this.helper, {this.setup});
8784

88-
DB newInMemoryDatabase(String uri);
85+
DB newInMemoryDatabase();
8986

90-
QueryExecutor wrapOpened(DB db);
87+
QueryExecutor wrapOpened(DB db, {required bool closeUnderlyingOnClose});
9188

9289
@override
9390
Future<void> migrateAndValidate(GeneratedDatabase db, int expectedVersion,
@@ -116,22 +113,8 @@ abstract base class VerifierImplementation<DB extends CommonDatabase>
116113
verify(referenceSchema, actualSchema, validateDropped);
117114
}
118115

119-
String _randomString() {
120-
const charCodeLowerA = 97;
121-
const charCodeLowerZ = 122;
122-
const length = 16;
123-
124-
final buffer = StringBuffer();
125-
for (var i = 0; i < length; i++) {
126-
buffer.writeCharCode(
127-
_random.nextInt(charCodeLowerZ - charCodeLowerA) + charCodeLowerA);
128-
}
129-
130-
return buffer.toString();
131-
}
132-
133-
DB _setupDatabase(String uri) {
134-
final database = newInMemoryDatabase(uri);
116+
DB _setupDatabase() {
117+
final database = newInMemoryDatabase();
135118
try {
136119
database.config.doubleQuotedStringLiterals = false;
137120
} on SqliteException {
@@ -148,23 +131,18 @@ abstract base class VerifierImplementation<DB extends CommonDatabase>
148131

149132
@override
150133
Future<InitializedSchema<DB>> schemaAt(int version) async {
151-
// Use distinct executors for setup and use, allowing us to close the helper
152-
// db here and avoid creating it twice.
153-
// https://www.sqlite.org/inmemorydb.html#sharedmemdb
154-
final uri = 'file:mem${_randomString()}?mode=memory&cache=shared';
155-
final dbForSetup = _setupDatabase(uri);
156-
final dbForUse = _setupDatabase(uri);
157-
158-
final executor = wrapOpened(dbForSetup);
134+
final rawDb = _setupDatabase();
135+
136+
final executor = wrapOpened(rawDb, closeUnderlyingOnClose: false);
159137
final db = helper.databaseForVersion(executor, version);
160138

161139
// Opening the helper database will instantiate the schema for us
162140
await executor.ensureOpen(db);
163141
await db.close();
164142

165-
return InitializedSchema(dbForUse, () {
166-
final db = _setupDatabase(uri);
167-
return DatabaseConnection(wrapOpened(db));
143+
return InitializedSchema(rawDb, () {
144+
return DatabaseConnection(
145+
wrapOpened(rawDb, closeUnderlyingOnClose: false));
168146
});
169147
}
170148

drift_dev/lib/src/services/schema/verifier_native.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ final class NativeSchemaVerifier extends VerifierImplementation<Database>
1010
NativeSchemaVerifier(super.helper, {super.setup});
1111

1212
@override
13-
Database newInMemoryDatabase(String uri) {
14-
return sqlite3.open(uri, uri: true);
13+
Database newInMemoryDatabase() {
14+
return sqlite3.openInMemory();
1515
}
1616

1717
@override
18-
QueryExecutor wrapOpened(Database db) {
19-
return NativeDatabase.opened(db);
18+
QueryExecutor wrapOpened(Database db,
19+
{required bool closeUnderlyingOnClose}) {
20+
return NativeDatabase.opened(db,
21+
closeUnderlyingOnClose: closeUnderlyingOnClose);
2022
}
2123
}

drift_dev/lib/src/services/schema/verifier_web.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ final class WebSchemaVerifier extends VerifierImplementation<CommonDatabase>
1212
WebSchemaVerifier(this.sqlite3, super.helper, {super.setup});
1313

1414
@override
15-
CommonDatabase newInMemoryDatabase(String uri) {
16-
return sqlite3.open(uri, uri: true);
15+
CommonDatabase newInMemoryDatabase() {
16+
return sqlite3.openInMemory();
1717
}
1818

1919
@override
20-
QueryExecutor wrapOpened(CommonDatabase db) {
21-
return WasmDatabase.opened(db);
20+
QueryExecutor wrapOpened(CommonDatabase db,
21+
{required bool closeUnderlyingOnClose}) {
22+
return WasmDatabase.opened(db,
23+
closeUnderlyingOnClose: closeUnderlyingOnClose);
2224
}
2325
}

0 commit comments

Comments
 (0)