Skip to content

Commit e90ea10

Browse files
committed
db [nfc]: Simplify downgrades by using that target is latest
As explained in the parent commit, and asserted just above the _dropAndCreateAll call site, this fact always holds. That lets us simplify how downgrades work, and as a bonus cut one step from the checklist for making schema updates. This reverts some of the changes made in 601936d.
1 parent 8ed6c5b commit e90ea10

File tree

1 file changed

+3
-28
lines changed

1 file changed

+3
-28
lines changed

lib/model/database.dart

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,6 @@ class UriConverter extends TypeConverter<Uri, String> {
6868
@override Uri fromSql(String fromDb) => Uri.parse(fromDb);
6969
}
7070

71-
// TODO(drift): generate this
72-
VersionedSchema _getSchema({
73-
required DatabaseConnectionUser database,
74-
required int schemaVersion,
75-
}) {
76-
switch (schemaVersion) {
77-
case 2:
78-
return Schema2(database: database);
79-
case 3:
80-
return Schema3(database: database);
81-
case 4:
82-
return Schema4(database: database);
83-
case 5:
84-
return Schema5(database: database);
85-
default:
86-
throw Exception('unknown schema version: $schemaVersion');
87-
}
88-
}
89-
9071
@DriftDatabase(tables: [GlobalSettings, Accounts])
9172
class AppDatabase extends _$AppDatabase {
9273
AppDatabase(super.e);
@@ -98,17 +79,14 @@ class AppDatabase extends _$AppDatabase {
9879
// and generate database code with build_runner.
9980
// See ../../README.md#generated-files for more
10081
// information on using the build_runner.
101-
// * Update [_getSchema] to handle the new latestSchemaVersion.
10282
// * Write a migration in `_migrationSteps` below.
10383
// * Write tests.
10484
static const int latestSchemaVersion = 5; // See note.
10585

10686
@override
10787
int get schemaVersion => latestSchemaVersion;
10888

109-
static Future<void> _dropAndCreateAll(Migrator m, {
110-
required int schemaVersion,
111-
}) async {
89+
static Future<void> _dropAndCreateAll(Migrator m) async {
11290
await m.database.transaction(() async {
11391
final query = m.database.customSelect(
11492
"SELECT name FROM sqlite_master WHERE type='table'");
@@ -124,10 +102,7 @@ class AppDatabase extends _$AppDatabase {
124102
// that should be affected by user data.
125103
await m.database.customStatement('DROP TABLE $tableName');
126104
}
127-
final schema = _getSchema(database: m.database, schemaVersion: schemaVersion);
128-
for (final entity in schema.entities) {
129-
await m.create(entity);
130-
}
105+
await m.createAll();
131106
});
132107
}
133108

@@ -181,7 +156,7 @@ class AppDatabase extends _$AppDatabase {
181156
// in migration tests; we can forego that for testing downgrades.
182157
assert(to == latestSchemaVersion);
183158

184-
await _dropAndCreateAll(m, schemaVersion: to);
159+
await _dropAndCreateAll(m);
185160
return;
186161
}
187162
assert(1 <= from && from <= to && to <= latestSchemaVersion);

0 commit comments

Comments
 (0)