Skip to content

Commit f036308

Browse files
committed
db: Fix downgrades so they match creating from scratch
Fixes #1427.
1 parent e4edead commit f036308

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

lib/model/database.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,16 @@ class AppDatabase extends _$AppDatabase {
135135
},
136136
);
137137

138+
Future<void> _createLatestSchema(Migrator m) async {
139+
await m.createAll();
140+
// Corresponds to `from4to5` above.
141+
await into(globalSettings).insert(GlobalSettingsCompanion());
142+
}
143+
138144
@override
139145
MigrationStrategy get migration {
140146
return MigrationStrategy(
141-
onCreate: (Migrator m) async {
142-
await m.createAll();
143-
// Corresponds to `from4to5` above.
144-
await into(globalSettings).insert(GlobalSettingsCompanion());
145-
},
147+
onCreate: _createLatestSchema,
146148
onUpgrade: (Migrator m, int from, int to) async {
147149
if (from > to) {
148150
// This should only ever happen in dev. As a dev convenience,
@@ -157,7 +159,7 @@ class AppDatabase extends _$AppDatabase {
157159
assert(to == latestSchemaVersion);
158160

159161
await _dropAll(m);
160-
await m.createAll();
162+
await _createLatestSchema(m);
161163
return;
162164
}
163165
assert(1 <= from && from <= to && to <= latestSchemaVersion);

test/model/database_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ void main() {
147147
// does not have the extra tables and columns.
148148
final after = AppDatabase(schema.newConnection());
149149
await verifier.migrateAndValidate(after, toVersion, validateDropped: true);
150+
// Check that a custom migration/setup step of ours got run too.
151+
check(await after.getGlobalSettings()).themeSetting.isNull();
150152
await after.close();
151153
});
152154

0 commit comments

Comments
 (0)