From af295a9fd82ba285e2b01c8458b89c7eaa725eb5 Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Thu, 29 Oct 2020 18:43:29 +0800 Subject: [PATCH] refactor: flyway --- .../migration/V39__test_case_node_add_pos.sql | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 backend/src/main/resources/db/migration/V39__test_case_node_add_pos.sql diff --git a/backend/src/main/resources/db/migration/V39__test_case_node_add_pos.sql b/backend/src/main/resources/db/migration/V39__test_case_node_add_pos.sql new file mode 100644 index 0000000000..36978c314a --- /dev/null +++ b/backend/src/main/resources/db/migration/V39__test_case_node_add_pos.sql @@ -0,0 +1,58 @@ +alter table test_case_node add pos double null; + +DROP PROCEDURE IF EXISTS pos_cursor; +DELIMITER // +CREATE PROCEDURE pos_cursor() +BEGIN + DECLARE projectId VARCHAR(64); + DECLARE nodeId VARCHAR(64); + DECLARE pos DOUBLE; + DECLARE level INT; + DECLARE done INT DEFAULT 0; + DECLARE cursor1 CURSOR FOR (SELECT DISTINCT project_id + FROM test_case_node + WHERE pos IS NULL); + DECLARE cursor2 CURSOR FOR (select id + from test_case_node + where project_id = projectId + and test_case_node.level = level + order by create_time); + DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; + OPEN cursor1; + outer_loop: + LOOP + FETCH cursor1 INTO projectId; + IF done + THEN + LEAVE outer_loop; + END IF; + SET level = 1; + select max(test_case_node.level) into @max_level from test_case_node where project_id = projectId; + while level <= @max_level + do + set pos = 65536; + OPEN cursor2; + inner_loop: + LOOP + FETCH cursor2 INTO nodeId; + IF done + THEN + LEAVE inner_loop; + END IF; + UPDATE test_case_node + SET test_case_node.pos = pos + WHERE id = nodeId; + SET pos = pos + 65536; + END LOOP; + SET done = 0; + CLOSE cursor2; + set level = level + 1; + end while; + + END LOOP; + CLOSE cursor1; +END // +DELIMITER ; + +CALL pos_cursor(); +DROP PROCEDURE IF EXISTS pos_cursor; \ No newline at end of file