Merge pull request #1915 from famedly/karthi/clear-legacy-db

fix: clear legacy db on logout
This commit is contained in:
Karthikeyan S 2024-12-11 18:24:22 +05:30 committed by GitHub
commit 168433a709
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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. /// Resets all settings and stops the synchronisation.
Future<void> clear() async { Future<void> clear() async {
Logs().outputEvents.clear(); 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 { try {
await abortSync(); await abortSync();
await database?.clear(); await database?.clear();
await legacyDatabase?.clear();
_backgroundSync = true; _backgroundSync = true;
} catch (e, s) { } catch (e, s) {
Logs().e('Unable to clear database', e, s); Logs().e('Unable to clear database', e, s);
} finally { } finally {
await database?.delete(); await database?.delete();
await legacyDatabase?.delete();
_database = null; _database = null;
} }
@ -3877,6 +3885,7 @@ class Client extends MatrixApi {
Logs().e('Unable to migrate inbound group sessions!', e, s); Logs().e('Unable to migrate inbound group sessions!', e, s);
} }
await legacyDatabase.clear();
await legacyDatabase.delete(); await legacyDatabase.delete();
_initLock = false; _initLock = false;

View File

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