use of org.eclipse.jgit.lib.BatchRefUpdate in project gerrit by GerritCodeReview.
the class Schema_139 method migrateData.
@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
ListMultimap<Account.Id, ProjectWatch> imports = MultimapBuilder.hashKeys().arrayListValues().build();
try (Statement stmt = ((JdbcSchema) db).getConnection().createStatement();
ResultSet rs = stmt.executeQuery("SELECT " + "account_id, " + "project_name, " + "filter, " + "notify_abandoned_changes, " + "notify_all_comments, " + "notify_new_changes, " + "notify_new_patch_sets, " + "notify_submitted_changes " + "FROM account_project_watches")) {
while (rs.next()) {
Account.Id accountId = new Account.Id(rs.getInt(1));
ProjectWatch.Builder b = ProjectWatch.builder().project(new Project.NameKey(rs.getString(2))).filter(rs.getString(3)).notifyAbandonedChanges(rs.getBoolean(4)).notifyAllComments(rs.getBoolean(5)).notifyNewChanges(rs.getBoolean(6)).notifyNewPatchSets(rs.getBoolean(7)).notifySubmittedChanges(rs.getBoolean(8));
imports.put(accountId, b.build());
}
}
if (imports.isEmpty()) {
return;
}
try (Repository git = repoManager.openRepository(allUsersName);
RevWalk rw = new RevWalk(git)) {
BatchRefUpdate bru = git.getRefDatabase().newBatchUpdate();
bru.setRefLogIdent(serverUser);
bru.setRefLogMessage(MSG, false);
for (Map.Entry<Account.Id, Collection<ProjectWatch>> e : imports.asMap().entrySet()) {
Map<ProjectWatchKey, Set<NotifyType>> projectWatches = new HashMap<>();
for (ProjectWatch projectWatch : e.getValue()) {
ProjectWatchKey key = ProjectWatchKey.create(projectWatch.project(), projectWatch.filter());
if (projectWatches.containsKey(key)) {
throw new OrmDuplicateKeyException("Duplicate key for watched project: " + key.toString());
}
Set<NotifyType> notifyValues = EnumSet.noneOf(NotifyType.class);
if (projectWatch.notifyAbandonedChanges()) {
notifyValues.add(NotifyType.ABANDONED_CHANGES);
}
if (projectWatch.notifyAllComments()) {
notifyValues.add(NotifyType.ALL_COMMENTS);
}
if (projectWatch.notifyNewChanges()) {
notifyValues.add(NotifyType.NEW_CHANGES);
}
if (projectWatch.notifyNewPatchSets()) {
notifyValues.add(NotifyType.NEW_PATCHSETS);
}
if (projectWatch.notifySubmittedChanges()) {
notifyValues.add(NotifyType.SUBMITTED_CHANGES);
}
projectWatches.put(key, notifyValues);
}
try (MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, allUsersName, git, bru)) {
md.getCommitBuilder().setAuthor(serverUser);
md.getCommitBuilder().setCommitter(serverUser);
md.setMessage(MSG);
WatchConfig watchConfig = new WatchConfig(e.getKey());
watchConfig.load(md);
watchConfig.setProjectWatches(projectWatches);
watchConfig.commit(md);
}
}
bru.execute(rw, NullProgressMonitor.INSTANCE);
} catch (IOException | ConfigInvalidException ex) {
throw new OrmException(ex);
}
}
use of org.eclipse.jgit.lib.BatchRefUpdate in project gerrit by GerritCodeReview.
the class Schema_119 method migrateData.
@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
JdbcSchema schema = (JdbcSchema) db;
Connection connection = schema.getConnection();
String tableName = "accounts";
String emailStrategy = "email_strategy";
Set<String> columns = schema.getDialect().listColumns(connection, tableName);
Map<Account.Id, GeneralPreferencesInfo> imports = new HashMap<>();
try (Statement stmt = ((JdbcSchema) db).getConnection().createStatement();
ResultSet rs = stmt.executeQuery("select " + "account_id, " + "maximum_page_size, " + "show_site_header, " + "use_flash_clipboard, " + "download_url, " + "download_command, " + (columns.contains(emailStrategy) ? emailStrategy + ", " : "copy_self_on_email, ") + "date_format, " + "time_format, " + "relative_date_in_change_table, " + "diff_view, " + "size_bar_in_change_table, " + "legacycid_in_change_table, " + "review_category_strategy, " + "mute_common_path_prefixes " + "from " + tableName)) {
while (rs.next()) {
GeneralPreferencesInfo p = new GeneralPreferencesInfo();
Account.Id accountId = new Account.Id(rs.getInt(1));
p.changesPerPage = (int) rs.getShort(2);
p.showSiteHeader = toBoolean(rs.getString(3));
p.useFlashClipboard = toBoolean(rs.getString(4));
p.downloadScheme = convertToModernNames(rs.getString(5));
p.downloadCommand = toDownloadCommand(rs.getString(6));
p.emailStrategy = toEmailStrategy(rs.getString(7), columns.contains(emailStrategy));
p.dateFormat = toDateFormat(rs.getString(8));
p.timeFormat = toTimeFormat(rs.getString(9));
p.relativeDateInChangeTable = toBoolean(rs.getString(10));
p.diffView = toDiffView(rs.getString(11));
p.sizeBarInChangeTable = toBoolean(rs.getString(12));
p.legacycidInChangeTable = toBoolean(rs.getString(13));
p.reviewCategoryStrategy = toReviewCategoryStrategy(rs.getString(14));
p.muteCommonPathPrefixes = toBoolean(rs.getString(15));
p.defaultBaseForMerges = GeneralPreferencesInfo.defaults().defaultBaseForMerges;
imports.put(accountId, p);
}
}
if (imports.isEmpty()) {
return;
}
try (Repository git = mgr.openRepository(allUsersName);
RevWalk rw = new RevWalk(git)) {
BatchRefUpdate bru = git.getRefDatabase().newBatchUpdate();
for (Map.Entry<Account.Id, GeneralPreferencesInfo> e : imports.entrySet()) {
try (MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, allUsersName, git, bru)) {
md.getCommitBuilder().setAuthor(serverUser);
md.getCommitBuilder().setCommitter(serverUser);
VersionedAccountPreferences p = VersionedAccountPreferences.forUser(e.getKey());
p.load(md);
storeSection(p.getConfig(), UserConfigSections.GENERAL, null, e.getValue(), GeneralPreferencesInfo.defaults());
p.commit(md);
}
}
bru.execute(rw, NullProgressMonitor.INSTANCE);
} catch (ConfigInvalidException | IOException ex) {
throw new OrmException(ex);
}
}
use of org.eclipse.jgit.lib.BatchRefUpdate in project gerrit by GerritCodeReview.
the class Schema_123 method migrateData.
@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
ListMultimap<Account.Id, Change.Id> imports = MultimapBuilder.hashKeys().arrayListValues().build();
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);
}
}
use of org.eclipse.jgit.lib.BatchRefUpdate in project gerrit by GerritCodeReview.
the class Schema_124 method migrateData.
@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
ListMultimap<Account.Id, AccountSshKey> imports = MultimapBuilder.hashKeys().arrayListValues().build();
try (Statement stmt = ((JdbcSchema) db).getConnection().createStatement();
ResultSet rs = stmt.executeQuery("SELECT " + "account_id, " + "seq, " + "ssh_public_key, " + "valid " + "FROM account_ssh_keys")) {
while (rs.next()) {
Account.Id accountId = new Account.Id(rs.getInt(1));
int seq = rs.getInt(2);
String sshPublicKey = rs.getString(3);
AccountSshKey key = new AccountSshKey(new AccountSshKey.Id(accountId, seq), sshPublicKey);
boolean valid = toBoolean(rs.getString(4));
if (!valid) {
key.setInvalid();
}
imports.put(accountId, key);
}
}
if (imports.isEmpty()) {
return;
}
try (Repository git = repoManager.openRepository(allUsersName);
RevWalk rw = new RevWalk(git)) {
BatchRefUpdate bru = git.getRefDatabase().newBatchUpdate();
for (Map.Entry<Account.Id, Collection<AccountSshKey>> e : imports.asMap().entrySet()) {
try (MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, allUsersName, git, bru)) {
md.getCommitBuilder().setAuthor(serverUser);
md.getCommitBuilder().setCommitter(serverUser);
VersionedAuthorizedKeys authorizedKeys = new VersionedAuthorizedKeys(new SimpleSshKeyCreator(), e.getKey());
authorizedKeys.load(md);
authorizedKeys.setKeys(fixInvalidSequenceNumbers(e.getValue()));
authorizedKeys.commit(md);
}
}
bru.execute(rw, NullProgressMonitor.INSTANCE);
} catch (ConfigInvalidException | IOException ex) {
throw new OrmException(ex);
}
}
use of org.eclipse.jgit.lib.BatchRefUpdate in project gerrit by GerritCodeReview.
the class RebuildNoteDb method deleteRefs.
private void deleteRefs(String prefix, Repository allUsersRepo) throws IOException {
RefDatabase refDb = allUsersRepo.getRefDatabase();
Map<String, Ref> allRefs = refDb.getRefs(prefix);
BatchRefUpdate bru = refDb.newBatchUpdate();
for (Map.Entry<String, Ref> ref : allRefs.entrySet()) {
bru.addCommand(new ReceiveCommand(ref.getValue().getObjectId(), ObjectId.zeroId(), prefix + ref.getKey()));
}
execute(bru, allUsersRepo);
}
Aggregations