Skip to content
Open
Changes from all 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
58 changes: 58 additions & 0 deletions conf/db/upgrade/V5.5.6__schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,61 @@ END$$
DELIMITER ;
CALL UpgradeApplicationDevelopmentServiceVersion();
DROP PROCEDURE IF EXISTS UpgradeApplicationDevelopmentServiceVersion;

-- ZSTAC-82069: Clean up orphan GpuDeviceSpecVO records where parent spec type is not GPU
-- Root cause: ZSTAC-81489 fixed GPU detection on Agent side, but didn't handle cleanup of
-- stale GpuDeviceSpecVO records when device type changed from GPU to Generic.
-- GpuDeviceSpecVO is a child table of PciDeviceSpecVO using @PrimaryKeyJoinColumn inheritance.
-- Only GPU-type specs should have corresponding records in GpuDeviceSpecVO.
DELETE g FROM GpuDeviceSpecVO g
INNER JOIN PciDeviceSpecVO p ON g.uuid = p.uuid
WHERE p.type NOT IN (
'GPU_Video_Controller',
'GPU_3D_Controller',
'GPU_Processing_Accelerators',
'GPU_Co_Processor',
'GPU_Communication_Controller'
);
Comment on lines +184 to +192
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

DELETE 语句需要反引号并补齐 NULL 类型的清理

当前语句未对表/列名加反引号,且 NOT IN 不匹配 NULL,会导致 typeNULL 的非 GPU 行未被清理。建议补齐反引号并显式处理 NULL

🛠️ 建议修改
-DELETE g FROM GpuDeviceSpecVO g
-INNER JOIN PciDeviceSpecVO p ON g.uuid = p.uuid
-WHERE p.type NOT IN (
+DELETE g FROM `GpuDeviceSpecVO` g
+INNER JOIN `PciDeviceSpecVO` p ON g.`uuid` = p.`uuid`
+WHERE (p.`type` NOT IN (
     'GPU_Video_Controller',
     'GPU_3D_Controller',
     'GPU_Processing_Accelerators',
     'GPU_Co_Processor',
     'GPU_Communication_Controller'
-);
+) OR p.`type` IS NULL);

As per coding guidelines, 所有表名和列名必须使用反引号包裹(例如:WHERE system = 1),以避免 MySQL 8.0 / GreatSQL 保留关键字冲突导致的语法错误

🤖 Prompt for AI Agents
In `@conf/db/upgrade/V5.5.6__schema.sql` around lines 184 - 192, The DELETE should
wrap identifiers in backticks and explicitly include NULL 'type' rows; update
the statement targeting GpuDeviceSpecVO and PciDeviceSpecVO (references to
`GpuDeviceSpecVO`, `PciDeviceSpecVO`, `p`.`type`, `g`.`uuid`, `p`.`uuid`) to use
backticks for all table and column names and change the predicate to treat NULL
as non-GPU (e.g. replace the NOT IN(...) check with a condition that also
matches `p`.`type` IS NULL so rows where `p`.`type` is NULL are deleted).


-- ZSTAC-73546: Migrate existing global GPU quota to per-vendor (NVIDIA) quota
-- For users who already set container.gpu.video.ram.size, copy the value as NVIDIA vendor quota.
-- Other vendor quotas will use the GlobalConfig default (32GB) via the quota framework.
DELIMITER $$

CREATE PROCEDURE MigrateGpuQuotaPerVendor()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE v_identity_uuid VARCHAR(32);
DECLARE v_identity_type VARCHAR(255);
DECLARE v_value BIGINT;
DECLARE cur CURSOR FOR
SELECT identityUuid, identityType, value
FROM QuotaVO
WHERE name = 'container.gpu.video.ram.size';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN cur;
read_loop: LOOP
FETCH cur INTO v_identity_uuid, v_identity_type, v_value;
IF done THEN
LEAVE read_loop;
END IF;

INSERT IGNORE INTO QuotaVO (uuid, name, identityUuid, identityType, value, lastOpDate, createDate)
VALUES (
REPLACE(UUID(), '-', ''),
'container.gpu.video.ram.size.nvidia',
v_identity_uuid,
v_identity_type,
v_value,
NOW(),
NOW()
);
END LOOP;
Comment on lines +205 to +228
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

