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:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user