From b8a90d801e2c38c59dd2d250ff5c8a5505cceeed Mon Sep 17 00:00:00 2001 From: WangJPLeo <103574007+WangJPLeo@users.noreply.github.com> Date: Thu, 7 Jul 2022 14:35:50 +0800 Subject: [PATCH] [Improvement] Resource authority module database table unique index increased. (#10807) * Resource authority module database table unique index increased. * add unique in sql file * rerun * ut fix * e2e rerun * dao un fix --- .../src/main/resources/sql/dolphinscheduler_h2.sql | 13 +++++++++---- .../main/resources/sql/dolphinscheduler_mysql.sql | 13 +++++++++---- .../resources/sql/dolphinscheduler_postgresql.sql | 11 ++++++++--- .../3.0.0_schema/mysql/dolphinscheduler_ddl.sql | 13 +++++++++++++ .../postgresql/dolphinscheduler_ddl.sql | 13 +++++++++++++ .../dao/mapper/UdfFuncMapperTest.java | 14 +++++++------- 6 files changed, 59 insertions(+), 18 deletions(-) diff --git a/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_h2.sql b/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_h2.sql index b9dcc6191c..134d31644b 100644 --- a/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_h2.sql +++ b/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_h2.sql @@ -638,7 +638,9 @@ CREATE TABLE t_ds_project flag tinyint(4) DEFAULT '1', create_time datetime NOT NULL, update_time datetime DEFAULT NULL, - PRIMARY KEY (id) + PRIMARY KEY (id), + UNIQUE KEY unique_name (name), + UNIQUE KEY unique_code (code) ); -- ---------------------------- @@ -656,7 +658,8 @@ CREATE TABLE t_ds_queue queue varchar(64) DEFAULT NULL, create_time datetime DEFAULT NULL, update_time datetime DEFAULT NULL, - PRIMARY KEY (id) + PRIMARY KEY (id), + UNIQUE KEY unique_queue_name (queue_name) ); -- ---------------------------- @@ -886,7 +889,8 @@ CREATE TABLE t_ds_tenant queue_id int(11) DEFAULT NULL, create_time datetime DEFAULT NULL, update_time datetime DEFAULT NULL, - PRIMARY KEY (id) + PRIMARY KEY (id), + UNIQUE KEY unique_tenant_code (tenant_code) ); -- ---------------------------- @@ -911,7 +915,8 @@ CREATE TABLE t_ds_udfs resource_name varchar(255) NOT NULL, create_time datetime NOT NULL, update_time datetime NOT NULL, - PRIMARY KEY (id) + PRIMARY KEY (id), + UNIQUE KEY unique_func_name (func_name) ); -- ---------------------------- diff --git a/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_mysql.sql b/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_mysql.sql index d44449d59b..62ebe62038 100644 --- a/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_mysql.sql +++ b/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_mysql.sql @@ -642,7 +642,9 @@ CREATE TABLE `t_ds_project` ( `create_time` datetime NOT NULL COMMENT 'create time', `update_time` datetime DEFAULT NULL COMMENT 'update time', PRIMARY KEY (`id`), - KEY `user_id_index` (`user_id`) USING BTREE + KEY `user_id_index` (`user_id`) USING BTREE, + UNIQUE KEY `unique_name`(`name`), + UNIQUE KEY `unique_code`(`code`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; -- ---------------------------- @@ -659,7 +661,8 @@ CREATE TABLE `t_ds_queue` ( `queue` varchar(64) DEFAULT NULL COMMENT 'yarn queue name', `create_time` datetime DEFAULT NULL COMMENT 'create time', `update_time` datetime DEFAULT NULL COMMENT 'update time', - PRIMARY KEY (`id`) + PRIMARY KEY (`id`), + UNIQUE KEY `unique_queue_name`(`queue_name`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; -- ---------------------------- @@ -882,7 +885,8 @@ CREATE TABLE `t_ds_tenant` ( `queue_id` int(11) DEFAULT NULL COMMENT 'queue id', `create_time` datetime DEFAULT NULL COMMENT 'create time', `update_time` datetime DEFAULT NULL COMMENT 'update time', - PRIMARY KEY (`id`) + PRIMARY KEY (`id`), + UNIQUE KEY `unique_tenant_code`(`tenant_code`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; -- ---------------------------- @@ -906,7 +910,8 @@ CREATE TABLE `t_ds_udfs` ( `resource_name` varchar(255) NOT NULL COMMENT 'resource name', `create_time` datetime NOT NULL COMMENT 'create time', `update_time` datetime NOT NULL COMMENT 'update time', - PRIMARY KEY (`id`) + PRIMARY KEY (`id`), + UNIQUE KEY `unique_func_name`(`func_name`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; -- ---------------------------- diff --git a/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgresql.sql b/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgresql.sql index e7045ba71e..148d04c2cd 100644 --- a/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgresql.sql +++ b/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgresql.sql @@ -571,6 +571,8 @@ CREATE TABLE t_ds_project ( ) ; create index user_id_index on t_ds_project (user_id); +CREATE UNIQUE INDEX unique_name on t_ds_project (name); +CREATE UNIQUE INDEX unique_code on t_ds_project (code); -- -- Table structure for table t_ds_queue @@ -585,7 +587,8 @@ CREATE TABLE t_ds_queue ( update_time timestamp DEFAULT NULL , PRIMARY KEY (id) ); - +-- add unique key to t_ds_queue +CREATE UNIQUE INDEX unique_queue_name on t_ds_queue (queue_name); -- -- Table structure for table t_ds_relation_datasource_user @@ -601,7 +604,6 @@ CREATE TABLE t_ds_relation_datasource_user ( update_time timestamp DEFAULT NULL , PRIMARY KEY (id) ) ; -; -- -- Table structure for table t_ds_relation_process_instance @@ -787,6 +789,8 @@ CREATE TABLE t_ds_tenant ( update_time timestamp DEFAULT NULL , PRIMARY KEY (id) ) ; +-- add unique key to t_ds_tenant +CREATE UNIQUE INDEX unique_tenant_code on t_ds_tenant (tenant_code); -- -- Table structure for table t_ds_udfs @@ -808,6 +812,8 @@ CREATE TABLE t_ds_udfs ( update_time timestamp NOT NULL , PRIMARY KEY (id) ) ; +-- add unique key to t_ds_udfs +CREATE UNIQUE INDEX unique_func_name on t_ds_udfs (func_name); -- -- Table structure for table t_ds_user @@ -967,7 +973,6 @@ DROP SEQUENCE IF EXISTS t_ds_worker_server_id_sequence; CREATE SEQUENCE t_ds_worker_server_id_sequence; ALTER TABLE t_ds_worker_server ALTER COLUMN id SET DEFAULT NEXTVAL('t_ds_worker_server_id_sequence'); - -- Records of t_ds_user?user : admin , password : dolphinscheduler123 INSERT INTO t_ds_user(user_name, user_password, user_type, email, phone, tenant_id, state, create_time, update_time, time_zone) VALUES ('admin', '7ad2410b2f4c074479a8937a28a22b8f', '0', 'xxx@qq.com', '', '0', 1, '2018-03-27 15:48:50', '2018-10-24 17:40:22', 'Asia/Shanghai'); diff --git a/dolphinscheduler-dao/src/main/resources/sql/upgrade/3.0.0_schema/mysql/dolphinscheduler_ddl.sql b/dolphinscheduler-dao/src/main/resources/sql/upgrade/3.0.0_schema/mysql/dolphinscheduler_ddl.sql index 8a8cc7f27f..081e12f1f8 100644 --- a/dolphinscheduler-dao/src/main/resources/sql/upgrade/3.0.0_schema/mysql/dolphinscheduler_ddl.sql +++ b/dolphinscheduler-dao/src/main/resources/sql/upgrade/3.0.0_schema/mysql/dolphinscheduler_ddl.sql @@ -61,6 +61,19 @@ 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); +-- add unique key to t_ds_project +ALTER TABLE t_ds_project ADD UNIQUE KEY unique_name(name); +ALTER TABLE t_ds_project ADD UNIQUE KEY unique_code(code); + +-- add unique key to t_ds_queue +ALTER TABLE t_ds_queue ADD UNIQUE KEY unique_queue_name(queue_name); + +-- add unique key to t_ds_udfs +ALTER TABLE t_ds_udfs ADD UNIQUE KEY unique_func_name(func_name); + +-- add unique key to t_ds_tenant +ALTER TABLE t_ds_tenant ADD UNIQUE KEY unique_tenant_code(tenant_code); + ALTER TABLE `t_ds_task_instance` ADD INDEX `idx_code_version` (`task_code`, `task_definition_version`) USING BTREE; ALTER TABLE `t_ds_task_instance` MODIFY COLUMN `task_params` longtext COMMENT 'job custom parameters' AFTER `app_link`; ALTER TABLE `t_ds_process_task_relation` ADD KEY `idx_code` (`project_code`, `process_definition_code`) USING BTREE; diff --git a/dolphinscheduler-dao/src/main/resources/sql/upgrade/3.0.0_schema/postgresql/dolphinscheduler_ddl.sql b/dolphinscheduler-dao/src/main/resources/sql/upgrade/3.0.0_schema/postgresql/dolphinscheduler_ddl.sql index 4c1cb7e63f..c00c7deadc 100644 --- a/dolphinscheduler-dao/src/main/resources/sql/upgrade/3.0.0_schema/postgresql/dolphinscheduler_ddl.sql +++ b/dolphinscheduler-dao/src/main/resources/sql/upgrade/3.0.0_schema/postgresql/dolphinscheduler_ddl.sql @@ -60,6 +60,19 @@ d// CREATE UNIQUE INDEX t_ds_relation_project_user_un on t_ds_relation_project_user (user_id, project_id); +-- add unique key to t_ds_project +CREATE UNIQUE INDEX unique_name on t_ds_project (name); +CREATE UNIQUE INDEX unique_code on t_ds_project (code); + +-- add unique key to t_ds_queue +CREATE UNIQUE INDEX unique_queue_name on t_ds_queue (queue_name); + +-- add unique key to t_ds_udfs +CREATE UNIQUE INDEX unique_func_name on t_ds_udfs (func_name); + +-- add unique key to t_ds_tenant +CREATE UNIQUE INDEX unique_tenant_code on t_ds_tenant (tenant_code); + delimiter d// CREATE OR REPLACE FUNCTION public.dolphin_update_metadata( ) diff --git a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/UdfFuncMapperTest.java b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/UdfFuncMapperTest.java index 704bf0e131..1adbe689ae 100644 --- a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/UdfFuncMapperTest.java +++ b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/UdfFuncMapperTest.java @@ -54,10 +54,10 @@ public class UdfFuncMapperTest extends BaseDaoTest { * * @return UdfFunc */ - private UdfFunc insertOne() { + private UdfFunc insertOne(String funcName) { UdfFunc udfFunc = new UdfFunc(); udfFunc.setUserId(1); - udfFunc.setFuncName("dolphin_udf_func"); + udfFunc.setFuncName(funcName); udfFunc.setClassName("org.apache.dolphinscheduler.test.mr"); udfFunc.setType(UdfType.HIVE); udfFunc.setResourceId(1); @@ -76,7 +76,7 @@ public class UdfFuncMapperTest extends BaseDaoTest { private UdfFunc insertOne(User user) { UdfFunc udfFunc = new UdfFunc(); udfFunc.setUserId(user.getId()); - udfFunc.setFuncName("dolphin_udf_func"); + udfFunc.setFuncName("dolphin_udf_func" + user.getUserName()); udfFunc.setClassName("org.apache.dolphinscheduler.test.mr"); udfFunc.setType(UdfType.HIVE); udfFunc.setResourceId(1); @@ -164,7 +164,7 @@ public class UdfFuncMapperTest extends BaseDaoTest { @Test public void testUpdate() { //insertOne - UdfFunc udfFunc = insertOne(); + UdfFunc udfFunc = insertOne("func1"); udfFunc.setResourceName("dolphin_resource_update"); udfFunc.setResourceId(2); udfFunc.setClassName("org.apache.dolphinscheduler.test.mrUpdate"); @@ -181,7 +181,7 @@ public class UdfFuncMapperTest extends BaseDaoTest { @Test public void testDelete() { //insertOne - UdfFunc udfFunc = insertOne(); + UdfFunc udfFunc = insertOne("func2"); //delete int delete = udfFuncMapper.deleteById(udfFunc.getId()); Assert.assertEquals(delete, 1); @@ -193,9 +193,9 @@ public class UdfFuncMapperTest extends BaseDaoTest { @Test public void testQueryUdfByIdStr() { //insertOne - UdfFunc udfFunc = insertOne(); + UdfFunc udfFunc = insertOne("func3"); //insertOne - UdfFunc udfFunc1 = insertOne(); + UdfFunc udfFunc1 = insertOne("func4"); Integer[] idArray = new Integer[]{udfFunc.getId(), udfFunc1.getId()}; //queryUdfByIdStr List udfFuncList = udfFuncMapper.queryUdfByIdStr(idArray, "");