Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug][db] Table relation_project_user have duplicate record #9536

Merged
merged 8 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,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 @@ -521,7 +522,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 @@ -557,10 +557,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 @@ -569,8 +566,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 @@ -609,15 +605,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) {
final Date today = new Date();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the final identifier is not needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I try to remove it

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),
SbloodyS marked this conversation as resolved.
Show resolved Hide resolved
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we should add to new SQL directory named 3.0.0?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we should add to new SQL directory named 3.0.0?

It's a bug fix. I think it should be included in 2.0.6-release. WDYT? @zhongjiajie

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I think change targe branch with dev database change should add to 3.0.0_schema, and committer judge whether should release in bug fix version. If it is only put in 2.0.5 and not in 3.0.0, it may be that the 3.0.0-bate release will miss this commit.

But in case, dev branch without 3.0.0_schema folder,(and that is my wrong, i forgot to add it after 3.0.0-release) so i think it could change into 2.0.6 or 3.0.0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I think change targe branch with dev database change should add to 3.0.0_schema, and committer judge whether should release in bug fix version. If it is only put in 2.0.5 and not in 3.0.0, it may be that the 3.0.0-bate release will miss this commit.

But in case, dev branch without 3.0.0_schema folder,(and that is my wrong, i forgot to add it after 3.0.0-release) so i think it could change into 2.0.6 or 3.0.0

I agree with you. @zhongjiajie



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);