Skip to content

Commit a1c2dcf

Browse files
committed
db [nfc]: Treat schema version as a static const, not instance method
Conceptually this is really a static fact about the class, not a fact about a given instance of it. (The implementation's body is always just a constant integer.) Make things a bit clearer -- and make it easier to get hold of the value from outside the class -- by making that explicit, with a static const field as the home of the value.
1 parent f644501 commit a1c2dcf

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

lib/model/database.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,19 @@ class AppDatabase extends _$AppDatabase {
9292
AppDatabase(super.e);
9393

9494
// When updating the schema:
95-
// * Make the change in the table classes, and bump schemaVersion.
95+
// * Make the change in the table classes, and bump latestSchemaVersion.
9696
// * Export the new schema and generate test migrations with drift:
9797
// $ tools/check --fix drift
9898
// and generate database code with build_runner.
9999
// See ../../README.md#generated-files for more
100100
// information on using the build_runner.
101-
// * Update [_getSchema] to handle the new schemaVersion.
101+
// * Update [_getSchema] to handle the new latestSchemaVersion.
102102
// * Write a migration in `_migrationSteps` below.
103103
// * Write tests.
104+
static const int latestSchemaVersion = 5; // See note.
105+
104106
@override
105-
int get schemaVersion => 5; // See note.
107+
int get schemaVersion => latestSchemaVersion;
106108

107109
static Future<void> _dropAndCreateAll(Migrator m, {
108110
required int schemaVersion,
@@ -175,7 +177,7 @@ class AppDatabase extends _$AppDatabase {
175177
await _dropAndCreateAll(m, schemaVersion: to);
176178
return;
177179
}
178-
assert(1 <= from && from <= to && to <= schemaVersion);
180+
assert(1 <= from && from <= to && to <= latestSchemaVersion);
179181

180182
await m.runMigrationSteps(from: from, to: to, steps: _migrationSteps);
181183
});

0 commit comments

Comments
 (0)