Properly signal error when user id migration fails

Previous implementation was returning success status
when *any* account was migrated, despite possible
failures.

Return error when any account fails to be migrated.

The migration remines idempotent and can be re-run
again, until if finally succeeds.

Signed-off-by: Chris Narkiewicz <hello@ezaquarii.com>
This commit is contained in:
Chris Narkiewicz 2020-03-04 06:07:42 +00:00 committed by tobiasKaminsky
parent 9ef27576e1
commit 1ba288e5e6
No known key found for this signature in database
GPG key ID: 0E00D4D47D0C5AF7
3 changed files with 13 additions and 8 deletions

View file

@ -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();

View file

@ -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() {

View file

@ -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")
}
}
}