fix: clear legacy db on logout properly

This commit is contained in:
Karthikeyan S 2024-11-26 16:28:02 +05:30
parent f3e249dab3
commit 9ded85e786
No known key found for this signature in database
GPG Key ID: 28BA6AEE539ECE2E
2 changed files with 26 additions and 13 deletions

View File

@ -2182,14 +2182,22 @@ class Client extends MatrixApi {
/// Resets all settings and stops the synchronisation.
Future<void> clear() async {
Logs().outputEvents.clear();
DatabaseApi? legacyDatabase;
if (legacyDatabaseBuilder != null) {
// If there was data in the legacy db, it will never let the SDK
// completely log out as we migrate data from it, everytime we `init`
legacyDatabase = await legacyDatabaseBuilder?.call(this);
}
try {
await abortSync();
await database?.clear();
await legacyDatabase?.clear();
_backgroundSync = true;
} catch (e, s) {
Logs().e('Unable to clear database', e, s);
} finally {
await database?.delete();
await legacyDatabase?.delete();
_database = null;
}
@ -3877,6 +3885,7 @@ class Client extends MatrixApi {
Logs().e('Unable to migrate inbound group sessions!', e, s);
}
await legacyDatabase.clear();
await legacyDatabase.delete();
_initLock = false;

View File

@ -1422,36 +1422,36 @@ void main() {
});
test('Database Migration', () async {
final database = await getDatabase(null);
final moorClient = Client(
final firstDatabase = await getDatabase(null);
final firstClient = Client(
'testclient',
httpClient: FakeMatrixApi(),
databaseBuilder: (_) => database,
databaseBuilder: (_) => firstDatabase,
);
FakeMatrixApi.client = moorClient;
await moorClient.checkHomeserver(
FakeMatrixApi.client = firstClient;
await firstClient.checkHomeserver(
Uri.parse('https://fakeServer.notExisting'),
checkWellKnown: false,
);
await moorClient.init(
await firstClient.init(
newToken: 'abcd',
newUserID: '@test:fakeServer.notExisting',
newHomeserver: moorClient.homeserver,
newHomeserver: firstClient.homeserver,
newDeviceName: 'Text Matrix Client',
newDeviceID: 'GHTYAJCE',
newOlmAccount: pickledOlmAccount,
);
await Future.delayed(Duration(milliseconds: 200));
await moorClient.dispose(closeDatabase: false);
await firstClient.dispose(closeDatabase: false);
final hiveClient = Client(
final newClient = Client(
'testclient',
httpClient: FakeMatrixApi(),
databaseBuilder: getDatabase,
legacyDatabaseBuilder: (_) => database,
legacyDatabaseBuilder: (_) => firstDatabase,
);
final Set<InitState> initStates = {};
await hiveClient.init(onInitStateChanged: initStates.add);
await newClient.init(onInitStateChanged: initStates.add);
expect(initStates, {
InitState.initializing,
InitState.migratingDatabase,
@ -1459,8 +1459,12 @@ void main() {
InitState.finished,
});
await Future.delayed(Duration(milliseconds: 200));
expect(hiveClient.isLogged(), true);
await hiveClient.dispose(closeDatabase: false);
expect(newClient.isLogged(), true);
await newClient.dispose(closeDatabase: false);
await firstDatabase.close();
final sameOldFirstDatabase = await getDatabase(null);
expect(await sameOldFirstDatabase.getClient('testclient'), null);
});
test('getEventByPushNotification', () async {