Browse Source

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

* update sql ddl
* fix syntax err
3.0.0/version-upgrade
leiwingqueen 3 years ago committed by GitHub
parent
commit
c837580c68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 31
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java
  2. 3
      dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_h2.sql
  3. 2
      dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_mysql.sql
  4. 3
      dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgresql.sql
  5. 5
      dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/mysql/dolphinscheduler_ddl.sql
  6. 6
      dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/postgresql/dolphinscheduler_ddl.sql

31
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java

@ -73,6 +73,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.Arrays;
import java.util.stream.Collectors;
/**
@ -526,7 +527,6 @@ public class UsersServiceImpl extends BaseServiceImpl implements UsersService {
userMapper.queryTenantCodeByUserId(id);
accessTokenMapper.deleteAccessTokenByUserId(id);
userMapper.deleteById(id);
@ -562,10 +562,7 @@ public class UsersServiceImpl extends BaseServiceImpl implements UsersService {
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);
@ -574,8 +571,7 @@ public class UsersServiceImpl extends BaseServiceImpl implements UsersService {
projectUser.setCreateTime(now);
projectUser.setUpdateTime(now);
projectUserMapper.insert(projectUser);
}
});
putMsg(result, Status.SUCCESS);
return result;
@ -614,15 +610,18 @@ public class UsersServiceImpl extends BaseServiceImpl implements UsersService {
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;

3
dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_h2.sql

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

2
dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_mysql.sql

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

3
dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgresql.sql

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

5
dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/mysql/dolphinscheduler_ddl.sql

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

6
dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/postgresql/dolphinscheduler_ddl.sql

@ -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);
Loading…
Cancel
Save