diff --git a/src/main/java/com/nextcloud/client/account/UserAccountManager.java b/src/main/java/com/nextcloud/client/account/UserAccountManager.java index 1281ec50f5..2c3de3779a 100644 --- a/src/main/java/com/nextcloud/client/account/UserAccountManager.java +++ b/src/main/java/com/nextcloud/client/account/UserAccountManager.java @@ -89,9 +89,11 @@ public interface UserAccountManager extends CurrentAccountProvider { boolean exists(Account account); /** - * Verifies that every account has userId set + * Verifies that every account has userId set and sets the user id if not. + * This migration is idempotent and can be run multiple times until + * all accounts are migrated. * - * @return true if any account was migrated, false if no account was migrated + * @return true if migration was successful, false if any account failed to be migrated */ boolean migrateUserId(); diff --git a/src/main/java/com/nextcloud/client/account/UserAccountManagerImpl.java b/src/main/java/com/nextcloud/client/account/UserAccountManagerImpl.java index 49d695a2f7..4784b94412 100644 --- a/src/main/java/com/nextcloud/client/account/UserAccountManagerImpl.java +++ b/src/main/java/com/nextcloud/client/account/UserAccountManagerImpl.java @@ -342,12 +342,11 @@ public class UserAccountManagerImpl implements UserAccountManager { } public boolean migrateUserId() { - boolean success = false; Account[] ocAccounts = accountManager.getAccountsByType(MainApp.getAccountType(context)); String userId; String displayName; GetUserInfoRemoteOperation remoteUserNameOperation = new GetUserInfoRemoteOperation(); - + int failed = 0; for (Account account : ocAccounts) { String storedUserId = accountManager.getUserData(account, com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_USER_ID); @@ -370,10 +369,12 @@ public class UserAccountManagerImpl implements UserAccountManager { } else { // skip account, try it next time Log_OC.e(TAG, "Error while getting username for account: " + account.name); + failed++; continue; } } catch (Exception e) { Log_OC.e(TAG, "Error while getting username: " + e.getMessage()); + failed++; continue; } @@ -383,11 +384,9 @@ public class UserAccountManagerImpl implements UserAccountManager { accountManager.setUserData(account, com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_USER_ID, userId); - - success = true; } - return success; + return failed == 0; } private String getAccountType() { diff --git a/src/main/java/com/nextcloud/client/migrations/Migrations.kt b/src/main/java/com/nextcloud/client/migrations/Migrations.kt index dce6eac4fc..366e09fcb3 100644 --- a/src/main/java/com/nextcloud/client/migrations/Migrations.kt +++ b/src/main/java/com/nextcloud/client/migrations/Migrations.kt @@ -21,6 +21,7 @@ package com.nextcloud.client.migrations import com.nextcloud.client.account.UserAccountManager import javax.inject.Inject +import kotlin.IllegalStateException /** * This class collects all migration steps and provides API to supply those @@ -50,6 +51,9 @@ class Migrations @Inject constructor( ).sortedBy { it.id } fun migrateUserId() { - userAccountManager.migrateUserId() + val allAccountsHaveUserId = userAccountManager.migrateUserId() + if (!allAccountsHaveUserId) { + throw IllegalStateException("Failed to set user id for all accounts") + } } }