#92 -- Fix saving in transaction

This commit is contained in:
Anton Romanov 2023-02-21 17:46:55 +04:00
parent bc58d72481
commit d72cf99fa6
3 changed files with 6 additions and 13 deletions

View File

@ -41,6 +41,7 @@ public class BranchService {
@Transactional
public Branch save(Branch branch, List<Commit> commits) {
LOG.debug("Start save {} branch with {} commits ", branch.getName(), commits.size());
LOG.debug("Current branch contains {} commits ", branch.getCommits().size());
List<Integer> commitsToRemoveIds = branch.getCommits().stream().map(BaseEntity::getId).collect(Collectors.toList());
branch.setCommits(commitService.save(commits));
LOG.debug("Save branch {} ", branch.getName());
@ -50,12 +51,12 @@ public class BranchService {
return branch;
}
@Transactional
public Branch addCommits(Branch branch, List<Commit> commits) {
LOG.debug("Start add commits to {} branch with {} commits ", branch.getName(), commits.size());
commitService.save(commits);
branch.getCommits().addAll(commitService.save(commits));
LOG.debug("Save branch {} ", branch.getName());
commitService.updateBranchId(commits, branch.getId());
return branch;
return save(branch);
}
public Branch findByRepositoryAndName(GitRepository gitRepository, String branchName) {

View File

@ -44,14 +44,6 @@ public class CommitService {
jdbcTemplate.update("DELETE FROM commit where branch_id is null");
}
public void updateBranchId(List<Commit> commits, Integer branchId) {
List<String> commitIds = commits.stream().map(c -> c.getId().toString()).collect(Collectors.toList());
String updateQuery = "update commit set branch_id = ? where id in (%s)";
updateQuery = String.format(updateQuery, String.join(",", commitIds));
jdbcTemplate.update(updateQuery, branchId);
}
public List<Commit> findByRepositoryIdAndName(Integer repositoryId, String branchName) {
return commitRepository.findByRepositoryIdAndBranchName(repositoryId, branchName);
}

View File

@ -74,9 +74,9 @@ public class IndexService {
commitsTo += COMMITS_PAGE_SIZE;
commits = gitRepositoryService.getCommits(gitRepository.getUrl(), branch.getName(), commitsFrom, commitsTo, false);
}
final Branch branchForSave = branch;
branch = branchService.findByBranchId(branch.getId()).orElseThrow(() -> new RuntimeException("Branch not found by id"));
final Branch branchForSave = branchService.updateStatus(branch, IndexingStatus.FINISHED);
timeSeriesCreators.forEach(tsCreator -> tsCreator.addTimeSeries(branchForSave));
branchService.updateStatus(branch, IndexingStatus.FINISHED);
LOG.debug("Complete indexing {} branch", branch.getName());
}