From 9158c312405a676dbfabdb219afae6d871793b63 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Tue, 26 Jul 2022 18:33:09 +0200 Subject: [PATCH] Respect db options in runtime schema validation --- drift/test/database/validate_schema_test.dart | 24 +++++++++++++++++++ drift/test/generated/custom_tables.dart | 4 ++++ drift_dev/lib/api/migrations.dart | 3 +++ 3 files changed, 31 insertions(+) create mode 100644 drift/test/database/validate_schema_test.dart diff --git a/drift/test/database/validate_schema_test.dart b/drift/test/database/validate_schema_test.dart new file mode 100644 index 00000000..fb38223b --- /dev/null +++ b/drift/test/database/validate_schema_test.dart @@ -0,0 +1,24 @@ +@TestOn('vm') +import 'package:drift/drift.dart'; +import 'package:drift_dev/api/migrations.dart'; +import 'package:test/test.dart'; + +import '../generated/custom_tables.dart'; +import '../test_utils/test_utils.dart'; + +void main() { + test('finds mismatch for datetime format', () async { + final db = CustomTablesDb.connect(testInMemoryDatabase()) + ..options = const DriftDatabaseOptions(storeDateTimeAsText: false); + await db.customSelect('SELECT 1').get(); // Open db, setup tables + + db.options = const DriftDatabaseOptions(storeDateTimeAsText: true); + // Validation should fail now because datetimes are in the wrong format. + + await expectLater( + db.validateDatabaseSchema(), + throwsA(isA().having( + (e) => e.toString(), 'toString()', contains('TEXT and INTEGER'))), + ); + }); +} diff --git a/drift/test/generated/custom_tables.dart b/drift/test/generated/custom_tables.dart index 5f3f7e27..1b58bb21 100644 --- a/drift/test/generated/custom_tables.dart +++ b/drift/test/generated/custom_tables.dart @@ -29,4 +29,8 @@ class CustomTablesDb extends _$CustomTablesDb { @override MigrationStrategy migration = MigrationStrategy(); + + @override + DriftDatabaseOptions options = + const DriftDatabaseOptions(storeDateTimeAsText: true); } diff --git a/drift_dev/lib/api/migrations.dart b/drift_dev/lib/api/migrations.dart index 8ac31bfc..e7a90a8f 100644 --- a/drift_dev/lib/api/migrations.dart +++ b/drift_dev/lib/api/migrations.dart @@ -103,6 +103,9 @@ class _GenerateFromScratch extends GeneratedDatabase { _GenerateFromScratch(this.reference, QueryExecutor executor) : super(executor); + @override + DriftDatabaseOptions get options => reference.options; + @override Iterable> get allTables => reference.allTables;