mirror of https://github.com/AMT-Cheif/drift.git
Add setup param to schema verifier
This commit is contained in:
parent
82123e5cc0
commit
37f120d287
|
@ -2,6 +2,9 @@
|
|||
|
||||
- Fix drift using the wrong import alias in generated part files.
|
||||
- Add the `use_sql_column_name_as_json_key` builder option.
|
||||
- Add a `setup` parameter to `SchemaVerifier`. It is called when the verifier
|
||||
creates database connections (similar to the callback on `NativeDatabase`)
|
||||
and can be used to register custom functions.
|
||||
|
||||
## 2.16.0
|
||||
|
||||
|
|
|
@ -11,8 +11,18 @@ export 'package:drift_dev/src/services/schema/verifier_common.dart'
|
|||
show SchemaMismatch;
|
||||
|
||||
abstract class SchemaVerifier {
|
||||
factory SchemaVerifier(SchemaInstantiationHelper helper) =
|
||||
VerifierImplementation;
|
||||
/// Creates a schema verifier for the drift-generated [helper].
|
||||
///
|
||||
/// See [tests] for more information.
|
||||
/// The optional [setup] parameter is used internally by the verifier for
|
||||
/// every database connection it opens. This can be used to, for instance,
|
||||
/// register custom functions expected by your database.
|
||||
///
|
||||
/// [tests]: https://drift.simonbinder.eu/docs/migrations/tests/
|
||||
factory SchemaVerifier(
|
||||
SchemaInstantiationHelper helper, {
|
||||
void Function(Database raw)? setup,
|
||||
}) = VerifierImplementation;
|
||||
|
||||
/// Creates a [DatabaseConnection] that contains empty tables created for the
|
||||
/// known schema [version].
|
||||
|
|
|
@ -13,8 +13,9 @@ Expando<List<Input>> expectedSchema = Expando();
|
|||
class VerifierImplementation implements SchemaVerifier {
|
||||
final SchemaInstantiationHelper helper;
|
||||
final Random _random = Random();
|
||||
final void Function(Database)? setup;
|
||||
|
||||
VerifierImplementation(this.helper);
|
||||
VerifierImplementation(this.helper, {this.setup});
|
||||
|
||||
@override
|
||||
Future<void> migrateAndValidate(GeneratedDatabase db, int expectedVersion,
|
||||
|
@ -57,14 +58,20 @@ class VerifierImplementation implements SchemaVerifier {
|
|||
return buffer.toString();
|
||||
}
|
||||
|
||||
Database _setupDatabase(String uri) {
|
||||
final database = sqlite3.open(uri, uri: true);
|
||||
setup?.call(database);
|
||||
return database;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<InitializedSchema> schemaAt(int version) async {
|
||||
// Use distinct executors for setup and use, allowing us to close the helper
|
||||
// db here and avoid creating it twice.
|
||||
// https://www.sqlite.org/inmemorydb.html#sharedmemdb
|
||||
final uri = 'file:mem${_randomString()}?mode=memory&cache=shared';
|
||||
final dbForSetup = sqlite3.open(uri, uri: true);
|
||||
final dbForUse = sqlite3.open(uri, uri: true);
|
||||
final dbForSetup = _setupDatabase(uri);
|
||||
final dbForUse = _setupDatabase(uri);
|
||||
|
||||
final executor = NativeDatabase.opened(dbForSetup);
|
||||
final db = helper.databaseForVersion(executor, version);
|
||||
|
@ -74,7 +81,7 @@ class VerifierImplementation implements SchemaVerifier {
|
|||
await db.close();
|
||||
|
||||
return InitializedSchema(dbForUse, () {
|
||||
final db = sqlite3.open(uri, uri: true);
|
||||
final db = _setupDatabase(uri);
|
||||
return DatabaseConnection(NativeDatabase.opened(db));
|
||||
});
|
||||
}
|
||||
|
|
|
@ -5,7 +5,11 @@ import 'package:drift_dev/src/services/schema/verifier_impl.dart';
|
|||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
final verifier = SchemaVerifier(_TestHelper());
|
||||
final verifier = SchemaVerifier(
|
||||
_TestHelper(),
|
||||
setup: (rawDb) => rawDb.createFunction(
|
||||
functionName: 'test_function', function: (args) => 1),
|
||||
);
|
||||
|
||||
group('startAt', () {
|
||||
test('starts at the requested version', () async {
|
||||
|
@ -15,6 +19,12 @@ void main() {
|
|||
expect(details.hadUpgrade, isFalse, reason: 'no upgrade expected');
|
||||
}));
|
||||
});
|
||||
|
||||
test('registers custom functions', () async {
|
||||
final db = (await verifier.startAt(17)).executor;
|
||||
await db.ensureOpen(_DelegatedUser(17, (_, details) async {}));
|
||||
await db.runSelect('select test_function()', []);
|
||||
});
|
||||
});
|
||||
|
||||
group('migrateAndValidate', () {
|
||||
|
|
Loading…
Reference in New Issue