Skip to content

Commit 8ed6c5b

Browse files
committed
db [nfc]: Assert downgrades are always to current version
And adjust a test to make that true in tests too.
1 parent a1c2dcf commit 8ed6c5b

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lib/model/database.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,13 @@ class AppDatabase extends _$AppDatabase {
174174
// drop everything from the database and start over.
175175
// TODO(log): log schema downgrade as an error
176176
assert(debugLog('Downgrading schema from v$from to v$to.'));
177+
178+
// In the actual app, the target schema version is always
179+
// the latest version as of the code that's being run.
180+
// Migrating to earlier versions is useful only for isolating steps
181+
// in migration tests; we can forego that for testing downgrades.
182+
assert(to == latestSchemaVersion);
183+
177184
await _dropAndCreateAll(m, schemaVersion: to);
178185
return;
179186
}

test/model/database_test.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,25 +127,26 @@ void main() {
127127
});
128128

129129
test('downgrading', () async {
130-
final schema = await verifier.schemaAt(2);
130+
final toVersion = AppDatabase.latestSchemaVersion;
131+
final schema = await verifier.schemaAt(toVersion);
131132

132133
// This simulates the scenario during development when running the app
133134
// with a future schema version that has additional tables and columns.
134135
final before = AppDatabase(schema.newConnection());
135136
await before.customStatement('CREATE TABLE test_extra (num int)');
136137
await before.customStatement('ALTER TABLE accounts ADD extra_column int');
137138
await check(verifier.migrateAndValidate(
138-
before, 2, validateDropped: true)).throws<SchemaMismatch>();
139+
before, toVersion, validateDropped: true)).throws<SchemaMismatch>();
139140
// Override the schema version by modifying the underlying value
140141
// drift internally keeps track of in the database.
141142
// TODO(drift): Expose a better interface for testing this.
142-
await before.customStatement('PRAGMA user_version = 999;');
143+
await before.customStatement('PRAGMA user_version = ${toVersion + 1};');
143144
await before.close();
144145

145146
// Simulate starting up the app, with an older schema version that
146147
// does not have the extra tables and columns.
147148
final after = AppDatabase(schema.newConnection());
148-
await verifier.migrateAndValidate(after, 2, validateDropped: true);
149+
await verifier.migrateAndValidate(after, toVersion, validateDropped: true);
149150
await after.close();
150151
});
151152

0 commit comments

Comments
 (0)