diff --git a/drift/CHANGELOG.md b/drift/CHANGELOG.md index 4f04897f..0dde301c 100644 --- a/drift/CHANGELOG.md +++ b/drift/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.8.3-dev + +- Forbid `schemaVersion` returning `0`, as this causes issues in the migrator. + ## 2.8.2 - Fix prepared statements leaking when the statement cache is disabled. diff --git a/drift/lib/src/runtime/api/db_base.dart b/drift/lib/src/runtime/api/db_base.dart index 8c2327b0..5d223f02 100644 --- a/drift/lib/src/runtime/api/db_base.dart +++ b/drift/lib/src/runtime/api/db_base.dart @@ -20,6 +20,9 @@ abstract class GeneratedDatabase extends DatabaseConnectionUser /// Specify the schema version of your database. Whenever you change or add /// tables, you should bump this field and provide a [migration] strategy. + /// + /// The [schemaVersion] must be positive. Typically, one starts with a value + /// of `1` and increments the value for each modification to the schema. @override int get schemaVersion; @@ -110,6 +113,14 @@ abstract class GeneratedDatabase extends DatabaseConnectionUser @nonVirtual Future beforeOpen(QueryExecutor executor, OpeningDetails details) { return _runConnectionZoned(BeforeOpenRunner(this, executor), () async { + if (schemaVersion <= 0) { + throw StateError( + 'The schemaVersion of your database must be positive. \n' + "A value of zero can't be distinguished from an uninitialized " + 'database, which causes issues in the migrator', + ); + } + if (details.wasCreated) { final migrator = createMigrator(); await _resolvedMigration.onCreate(migrator); diff --git a/drift/test/database/database_test.dart b/drift/test/database/database_test.dart index 0dbca6dc..c5c18a44 100644 --- a/drift/test/database/database_test.dart +++ b/drift/test/database/database_test.dart @@ -110,4 +110,15 @@ void main() { verify(ex2.runSelect('SELECT 1', [])); verifyNever(ex2.runSelect(any, any)); }); + + test('disallows zero as a schema version', () async { + var db = TodoDb(MockExecutor(OpeningDetails(null, 0)))..schemaVersion = 0; + await expectLater(db.customSelect('SELECT 1').get(), throwsStateError); + + db = TodoDb(MockExecutor(OpeningDetails(null, 0)))..schemaVersion = -1; + await expectLater(db.customSelect('SELECT 1').get(), throwsStateError); + + db = TodoDb(MockExecutor(OpeningDetails(null, 0)))..schemaVersion = 1; + await expectLater(db.customSelect('SELECT 1').get(), completes); + }); }