Skip to content

Commit

Permalink
- fix database design bug
Browse files Browse the repository at this point in the history
- remove exception which would cause unexpected transaction-rollback
  • Loading branch information
BlackBear2003 committed Oct 17, 2023
1 parent ba3c354 commit ad324de
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 68 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.ctrip.framework.apollo.audit.dto.ApolloAuditLogDataInfluenceDTO;
import com.ctrip.framework.apollo.audit.dto.ApolloAuditLogDetailsDTO;
import com.ctrip.framework.apollo.audit.entity.ApolloAuditLogDataInfluence;
import com.ctrip.framework.apollo.audit.exception.ApolloAuditEntityBeanDefinitionException;
import com.ctrip.framework.apollo.audit.service.ApolloAuditLogDataInfluenceService;
import com.ctrip.framework.apollo.audit.service.ApolloAuditLogService;
import com.ctrip.framework.apollo.audit.util.ApolloAuditUtil;
Expand All @@ -35,8 +34,6 @@
import java.util.Date;
import java.util.List;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ApolloAuditLogApiJpaImpl implements ApolloAuditLogApi {

Expand Down Expand Up @@ -82,11 +79,10 @@ public void appendSingleDataInfluence(String entityId, String entityName, String
}

@Override
public void appendDataInfluences(List<Object> entities, Class<?> beanDefinition) throws ApolloAuditEntityBeanDefinitionException {
public void appendDataInfluences(List<Object> entities, Class<?> beanDefinition) {
String tableName = ApolloAuditUtil.getApolloAuditLogTableName(beanDefinition);
if (Objects.isNull(tableName) || tableName.equals("")) {
throw new ApolloAuditEntityBeanDefinitionException("entity not being managed by audit annotations",
beanDefinition.getName());
return;
}
List<Field> dataInfluenceFields = ApolloAuditUtil.getAnnotatedFields(
ApolloAuditLogDataInfluenceTableField.class, beanDefinition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,10 @@ public ApolloAuditLogDataInfluence save(ApolloAuditLogDataInfluence dataInfluenc
return dataInfluenceRepository.save(dataInfluence);

Check warning on line 39 in apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/service/ApolloAuditLogDataInfluenceService.java

View check run for this annotation

Codecov / codecov/patch

apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/service/ApolloAuditLogDataInfluenceService.java#L39

Added line #L39 was not covered by tests
}

public void batchSave(List<ApolloAuditLogDataInfluence> dataInfluences) {
dataInfluenceRepository.saveAll(dataInfluences);
}

public List<ApolloAuditLogDataInfluence> findBySpanId(String spanId) {
return dataInfluenceRepository.findBySpanId(spanId);

Check warning on line 43 in apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/service/ApolloAuditLogDataInfluenceService.java

View check run for this annotation

Codecov / codecov/patch

apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/service/ApolloAuditLogDataInfluenceService.java#L43

Added line #L43 was not covered by tests
}

public List<ApolloAuditLogDataInfluence> findByEntityNameAndEntityId(String entityName,
String entityId, int page, int size) {
Pageable pageable = pageSortByTime(page, size);
return dataInfluenceRepository.findByInfluenceEntityNameAndInfluenceEntityId(entityName,
entityId, pageable);
}

public List<ApolloAuditLogDataInfluence> findByEntityNameAndEntityIdAndFieldName(
String entityName, String entityId, String fieldName, int page, int size) {
Pageable pageable = pageSortByTime(page, size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ public void logSpan(ApolloAuditSpan span) {
.spanId(span.spanId())
.parentSpanId(span.parentId())
.followsFromSpanId(span.followsFromId())

Check warning on line 48 in apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/service/ApolloAuditLogService.java

View check run for this annotation

Codecov / codecov/patch

apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/service/ApolloAuditLogService.java#L44-L48

Added lines #L44 - L48 were not covered by tests
.operator(span.operator() != null ? span.operator() : "unknown")
.operator(span.operator() != null ? span.operator() : "anonymous")
.opName(span.getOpName())
.opType(span.getOpType().toString())
.description(span.getDescription())
.happenedTime(new Date())
.build();
auditLog.setDataChangeCreatedBy(auditLog.getOperator());
logRepository.save(auditLog);
}

Check warning on line 56 in apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/service/ApolloAuditLogService.java

View check run for this annotation

Codecov / codecov/patch

apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/service/ApolloAuditLogService.java#L50-L56

Added lines #L50 - L56 were not covered by tests

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
package com.ctrip.framework.apollo.audit.component;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.ctrip.framework.apollo.audit.MockBeanFactory;
import com.ctrip.framework.apollo.audit.MockDataInfluenceEntity;
import com.ctrip.framework.apollo.audit.MockNotDataInfluenceEntity;
import com.ctrip.framework.apollo.audit.annotation.OpType;
import com.ctrip.framework.apollo.audit.context.ApolloAuditScope;
import com.ctrip.framework.apollo.audit.context.ApolloAuditScopeManager;
Expand All @@ -34,7 +32,6 @@
import com.ctrip.framework.apollo.audit.dto.ApolloAuditLogDetailsDTO;
import com.ctrip.framework.apollo.audit.entity.ApolloAuditLog;
import com.ctrip.framework.apollo.audit.entity.ApolloAuditLogDataInfluence;
import com.ctrip.framework.apollo.audit.exception.ApolloAuditEntityBeanDefinitionException;
import com.ctrip.framework.apollo.audit.service.ApolloAuditLogDataInfluenceService;
import com.ctrip.framework.apollo.audit.service.ApolloAuditLogService;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -163,10 +160,13 @@ public void testAppendSingleDataInfluenceCaseActiveSpanIsNull() {

@Test
public void testAppendDataInfluencesCaseIncompleteConditions() {
List<Object> entities = new ArrayList<>();
List<Object> entities = new ArrayList<>(entityNum);

assertThrows(ApolloAuditEntityBeanDefinitionException.class,
() -> api.appendDataInfluences(entities, MockNotDataInfluenceEntity.class));
api.appendDataInfluences(entities, Object.class);

Mockito.verify(api, Mockito.times(0))
.appendSingleDataInfluence(Mockito.any(), Mockito.any(),
Mockito.any(), Mockito.any(), Mockito.any());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public List<App> findAppsByOwner(@RequestParam("owner") String owner, Pageable p

@PreAuthorize(value = "@permissionValidator.hasCreateApplicationPermission()")
@PostMapping
@ApolloAuditLog(type = OpType.RPC, name = "App.create")
@ApolloAuditLog(type = OpType.RPC, name = "App.create.request")
public App create(@Valid @RequestBody AppModel appModel) {

App app = transformToApp(appModel);
Expand All @@ -130,7 +130,7 @@ public App create(@Valid @RequestBody AppModel appModel) {

@PreAuthorize(value = "@permissionValidator.isAppAdmin(#appId)")
@PutMapping("/{appId:.+}")
@ApolloAuditLog(type = OpType.RPC, name = "App.update")
@ApolloAuditLog(type = OpType.RPC, name = "App.update.request")
public void update(@PathVariable String appId, @Valid @RequestBody AppModel appModel) {
if (!Objects.equals(appId, appModel.getAppId())) {
throw new BadRequestException("The App Id of path variable and request body is different");
Expand Down Expand Up @@ -182,7 +182,7 @@ public AppDTO load(@PathVariable String appId) {

@PreAuthorize(value = "@permissionValidator.isSuperAdmin()")
@DeleteMapping("/{appId:.+}")
@ApolloAuditLog(type = OpType.RPC, name = "App.delete")
@ApolloAuditLog(type = OpType.RPC, name = "App.delete.request")
public void deleteApp(@PathVariable String appId) {
App app = appService.deleteAppInLocal(appId);

Expand Down
2 changes: 1 addition & 1 deletion scripts/sql/apolloconfigdb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ CREATE TABLE `AuditLog` (
`Description` varchar(200) DEFAULT NULL COMMENT '备注',
`IsDeleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '1: deleted, 0: normal',
`DeletedAt` BIGINT(20) NOT NULL DEFAULT '0' COMMENT 'Delete timestamp based on milliseconds',
`DataChange_CreatedBy` varchar(64) NOT NULL DEFAULT 'default' COMMENT '创建人邮箱前缀',
`DataChange_CreatedBy` varchar(64) DEFAULT NULL COMMENT '创建人邮箱前缀',
`DataChange_CreatedTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`DataChange_LastModifiedBy` varchar(64) DEFAULT '' COMMENT '最后修改人邮箱前缀',
`DataChange_LastTime` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后修改时间',
Expand Down
2 changes: 1 addition & 1 deletion scripts/sql/apolloportaldb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ CREATE TABLE `AuditLog` (
`Description` varchar(200) DEFAULT NULL COMMENT '备注',
`IsDeleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '1: deleted, 0: normal',
`DeletedAt` BIGINT(20) NOT NULL DEFAULT '0' COMMENT 'Delete timestamp based on milliseconds',
`DataChange_CreatedBy` varchar(64) NOT NULL DEFAULT 'default' COMMENT '创建人邮箱前缀',
`DataChange_CreatedBy` varchar(64) DEFAULT NULL COMMENT '创建人邮箱前缀',
`DataChange_CreatedTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`DataChange_LastModifiedBy` varchar(64) DEFAULT '' COMMENT '最后修改人邮箱前缀',
`DataChange_LastTime` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后修改时间',
Expand Down

0 comments on commit ad324de

Please sign in to comment.