Migrate starred changes to git (part 2)
This is the second part of migrating the starred changes from database to git. This change: * bumps the database schema version * migrates the starred changes from database to git (for single instance Gerrit servers) * deletes the database table * deletes the user.readStarredChangesFromGit config option Change-Id: I534e98dc60050c5e409765c96def359503529035 Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
@@ -49,10 +49,6 @@ import java.sql.Timestamp;
|
||||
* the internal SSH daemon. One record per SSH key uploaded by the user, keys
|
||||
* are checked in random order until a match is found.</li>
|
||||
*
|
||||
* <li>{@link StarredChange}: user has starred the change, tracking
|
||||
* notifications of updates on that change, or just book-marking it for faster
|
||||
* future reference. One record per starred change.</li>
|
||||
*
|
||||
* <li>{@link DiffPreferencesInfo}: user's preferences for rendering side-to-side
|
||||
* and unified diff</li>
|
||||
*
|
||||
|
@@ -1,77 +0,0 @@
|
||||
// Copyright (C) 2008 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.reviewdb.client;
|
||||
|
||||
import com.google.gwtorm.client.Column;
|
||||
import com.google.gwtorm.client.CompoundKey;
|
||||
|
||||
/** A {@link Change} starred by an {@link Account}. */
|
||||
public class StarredChange {
|
||||
public static class Key extends CompoundKey<Account.Id> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Column(id = 1)
|
||||
protected Account.Id accountId;
|
||||
|
||||
@Column(id = 2)
|
||||
protected Change.Id changeId;
|
||||
|
||||
protected Key() {
|
||||
accountId = new Account.Id();
|
||||
changeId = new Change.Id();
|
||||
}
|
||||
|
||||
public Key(final Account.Id a, final Change.Id g) {
|
||||
accountId = a;
|
||||
changeId = g;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Account.Id getParentKey() {
|
||||
return accountId;
|
||||
}
|
||||
|
||||
public Change.Id getChangeId() {
|
||||
return changeId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public com.google.gwtorm.client.Key<?>[] members() {
|
||||
return new com.google.gwtorm.client.Key<?>[] {changeId};
|
||||
}
|
||||
}
|
||||
|
||||
@Column(id = 1, name = Column.NONE)
|
||||
protected Key key;
|
||||
|
||||
protected StarredChange() {
|
||||
}
|
||||
|
||||
public StarredChange(final StarredChange.Key k) {
|
||||
key = k;
|
||||
}
|
||||
|
||||
public StarredChange.Key getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public Account.Id getAccountId() {
|
||||
return key.accountId;
|
||||
}
|
||||
|
||||
public Change.Id getChangeId() {
|
||||
return key.changeId;
|
||||
}
|
||||
}
|
@@ -68,10 +68,9 @@ public interface ReviewDb extends Schema {
|
||||
@Relation(id = 13)
|
||||
AccountGroupMemberAuditAccess accountGroupMembersAudit();
|
||||
|
||||
//Deleted @Relation(id = 17)
|
||||
// Deleted @Relation(id = 17)
|
||||
|
||||
@Relation(id = 18)
|
||||
StarredChangeAccess starredChanges();
|
||||
// Deleted @Relation(id = 18)
|
||||
|
||||
@Relation(id = 19)
|
||||
AccountProjectWatchAccess accountProjectWatches();
|
||||
|
@@ -113,11 +113,6 @@ public class ReviewDbWrapper implements ReviewDb {
|
||||
return delegate.accountGroupMembersAudit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public StarredChangeAccess starredChanges() {
|
||||
return delegate.starredChanges();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccountProjectWatchAccess accountProjectWatches() {
|
||||
return delegate.accountProjectWatches();
|
||||
|
@@ -1,37 +0,0 @@
|
||||
// Copyright (C) 2008 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.reviewdb.server;
|
||||
|
||||
import com.google.gerrit.reviewdb.client.Account;
|
||||
import com.google.gerrit.reviewdb.client.Change;
|
||||
import com.google.gerrit.reviewdb.client.StarredChange;
|
||||
import com.google.gwtorm.server.Access;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
import com.google.gwtorm.server.PrimaryKey;
|
||||
import com.google.gwtorm.server.Query;
|
||||
import com.google.gwtorm.server.ResultSet;
|
||||
|
||||
public interface StarredChangeAccess extends
|
||||
Access<StarredChange, StarredChange.Key> {
|
||||
@Override
|
||||
@PrimaryKey("key")
|
||||
StarredChange get(StarredChange.Key key) throws OrmException;
|
||||
|
||||
@Query("WHERE key.accountId = ?")
|
||||
ResultSet<StarredChange> byAccount(Account.Id id) throws OrmException;
|
||||
|
||||
@Query("WHERE key.changeId = ?")
|
||||
ResultSet<StarredChange> byChange(Change.Id id) throws OrmException;
|
||||
}
|
@@ -86,10 +86,3 @@ ON patch_comments (status, author_id);
|
||||
-- PatchSetAccess
|
||||
CREATE INDEX patch_sets_byRevision
|
||||
ON patch_sets (revision);
|
||||
|
||||
-- *********************************************************************
|
||||
-- StarredChangeAccess
|
||||
-- @PrimaryKey covers: byAccount
|
||||
|
||||
CREATE INDEX starred_changes_byChange
|
||||
ON starred_changes (change_id);
|
||||
|
@@ -95,11 +95,3 @@ ON patch_comments (status, author_id)
|
||||
CREATE INDEX patch_sets_byRevision
|
||||
ON patch_sets (revision)
|
||||
#
|
||||
|
||||
-- *********************************************************************
|
||||
-- StarredChangeAccess
|
||||
-- @PrimaryKey covers: byAccount
|
||||
|
||||
CREATE INDEX starred_changes_byChange
|
||||
ON starred_changes (change_id)
|
||||
#
|
||||
|
@@ -135,11 +135,3 @@ WHERE status = 'd';
|
||||
-- PatchSetAccess
|
||||
CREATE INDEX patch_sets_byRevision
|
||||
ON patch_sets (revision);
|
||||
|
||||
-- *********************************************************************
|
||||
-- StarredChangeAccess
|
||||
-- @PrimaryKey covers: byAccount
|
||||
|
||||
CREATE INDEX starred_changes_byChange
|
||||
ON starred_changes (change_id);
|
||||
|
||||
|
@@ -24,16 +24,12 @@ import com.google.common.collect.FluentIterable;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.ImmutableSortedSet;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Iterators;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.gerrit.reviewdb.client.Account;
|
||||
import com.google.gerrit.reviewdb.client.Change;
|
||||
import com.google.gerrit.reviewdb.client.Project;
|
||||
import com.google.gerrit.reviewdb.client.RefNames;
|
||||
import com.google.gerrit.reviewdb.client.StarredChange;
|
||||
import com.google.gerrit.reviewdb.server.ReviewDb;
|
||||
import com.google.gerrit.server.config.AllUsersName;
|
||||
import com.google.gerrit.server.config.GerritServerConfig;
|
||||
import com.google.gerrit.server.git.GitRepositoryManager;
|
||||
import com.google.gerrit.server.index.change.ChangeField;
|
||||
import com.google.gerrit.server.index.change.ChangeIndexer;
|
||||
@@ -48,7 +44,6 @@ import com.google.inject.Provider;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
import org.eclipse.jgit.lib.BatchRefUpdate;
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
import org.eclipse.jgit.lib.Constants;
|
||||
import org.eclipse.jgit.lib.NullProgressMonitor;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
@@ -67,7 +62,6 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
@@ -79,6 +73,8 @@ public class StarredChangesUtil {
|
||||
LoggerFactory.getLogger(StarredChangesUtil.class);
|
||||
|
||||
private static final String DEFAULT_LABEL = "star";
|
||||
public static final ImmutableSortedSet<String> DEFAULT_LABELS =
|
||||
ImmutableSortedSet.of(DEFAULT_LABEL);
|
||||
|
||||
private final GitRepositoryManager repoManager;
|
||||
private final AllUsersName allUsers;
|
||||
@@ -86,7 +82,6 @@ public class StarredChangesUtil {
|
||||
private final PersonIdent serverIdent;
|
||||
private final ChangeIndexer indexer;
|
||||
private final Provider<InternalChangeQuery> queryProvider;
|
||||
private final boolean readFromGit;
|
||||
|
||||
@Inject
|
||||
StarredChangesUtil(GitRepositoryManager repoManager,
|
||||
@@ -94,24 +89,17 @@ public class StarredChangesUtil {
|
||||
Provider<ReviewDb> dbProvider,
|
||||
@GerritPersonIdent PersonIdent serverIdent,
|
||||
ChangeIndexer indexer,
|
||||
Provider<InternalChangeQuery> queryProvider,
|
||||
@GerritServerConfig Config cfg) {
|
||||
Provider<InternalChangeQuery> queryProvider) {
|
||||
this.repoManager = repoManager;
|
||||
this.allUsers = allUsers;
|
||||
this.dbProvider = dbProvider;
|
||||
this.serverIdent = serverIdent;
|
||||
this.indexer = indexer;
|
||||
this.queryProvider = queryProvider;
|
||||
this.readFromGit =
|
||||
cfg.getBoolean("user", null, "readStarredChangesFromGit", false);
|
||||
}
|
||||
|
||||
public void star(Account.Id accountId, Project.NameKey project,
|
||||
Change.Id changeId) throws OrmException {
|
||||
dbProvider.get().starredChanges()
|
||||
.insert(Collections.singleton(new StarredChange(
|
||||
new StarredChange.Key(accountId, changeId))));
|
||||
|
||||
try (Repository repo = repoManager.openRepository(allUsers)) {
|
||||
String refName = RefNames.refsStarredChanges(changeId, accountId);
|
||||
ObjectId oldObjectId = getObjectId(repo, refName);
|
||||
@@ -128,10 +116,6 @@ public class StarredChangesUtil {
|
||||
|
||||
public void unstar(Account.Id accountId, Project.NameKey project,
|
||||
Change.Id changeId) throws OrmException {
|
||||
dbProvider.get().starredChanges()
|
||||
.delete(Collections.singleton(new StarredChange(
|
||||
new StarredChange.Key(accountId, changeId))));
|
||||
|
||||
try (Repository repo = repoManager.openRepository(allUsers);
|
||||
RevWalk rw = new RevWalk(repo)) {
|
||||
RefUpdate u = repo.updateRef(
|
||||
@@ -167,9 +151,6 @@ public class StarredChangesUtil {
|
||||
|
||||
public void unstarAll(Project.NameKey project, Change.Id changeId)
|
||||
throws OrmException, NoSuchChangeException {
|
||||
dbProvider.get().starredChanges().delete(
|
||||
dbProvider.get().starredChanges().byChange(changeId));
|
||||
|
||||
try (Repository repo = repoManager.openRepository(allUsers);
|
||||
RevWalk rw = new RevWalk(repo)) {
|
||||
BatchRefUpdate batchUpdate = repo.getRefDatabase().newBatchUpdate();
|
||||
@@ -199,16 +180,6 @@ public class StarredChangesUtil {
|
||||
|
||||
public Set<Account.Id> byChange(Change.Id changeId)
|
||||
throws OrmException {
|
||||
if (!readFromGit) {
|
||||
return FluentIterable
|
||||
.from(dbProvider.get().starredChanges().byChange(changeId))
|
||||
.transform(new Function<StarredChange, Account.Id>() {
|
||||
@Override
|
||||
public Account.Id apply(StarredChange in) {
|
||||
return in.getAccountId();
|
||||
}
|
||||
}).toSet();
|
||||
}
|
||||
return FluentIterable
|
||||
.from(getRefNames(RefNames.refsStarredChangesPrefix(changeId)))
|
||||
.transform(new Function<String, Account.Id>() {
|
||||
@@ -235,11 +206,6 @@ public class StarredChangesUtil {
|
||||
@Deprecated
|
||||
public ResultSet<Change.Id> queryFromIndex(final Account.Id accountId) {
|
||||
try {
|
||||
if (!readFromGit) {
|
||||
return new ChangeIdResultSet(
|
||||
dbProvider.get().starredChanges().byAccount(accountId));
|
||||
}
|
||||
|
||||
Set<String> fields = ImmutableSet.of(
|
||||
ChangeField.ID.getName());
|
||||
List<ChangeData> changeData =
|
||||
@@ -351,38 +317,4 @@ public class StarredChangesUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class ChangeIdResultSet implements ResultSet<Change.Id> {
|
||||
private static final Function<StarredChange, Change.Id>
|
||||
STARRED_CHANGE_TO_CHANGE_ID =
|
||||
new Function<StarredChange, Change.Id>() {
|
||||
@Override
|
||||
public Change.Id apply(StarredChange starredChange) {
|
||||
return starredChange.getChangeId();
|
||||
}
|
||||
};
|
||||
|
||||
private final ResultSet<StarredChange> starredChangesResultSet;
|
||||
|
||||
ChangeIdResultSet(ResultSet<StarredChange> starredChangesResultSet) {
|
||||
this.starredChangesResultSet = starredChangesResultSet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Change.Id> iterator() {
|
||||
return Iterators.transform(starredChangesResultSet.iterator(),
|
||||
STARRED_CHANGE_TO_CHANGE_ID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Change.Id> toList() {
|
||||
return Lists.transform(starredChangesResultSet.toList(),
|
||||
STARRED_CHANGE_TO_CHANGE_ID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
starredChangesResultSet.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -32,7 +32,7 @@ import java.util.List;
|
||||
/** A version of the database schema. */
|
||||
public abstract class SchemaVersion {
|
||||
/** The current schema version. */
|
||||
public static final Class<Schema_122> C = Schema_122.class;
|
||||
public static final Class<Schema_123> C = Schema_123.class;
|
||||
|
||||
public static int getBinaryVersion() {
|
||||
return guessVersion(C);
|
||||
|
@@ -0,0 +1,92 @@
|
||||
// Copyright (C) 2016 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.schema;
|
||||
|
||||
import com.google.common.collect.ArrayListMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.gerrit.reviewdb.client.Account;
|
||||
import com.google.gerrit.reviewdb.client.Change;
|
||||
import com.google.gerrit.reviewdb.client.RefNames;
|
||||
import com.google.gerrit.reviewdb.server.ReviewDb;
|
||||
import com.google.gerrit.server.StarredChangesUtil;
|
||||
import com.google.gerrit.server.config.AllUsersName;
|
||||
import com.google.gerrit.server.git.GitRepositoryManager;
|
||||
import com.google.gwtorm.jdbc.JdbcSchema;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
|
||||
import org.eclipse.jgit.lib.BatchRefUpdate;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.lib.TextProgressMonitor;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
import org.eclipse.jgit.transport.ReceiveCommand;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Map;
|
||||
|
||||
public class Schema_123 extends SchemaVersion {
|
||||
private final GitRepositoryManager repoManager;
|
||||
private final AllUsersName allUsersName;
|
||||
|
||||
@Inject
|
||||
Schema_123(Provider<Schema_122> prior,
|
||||
GitRepositoryManager repoManager,
|
||||
AllUsersName allUsersName) {
|
||||
super(prior);
|
||||
this.repoManager = repoManager;
|
||||
this.allUsersName = allUsersName;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void migrateData(ReviewDb db, UpdateUI ui)
|
||||
throws OrmException, SQLException {
|
||||
Multimap<Account.Id, Change.Id> imports = ArrayListMultimap.create();
|
||||
try (Statement stmt = ((JdbcSchema) db).getConnection().createStatement();
|
||||
ResultSet rs = stmt.executeQuery(
|
||||
"SELECT "
|
||||
+ "account_id, "
|
||||
+ "change_id "
|
||||
+ "FROM starred_changes")) {
|
||||
while (rs.next()) {
|
||||
Account.Id accountId = new Account.Id(rs.getInt(1));
|
||||
Change.Id changeId = new Change.Id(rs.getInt(2));
|
||||
imports.put(accountId, changeId);
|
||||
}
|
||||
}
|
||||
|
||||
if (imports.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try (Repository git = repoManager.openRepository(allUsersName);
|
||||
RevWalk rw = new RevWalk(git)) {
|
||||
BatchRefUpdate bru = git.getRefDatabase().newBatchUpdate();
|
||||
ObjectId id = StarredChangesUtil.writeLabels(git,
|
||||
StarredChangesUtil.DEFAULT_LABELS);
|
||||
for (Map.Entry<Account.Id, Change.Id> e : imports.entries()) {
|
||||
bru.addCommand(new ReceiveCommand(ObjectId.zeroId(), id,
|
||||
RefNames.refsStarredChanges(e.getValue(), e.getKey())));
|
||||
}
|
||||
bru.execute(rw, new TextProgressMonitor());
|
||||
} catch (IOException ex) {
|
||||
throw new OrmException(ex);
|
||||
}
|
||||
}
|
||||
}
|
@@ -32,7 +32,6 @@ import com.google.gerrit.reviewdb.server.PatchSetAccess;
|
||||
import com.google.gerrit.reviewdb.server.PatchSetApprovalAccess;
|
||||
import com.google.gerrit.reviewdb.server.ReviewDb;
|
||||
import com.google.gerrit.reviewdb.server.SchemaVersionAccess;
|
||||
import com.google.gerrit.reviewdb.server.StarredChangeAccess;
|
||||
import com.google.gerrit.reviewdb.server.SystemConfigAccess;
|
||||
import com.google.gwtorm.server.Access;
|
||||
import com.google.gwtorm.server.StatementExecutor;
|
||||
@@ -122,11 +121,6 @@ public class DisabledReviewDb implements ReviewDb {
|
||||
throw new Disabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public StarredChangeAccess starredChanges() {
|
||||
throw new Disabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccountProjectWatchAccess accountProjectWatches() {
|
||||
throw new Disabled();
|
||||
|
Reference in New Issue
Block a user