Skip to content

Commit

Permalink
[bug][db] Table relation_project_user have duplicate record (#9536)
Browse files Browse the repository at this point in the history
* update sql ddl
* fix syntax err
  • Loading branch information
leiwingqueen authored Apr 27, 2022
1 parent 8fab44c commit c837580
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -526,7 +527,6 @@ public Map<String, Object> deleteUserById(User loginUser, int id) throws IOExcep
userMapper.queryTenantCodeByUserId(id);



accessTokenMapper.deleteAccessTokenByUserId(id);

userMapper.deleteById(id);
Expand Down Expand Up @@ -562,10 +562,7 @@ public Map<String, Object> grantProject(User loginUser, int userId, String proje
if (check(result, StringUtils.isEmpty(projectIds), Status.SUCCESS)) {
return result;
}

String[] projectIdArr = projectIds.split(",");

for (String projectId : projectIdArr) {
Arrays.stream(projectIds.split(",")).distinct().forEach(projectId -> {
Date now = new Date();
ProjectUser projectUser = new ProjectUser();
projectUser.setUserId(userId);
Expand All @@ -574,8 +571,7 @@ public Map<String, Object> grantProject(User loginUser, int userId, String proje
projectUser.setCreateTime(now);
projectUser.setUpdateTime(now);
projectUserMapper.insert(projectUser);
}

});
putMsg(result, Status.SUCCESS);

return result;
Expand Down Expand Up @@ -614,15 +610,18 @@ public Map<String, Object> grantProjectByCode(final User loginUser, final int us
return result;
}

// 4. maintain the relationship between project and user
final Date today = new Date();
ProjectUser projectUser = new ProjectUser();
projectUser.setUserId(userId);
projectUser.setProjectId(project.getId());
projectUser.setPerm(7);
projectUser.setCreateTime(today);
projectUser.setUpdateTime(today);
this.projectUserMapper.insert(projectUser);
// 4. maintain the relationship between project and user if not exists
ProjectUser projectUser = projectUserMapper.queryProjectRelation(project.getId(), userId);
if (projectUser == null) {
Date today = new Date();
projectUser = new ProjectUser();
projectUser.setUserId(userId);
projectUser.setProjectId(project.getId());
projectUser.setPerm(7);
projectUser.setCreateTime(today);
projectUser.setUpdateTime(today);
this.projectUserMapper.insert(projectUser);
}

this.putMsg(result, Status.SUCCESS);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,8 @@ CREATE TABLE t_ds_relation_project_user
perm int(11) DEFAULT '1',
create_time datetime DEFAULT NULL,
update_time datetime DEFAULT NULL,
PRIMARY KEY (id)
PRIMARY KEY (id),
UNIQUE KEY uniq_uid_pid(user_id,project_id)
);

-- ----------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ CREATE TABLE `t_ds_relation_project_user` (
`create_time` datetime DEFAULT NULL COMMENT 'create time',
`update_time` datetime DEFAULT NULL COMMENT 'update time',
PRIMARY KEY (`id`),
KEY `user_id_index` (`user_id`) USING BTREE
UNIQUE KEY uniq_uid_pid(user_id,project_id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

-- ----------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,8 @@ CREATE TABLE t_ds_relation_project_user (
perm int DEFAULT '1' ,
create_time timestamp DEFAULT NULL ,
update_time timestamp DEFAULT NULL ,
PRIMARY KEY (id)
PRIMARY KEY (id),
CONSTRAINT t_ds_relation_project_user_un UNIQUE (user_id, project_id)
) ;
create index relation_project_user_id_index on t_ds_relation_project_user (user_id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,8 @@ d//
delimiter ;
CALL uc_dolphin_T_t_ds_alert_R_sign;
DROP PROCEDURE uc_dolphin_T_t_ds_alert_R_sign;

-- add unique key to t_ds_relation_project_user
ALTER TABLE t_ds_relation_project_user ADD UNIQUE KEY uniq_uid_pid(user_id,project_id);


Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ $BODY$;

select dolphin_update_metadata();

d//
d//

-- add unique key to t_ds_relation_project_user
CREATE UNIQUE INDEX t_ds_relation_project_user_un
on t_ds_relation_project_user (user_id, project_id);

0 comments on commit c837580

Please sign in to comment.