Skip to content

Commit

Permalink
Fix issue on project bulk update key
Browse files Browse the repository at this point in the history
  • Loading branch information
julienlancelot committed Jan 14, 2015
1 parent 47e1279 commit ed9951f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 31 deletions.
Expand Up @@ -138,15 +138,14 @@ public Map<String, String> checkModuleKeysBeforeRenaming(String projectKey, Stri
public void bulkUpdateKey(String projectKey, String stringToReplace, String replacementString) {
UserSession.get().checkProjectPermission(UserRole.ADMIN, projectKey);

DbSession session = dbClient.openSession(false);
// Open a batch session
DbSession session = dbClient.openSession(true);
try {
ComponentDto project = getByKey(session, projectKey);

resourceKeyUpdaterDao.bulkUpdateKey(project.getId(), stringToReplace, replacementString);
session.commit();
resourceKeyUpdaterDao.bulkUpdateKey(session, project.getId(), stringToReplace, replacementString);

ComponentDto newProject = dbClient.componentDao().getById(project.getId(), session);
previewCache.reportResourceModification(newProject.key());
previewCache.reportResourceModification(session, newProject.key());

session.commit();
} finally {
Expand Down
15 changes: 13 additions & 2 deletions sonar-core/src/main/java/org/sonar/core/preview/PreviewCache.java
Expand Up @@ -27,6 +27,7 @@
import org.sonar.api.ServerExtension;
import org.sonar.api.platform.ServerFileSystem;
import org.sonar.api.utils.SonarException;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.persistence.MyBatis;
import org.sonar.core.persistence.PreviewDatabaseFactory;
import org.sonar.core.properties.PropertiesDao;
Expand Down Expand Up @@ -207,11 +208,21 @@ public void reportGlobalModification(SqlSession session) {
}

public void reportResourceModification(String resourceKey) {
ResourceDto rootProject = resourceDao.getRootProjectByComponentKey(resourceKey);
DbSession session = mybatis.openSession(false);
try {
reportResourceModification(session, resourceKey);
session.commit();
} finally {
MyBatis.closeQuietly(session);
}
}

public void reportResourceModification(DbSession session, String resourceKey) {
ResourceDto rootProject = resourceDao.getRootProjectByComponentKey(session, resourceKey);
if (rootProject == null) {
throw new SonarException("Unable to find root project for component with [key=" + resourceKey + "]");
}
propertiesDao.setProperty(new PropertyDto().setKey(SONAR_PREVIEW_CACHE_LAST_UPDATE_KEY).setResourceId(rootProject.getId())
.setValue(String.valueOf(System.currentTimeMillis())));
.setValue(String.valueOf(System.currentTimeMillis())), session);
}
}
Expand Up @@ -87,27 +87,30 @@ public Map<String, String> checkModuleKeysBeforeRenaming(long projectId, String
return result;
}

public void bulkUpdateKey(long projectId, String stringToReplace, String replacementString) {
DbSession session = mybatis.openSession(true);
public void bulkUpdateKey(DbSession session, long projectId, String stringToReplace, String replacementString) {
ResourceKeyUpdaterMapper mapper = session.getMapper(ResourceKeyUpdaterMapper.class);
try {
// must SELECT first everything
Set<ResourceDto> modules = collectAllModules(projectId, stringToReplace, mapper);
checkNewNameOfAllModules(modules, stringToReplace, replacementString, mapper);
Map<ResourceDto, List<ResourceDto>> allResourcesByModuleMap = Maps.newHashMap();
for (ResourceDto module : modules) {
allResourcesByModuleMap.put(module, mapper.selectProjectResources(module.getId()));
}
// must SELECT first everything
Set<ResourceDto> modules = collectAllModules(projectId, stringToReplace, mapper);
checkNewNameOfAllModules(modules, stringToReplace, replacementString, mapper);
Map<ResourceDto, List<ResourceDto>> allResourcesByModuleMap = Maps.newHashMap();
for (ResourceDto module : modules) {
allResourcesByModuleMap.put(module, mapper.selectProjectResources(module.getId()));
}

// and then proceed with the batch UPDATE at once
for (ResourceDto module : modules) {
String oldModuleKey = module.getKey();
String newModuleKey = computeNewKey(module, stringToReplace, replacementString);
Collection<ResourceDto> resources = Lists.newArrayList(module);
resources.addAll(allResourcesByModuleMap.get(module));
runBatchUpdateForAllResources(resources, oldModuleKey, newModuleKey, mapper);
}
// and then proceed with the batch UPDATE at once
for (ResourceDto module : modules) {
String oldModuleKey = module.getKey();
String newModuleKey = computeNewKey(module, stringToReplace, replacementString);
Collection<ResourceDto> resources = Lists.newArrayList(module);
resources.addAll(allResourcesByModuleMap.get(module));
runBatchUpdateForAllResources(resources, oldModuleKey, newModuleKey, mapper);
}
}

public void bulkUpdateKey(long projectId, String stringToReplace, String replacementString) {
DbSession session = mybatis.openSession(true);
try {
bulkUpdateKey(session, projectId, stringToReplace, replacementString);
session.commit();
} finally {
MyBatis.closeQuietly(session);
Expand Down
Expand Up @@ -45,10 +45,7 @@
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

public class PreviewCacheTest {

Expand Down Expand Up @@ -226,14 +223,14 @@ public void test_report_global_modification() {

@Test
public void test_report_resource_modification() {
when(resourceDao.getRootProjectByComponentKey("foo")).thenReturn(new ResourceDto().setId(456L));
when(resourceDao.getRootProjectByComponentKey(session, "foo")).thenReturn(new ResourceDto().setId(456L));

dryRunCache.reportResourceModification("foo");

verify(propertiesDao).setProperty(
new PropertyDto()
.setKey(PreviewCache.SONAR_PREVIEW_CACHE_LAST_UPDATE_KEY)
.setValue(anyString())
.setResourceId(456L));
.setResourceId(456L), eq(session));
}
}

0 comments on commit ed9951f

Please sign in to comment.