迁移过程内的表/列名同样需要反引号

过程里的 QuotaVO 与字段名同样未加反引号,建议按规范补齐,避免保留字冲突与跨版本兼容问题。

🛠️ 建议修改
-    DECLARE cur CURSOR FOR
-        SELECT identityUuid, identityType, value
-        FROM QuotaVO
-        WHERE name = 'container.gpu.video.ram.size';
+    DECLARE cur CURSOR FOR
+        SELECT `identityUuid`, `identityType`, `value`
+        FROM `QuotaVO`
+        WHERE `name` = 'container.gpu.video.ram.size';
...
-        INSERT IGNORE INTO QuotaVO (uuid, name, identityUuid, identityType, value, lastOpDate, createDate)
+        INSERT IGNORE INTO `QuotaVO` (`uuid`, `name`, `identityUuid`, `identityType`, `value`, `lastOpDate`, `createDate`)

As per coding guidelines, 所有表名和列名必须使用反引号包裹(例如:WHERE system = 1),以避免 MySQL 8.0 / GreatSQL 保留关键字冲突导致的语法错误

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
DECLARE cur CURSOR FOR
SELECT identityUuid, identityType, value
FROM QuotaVO
WHERE name = 'container.gpu.video.ram.size';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
FETCH cur INTO v_identity_uuid, v_identity_type, v_value;
IF done THEN
LEAVE read_loop;
END IF;
INSERT IGNORE INTO QuotaVO (uuid, name, identityUuid, identityType, value, lastOpDate, createDate)
VALUES (
REPLACE(UUID(), '-', ''),
'container.gpu.video.ram.size.nvidia',
v_identity_uuid,
v_identity_type,
v_value,
NOW(),
NOW()
);
END LOOP;
DECLARE cur CURSOR FOR
SELECT `identityUuid`, `identityType`, `value`
FROM `QuotaVO`
WHERE `name` = 'container.gpu.video.ram.size';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
FETCH cur INTO v_identity_uuid, v_identity_type, v_value;
IF done THEN
LEAVE read_loop;
END IF;
INSERT IGNORE INTO `QuotaVO` (`uuid`, `name`, `identityUuid`, `identityType`, `value`, `lastOpDate`, `createDate`)
VALUES (
REPLACE(UUID(), '-', ''),
'container.gpu.video.ram.size.nvidia',
v_identity_uuid,
v_identity_type,
v_value,
NOW(),
NOW()
);
END LOOP;
🤖 Prompt for AI Agents
In `@conf/db/upgrade/V5.5.6__schema.sql` around lines 205 - 228, Wrap all table
and column identifiers in the migration procedure with backticks to avoid
reserved-word conflicts: update the DECLARE cur CURSOR FOR SELECT to use
`QuotaVO` and backticked columns (`identityUuid`, `identityType`, `value`,
`name`), update the WHERE clause to `WHERE \`name\` =
'container.gpu.video.ram.size'`, and change the INSERT IGNORE INTO to `INSERT
IGNORE INTO \`QuotaVO\` (\`uuid\`, \`name\`, \`identityUuid\`, \`identityType\`,
\`value\`, \`lastOpDate\`, \`createDate\`) VALUES (...)` (also backtick the
literal column names such as `container.gpu.video.ram.size.nvidia` where used);
ensure every occurrence of QuotaVO and its columns in this procedure (cursor,
FETCH, INSERT) is backticked.

CLOSE cur;
END$$

DELIMITER ;

CALL MigrateGpuQuotaPerVendor();
DROP PROCEDURE IF EXISTS MigrateGpuQuotaPerVendor;
Comment on lines +199 to +235
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Find QuotaVO table definition and constraints
rg -n 'CREATE TABLE.*QuotaVO|CREATE TABLE.*`QuotaVO`' -A 20 -g '*.sql'

Repository: MatheMatrix/zstack

Length of output: 1660


🏁 Script executed:

#!/bin/bash
# Search for any UNIQUE or KEY constraints related to QuotaVO
rg -n 'QuotaVO' -B 5 -A 10 -g '*.sql' | rg -A 10 'PRIMARY KEY|UNIQUE|KEY'

Repository: MatheMatrix/zstack

Length of output: 5723


🏁 Script executed:

