1- import 'dart:math' ;
2-
31import 'package:drift/drift.dart' ;
42import 'package:drift_dev/api/migrations_common.dart' ;
53import 'package:sqlite3/common.dart' ;
@@ -79,15 +77,14 @@ Expando<List<Input>> expectedSchema = Expando();
7977abstract 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
0 commit comments