From ad3a3bb49bc58db16b6d51d4893a8abc08ad2872 Mon Sep 17 00:00:00 2001 From: leiwingqueen Date: Sun, 17 Apr 2022 17:44:04 +0800 Subject: [PATCH 1/7] bugfix:relation_project_user duplicate --- .../api/service/impl/UsersServiceImpl.java | 40 ++++++++----------- .../sql/dolphinscheduler_postgresql.sql | 3 +- 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java index f83ffbfba7a1..84b7fe0f3c37 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java @@ -63,14 +63,7 @@ import java.io.IOException; import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TimeZone; +import java.util.*; import java.util.stream.Collectors; /** @@ -521,7 +514,6 @@ public Map deleteUserById(User loginUser, int id) throws IOExcep userMapper.queryTenantCodeByUserId(id); - accessTokenMapper.deleteAccessTokenByUserId(id); userMapper.deleteById(id); @@ -557,20 +549,16 @@ public Map 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(pId -> { Date now = new Date(); ProjectUser projectUser = new ProjectUser(); projectUser.setUserId(userId); - projectUser.setProjectId(Integer.parseInt(projectId)); + projectUser.setProjectId(Integer.parseInt(pId)); projectUser.setPerm(Constants.AUTHORIZE_WRITABLE_PERM); projectUser.setCreateTime(now); projectUser.setUpdateTime(now); projectUserMapper.insert(projectUser); - } - + }); putMsg(result, Status.SUCCESS); return result; @@ -610,14 +598,18 @@ public Map grantProjectByCode(final User loginUser, final int us } // 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); + // check if already existed + ProjectUser pu = projectUserMapper.queryProjectRelation(project.getId(), userId); + if (pu == null) { + 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); + } this.putMsg(result, Status.SUCCESS); return result; diff --git a/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgresql.sql b/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgresql.sql index 61119fb0b38a..76442e6ca4b1 100644 --- a/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgresql.sql +++ b/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgresql.sql @@ -622,7 +622,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), + UNIQUE KEY uniq_uid_pid(user_id,project_id) ) ; create index relation_project_user_id_index on t_ds_relation_project_user (user_id); From c5f5dbac5a3b3ba9ead2dff1ae1ad7634697570a Mon Sep 17 00:00:00 2001 From: leiwingqueen Date: Sun, 17 Apr 2022 21:12:38 +0800 Subject: [PATCH 2/7] update sql ddl --- .../api/service/impl/UsersServiceImpl.java | 10 +++++++++- .../src/main/resources/sql/dolphinscheduler_h2.sql | 3 ++- .../src/main/resources/sql/dolphinscheduler_mysql.sql | 2 +- .../2.0.6_schema/mysql/dolphinscheduler_ddl.sql | 5 +++++ .../2.0.6_schema/postgresql/dolphinscheduler_ddl.sql | 5 ++++- 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java index 84b7fe0f3c37..53ac9839a9ff 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java @@ -63,7 +63,15 @@ import java.io.IOException; import java.text.MessageFormat; -import java.util.*; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +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; /** diff --git a/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_h2.sql b/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_h2.sql index 7da05b3610bd..1d3d2e6e96c9 100644 --- a/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_h2.sql +++ b/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_h2.sql @@ -707,7 +707,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) ); -- ---------------------------- diff --git a/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_mysql.sql b/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_mysql.sql index ca0c5630395d..9f0266c764eb 100644 --- a/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_mysql.sql +++ b/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_mysql.sql @@ -707,7 +707,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; -- ---------------------------- diff --git a/dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/mysql/dolphinscheduler_ddl.sql b/dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/mysql/dolphinscheduler_ddl.sql index 45f8acd4da5f..14062d8cb0ad 100644 --- a/dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/mysql/dolphinscheduler_ddl.sql +++ b/dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/mysql/dolphinscheduler_ddl.sql @@ -36,3 +36,8 @@ d// delimiter ; CALL uc_dolphin_T_t_ds_resources_R_full_name; DROP PROCEDURE uc_dolphin_T_t_ds_resources_R_full_name; + +-- 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); + + diff --git a/dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/postgresql/dolphinscheduler_ddl.sql b/dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/postgresql/dolphinscheduler_ddl.sql index 14a20fcd8e7b..4c93d0d6c0a9 100644 --- a/dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/postgresql/dolphinscheduler_ddl.sql +++ b/dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/postgresql/dolphinscheduler_ddl.sql @@ -41,4 +41,7 @@ $BODY$; select dolphin_update_metadata(); -d// \ No newline at end of file +d// + +-- 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); \ No newline at end of file From da6633e54ea4529daf01d75ad1fee70d588e5790 Mon Sep 17 00:00:00 2001 From: leiwingqueen Date: Mon, 18 Apr 2022 11:40:02 +0800 Subject: [PATCH 3/7] fix syntax err --- .../src/main/resources/sql/dolphinscheduler_postgresql.sql | 2 +- .../upgrade/2.0.6_schema/postgresql/dolphinscheduler_ddl.sql | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgresql.sql b/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgresql.sql index 72a502d80d62..48b7e6e26654 100644 --- a/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgresql.sql +++ b/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgresql.sql @@ -626,7 +626,7 @@ CREATE TABLE t_ds_relation_project_user ( create_time timestamp DEFAULT NULL , update_time timestamp DEFAULT NULL , PRIMARY KEY (id), - UNIQUE KEY uniq_uid_pid(user_id,project_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); diff --git a/dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/postgresql/dolphinscheduler_ddl.sql b/dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/postgresql/dolphinscheduler_ddl.sql index 80edfca5674e..9f70bf422a2d 100644 --- a/dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/postgresql/dolphinscheduler_ddl.sql +++ b/dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/postgresql/dolphinscheduler_ddl.sql @@ -48,4 +48,5 @@ select dolphin_update_metadata(); d// -- 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); \ No newline at end of file +CREATE UNIQUE INDEX t_ds_relation_project_user_un + on t_ds_relation_project_user (user_id, project_id); \ No newline at end of file From 46ef30d3bd36692830a6b4ab8103abd5a4caba30 Mon Sep 17 00:00:00 2001 From: leiwingqueen Date: Tue, 19 Apr 2022 09:45:22 +0800 Subject: [PATCH 4/7] change comment --- .../dolphinscheduler/api/service/impl/UsersServiceImpl.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java index 53ac9839a9ff..f802cff62d25 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java @@ -605,8 +605,7 @@ public Map grantProjectByCode(final User loginUser, final int us return result; } - // 4. maintain the relationship between project and user - // check if already existed + // 4. maintain the relationship between project and user if not exists ProjectUser pu = projectUserMapper.queryProjectRelation(project.getId(), userId); if (pu == null) { final Date today = new Date(); From 7bff597c0fb893e55d8810817849c9a1670d6fc1 Mon Sep 17 00:00:00 2001 From: leiwingqueen Date: Fri, 22 Apr 2022 10:32:21 +0800 Subject: [PATCH 5/7] variable change name --- .../dolphinscheduler/api/service/impl/UsersServiceImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java index f802cff62d25..7c0205f29281 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java @@ -557,11 +557,11 @@ public Map grantProject(User loginUser, int userId, String proje if (check(result, StringUtils.isEmpty(projectIds), Status.SUCCESS)) { return result; } - Arrays.stream(projectIds.split(",")).distinct().forEach(pId -> { + Arrays.stream(projectIds.split(",")).distinct().forEach(projectId -> { Date now = new Date(); ProjectUser projectUser = new ProjectUser(); projectUser.setUserId(userId); - projectUser.setProjectId(Integer.parseInt(pId)); + projectUser.setProjectId(Integer.parseInt(projectId)); projectUser.setPerm(Constants.AUTHORIZE_WRITABLE_PERM); projectUser.setCreateTime(now); projectUser.setUpdateTime(now); From 8d7e172739234333ebbb800492b3dbb1c6ff8100 Mon Sep 17 00:00:00 2001 From: leiwingqueen Date: Fri, 22 Apr 2022 13:53:37 +0800 Subject: [PATCH 6/7] variable change name --- .../dolphinscheduler/api/service/impl/UsersServiceImpl.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java index 7c0205f29281..99e1a0ef2d5a 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java @@ -606,10 +606,10 @@ public Map grantProjectByCode(final User loginUser, final int us } // 4. maintain the relationship between project and user if not exists - ProjectUser pu = projectUserMapper.queryProjectRelation(project.getId(), userId); - if (pu == null) { + ProjectUser projectUser = projectUserMapper.queryProjectRelation(project.getId(), userId); + if (projectUser == null) { final Date today = new Date(); - ProjectUser projectUser = new ProjectUser(); + projectUser = new ProjectUser(); projectUser.setUserId(userId); projectUser.setProjectId(project.getId()); projectUser.setPerm(7); From b6817d0acca7edea30c4e75d3bb521e784e600f0 Mon Sep 17 00:00:00 2001 From: leiwingqueen Date: Mon, 25 Apr 2022 14:06:59 +0800 Subject: [PATCH 7/7] remote final keyword --- .../dolphinscheduler/api/service/impl/UsersServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java index 99e1a0ef2d5a..9cde088c26ee 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java @@ -608,7 +608,7 @@ public Map grantProjectByCode(final User loginUser, final int us // 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(); + Date today = new Date(); projectUser = new ProjectUser(); projectUser.setUserId(userId); projectUser.setProjectId(project.getId());