#!/bin/bash
# Check the specific upgrade file for context
find . -name 'V5.5.6__schema.sql' -type f | head -5

Repository: MatheMatrix/zstack

Length of output: 98


🏁 Script executed:

#!/bin/bash
# Look for MigrateGpuQuotaPerVendor procedure in the upgrade file
rg -n 'MigrateGpuQuotaPerVendor' -B 5 -A 40 conf/db/upgrade/V5.5.6__schema.sql

Repository: MatheMatrix/zstack

Length of output: 1557


存储过程缺少前置删除检查,且缺乏幂等性保证

CREATE PROCEDURE 未在前置执行 DROP IF EXISTS,若升级中断后重跑会导致执行失败。同时 INSERT IGNOREuuid 采用自动生成且约束在整行上,无法确保业务逻辑的幂等性。当重复执行时,相同的 nameidentityUuididentityType 组合会被重复插入。

此外,表名和列名未使用反引号包裹,违反编码规范。

建议修改
+DROP PROCEDURE IF EXISTS MigrateGpuQuotaPerVendor;
 DELIMITER $$
 
 CREATE PROCEDURE MigrateGpuQuotaPerVendor()
 BEGIN
-    DECLARE cur CURSOR FOR
-        SELECT identityUuid, identityType, value
-        FROM QuotaVO
-        WHERE name = 'container.gpu.video.ram.size';
+    DECLARE cur CURSOR FOR
+        SELECT identityUuid, identityType, value
+        FROM `QuotaVO`
+        WHERE `name` = 'container.gpu.video.ram.size';
     DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
 
     OPEN cur;
     read_loop: LOOP
         FETCH cur INTO v_identity_uuid, v_identity_type, v_value;
         IF done THEN
             LEAVE read_loop;
         END IF;
 
-        INSERT IGNORE INTO QuotaVO (uuid, name, identityUuid, identityType, value, lastOpDate, createDate)
-        VALUES (
-            REPLACE(UUID(), '-', ''),
-            'container.gpu.video.ram.size.nvidia',
-            v_identity_uuid,
-            v_identity_type,
-            v_value,
-            NOW(),
-            NOW()
-        );
+        INSERT INTO `QuotaVO` (`uuid`, `name`, `identityUuid`, `identityType`, `value`, `lastOpDate`, `createDate`)
+        SELECT
+            REPLACE(UUID(), '-', ''),
+            'container.gpu.video.ram.size.nvidia',
+            v_identity_uuid,
+            v_identity_type,
+            v_value,
+            NOW(),
+            NOW()
+        FROM DUAL
+        WHERE NOT EXISTS (
+            SELECT 1
+            FROM `QuotaVO`
+            WHERE `name` = 'container.gpu.video.ram.size.nvidia'
+              AND `identityUuid` = v_identity_uuid
+              AND `identityType` = v_identity_type
+        );
🤖 Prompt for AI Agents
In `@conf/db/upgrade/V5.5.6__schema.sql` around lines 199 - 235, Add idempotency
and guard against re-creation: drop the procedure before creating it, wrap
identifiers with backticks, and change the INSERT logic to only insert when the
(name, identityUuid, identityType) tuple does not already exist. Specifically,
add DROP PROCEDURE IF EXISTS MigrateGpuQuotaPerVendor; before CREATE PROCEDURE
MigrateGpuQuotaPerVendor(), use backticks for `QuotaVO`, `uuid`, `name`,
`identityUuid`, `identityType`, `value`, `lastOpDate`, `createDate`, and replace
the INSERT IGNORE INTO QuotaVO ... VALUES(...) with an INSERT INTO `QuotaVO`
(`uuid`,`name`,`identityUuid`,`identityType`,`value`,`lastOpDate`,`createDate`)
SELECT REPLACE(UUID(),'-',''), 'container.gpu.video.ram.size.nvidia',
v_identity_uuid, v_identity_type, v_value, NOW(), NOW() FROM DUAL WHERE NOT
EXISTS (SELECT 1 FROM `QuotaVO` q WHERE
q.`name`='container.gpu.video.ram.size.nvidia' AND
q.`identityUuid`=v_identity_uuid AND q.`identityType`=v_identity_type); keep the
rest of the cursor logic and the final DROP PROCEDURE IF EXISTS call.