Support schema downgrades (#2336)

This commit is contained in:
Simon Binder 2023-03-03 21:24:00 +01:00
parent adc2e22c46
commit 4db8f19a43
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
3 changed files with 15 additions and 2 deletions

View File

@ -5,6 +5,8 @@
statements.
- Add `rowid` parameter to companions for tables with rowids that don't have a
visible alias for the rowid.
- After opening a database with a higher schema version than the current one set
in the database class, the schema version in the database will now be downgraded.
## 2.5.0

View File

@ -458,7 +458,7 @@ class DelegatedDatabase extends _BaseExecutor {
await user.beforeOpen(_BeforeOpeningExecutor(this), openingDetails);
if (versionDelegate is DynamicVersionDelegate &&
(oldVersion == null || oldVersion < currentVersion)) {
oldVersion != currentVersion) {
// set version now, after migrations ran successfully
await versionDelegate.setSchemaVersion(currentVersion);
}

View File

@ -103,7 +103,7 @@ void main() {
verify(delegate.open(userDb));
verifyNever(delegate.runCustom(any, any));
verify(version.schemaVersion);
// Running migrations from version 3 to 3
// Not running migrations from version 3 to 3
verifyNever(version.setSchemaVersion(3));
when(version.schemaVersion).thenAnswer((_) => Future.value(2));
@ -127,6 +127,17 @@ void main() {
verify(delegate.runCustom('updated', argThat(equals([1, 3]))));
});
test('handles database downgrades', () async {
final version = MockDynamicVersionDelegate();
when(version.schemaVersion).thenAnswer((_) => Future.value(4));
when(delegate.versionDelegate).thenReturn(version);
await db.ensureOpen(userDb);
verify(delegate.open(userDb));
verify(delegate.runCustom('updated', argThat(equals([4, 3]))));
verify(version.setSchemaVersion(3));
});
});
group('transactions', () {