Remove AccountAccess.anyAccounts and instead check if user branches exist

With change I81491a253 it is ensured that we have a user branch for each
account. Hence to check if any account exists we can check if any user
branch exists.

This is a preparation step for migration the accounts from ReviewDb to
NoteDb.

Change-Id: Ib8117125a29e6b5f954e70e58e0c8b25e51c585c
This commit is contained in:
Edwin Kempin 2017-04-12 16:12:39 +02:00
parent 5a1d6a882e
commit 5a48534cb0
5 changed files with 77 additions and 6 deletions

View File

@ -21,6 +21,7 @@ import com.google.gerrit.pgm.init.api.InitFlags;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.GerritPersonIdentProvider;
import com.google.gerrit.server.account.Accounts;
import com.google.gerrit.server.account.AccountsUpdate;
import com.google.gerrit.server.config.SitePaths;
import com.google.gwtorm.server.OrmException;
@ -60,6 +61,17 @@ public class AccountsOnInit {
}
}
public boolean hasAnyAccount() throws IOException {
File path = getPath();
if (path == null) {
return false;
}
try (Repository repo = new FileRepository(path)) {
return Accounts.hasAnyAccount(repo);
}
}
private File getPath() {
Path basePath = site.resolve(flags.cfg.getString("gerrit", null, "basePath"));
checkArgument(basePath != null, "gerrit.basePath must be configured");

View File

@ -88,7 +88,7 @@ public class InitAdminUser implements InitStep {
}
try (ReviewDb db = dbFactory.open()) {
if (db.accounts().anyAccounts().toList().isEmpty()) {
if (!accounts.hasAnyAccount()) {
ui.header("Gerrit Administrator");
if (ui.yesno(true, "Create administrator user")) {
Account.Id id = new Account.Id(db.nextAccountId());

View File

@ -41,9 +41,6 @@ public interface AccountAccess extends Access<Account, Account.Id> {
ResultSet<Account> suggestByPreferredEmail(String nameA, String nameB, int limit)
throws OrmException;
@Query("LIMIT 1")
ResultSet<Account> anyAccounts() throws OrmException;
@Query("ORDER BY accountId LIMIT ?")
ResultSet<Account> firstNById(int n) throws OrmException;

View File

@ -52,6 +52,7 @@ public class AccountManager {
private static final Logger log = LoggerFactory.getLogger(AccountManager.class);
private final SchemaFactory<ReviewDb> schema;
private final Accounts accounts;
private final AccountsUpdate.Server accountsUpdateFactory;
private final AccountCache byIdCache;
private final AccountByEmailCache byEmailCache;
@ -68,6 +69,7 @@ public class AccountManager {
@Inject
AccountManager(
SchemaFactory<ReviewDb> schema,
Accounts accounts,
AccountsUpdate.Server accountsUpdateFactory,
AccountCache byIdCache,
AccountByEmailCache byEmailCache,
@ -80,6 +82,7 @@ public class AccountManager {
ExternalIds externalIds,
ExternalIdsUpdate.Server externalIdsUpdateFactory) {
this.schema = schema;
this.accounts = accounts;
this.accountsUpdateFactory = accountsUpdateFactory;
this.byIdCache = byIdCache;
this.byEmailCache = byEmailCache;
@ -226,8 +229,7 @@ public class AccountManager {
account.setFullName(who.getDisplayName());
account.setPreferredEmail(extId.email());
boolean isFirstAccount =
awaitsFirstAccountCheck.getAndSet(false) && db.accounts().anyAccounts().toList().isEmpty();
boolean isFirstAccount = awaitsFirstAccountCheck.getAndSet(false) && !accounts.hasAnyAccount();
try {
AccountsUpdate accountsUpdate = accountsUpdateFactory.create();

View File

@ -0,0 +1,60 @@
// Copyright (C) 2017 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.account;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.server.config.AllUsersName;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.io.IOException;
import java.util.Objects;
import org.eclipse.jgit.lib.Repository;
/** Class to access accounts. */
@Singleton
public class Accounts {
private final GitRepositoryManager repoManager;
private final AllUsersName allUsersName;
@Inject
Accounts(GitRepositoryManager repoManager, AllUsersName allUsersName) {
this.repoManager = repoManager;
this.allUsersName = allUsersName;
}
/**
* Checks if any account exists.
*
* @return {@code true} if at least one account exists, otherwise {@code false}
*/
public boolean hasAnyAccount() throws IOException {
try (Repository repo = repoManager.openRepository(allUsersName)) {
return hasAnyAccount(repo);
}
}
public static boolean hasAnyAccount(Repository repo) throws IOException {
return repo.getRefDatabase()
.getRefs(RefNames.REFS_USERS)
.values()
.stream()
.map(r -> Account.Id.fromRef(r.getName()))
.filter(Objects::nonNull)
.findAny()
.isPresent();
}
}