cmvr-wms/sql/projectdev/20261021_member_project_id_cutover.sql

50 lines
4.2 KiB
MySQL
Raw Permalink Normal View History

-- MANUAL COORDINATED CUTOVER ONLY. Never run against an old collaboration application.
-- 1. Back up the full database and verify restore before using this file.
-- 2. Stop all writers. Deploy the colleague's compatible collaboration release together with ours.
-- 3. Inspect the preflight below, resolve missing/inactive creators or multiple managers explicitly.
-- 4. Set @project_member_cutover_confirmed=1 in this same connection, then run the procedure.
-- This file does not modify collaboration application code, iterations, tasks or audit records.
DELIMITER $$
CREATE PROCEDURE cutover_project_member_ids_20261021()
BEGIN
DECLARE failures bigint DEFAULT 0;
IF COALESCE(@project_member_cutover_confirmed,0)<>1 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT='Coordinated cutover not confirmed; no data changed';
END IF;
IF EXISTS(SELECT 1 FROM information_schema.key_column_usage WHERE table_schema=database() AND table_name='pm_collab_project_member' AND column_name='project_id' AND referenced_table_name='project_info') THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT='Already migrated; do not replay';
END IF;
SELECT count(*) INTO failures FROM pm_collab_project_member m LEFT JOIN pm_collab_project e ON e.id=m.project_id
LEFT JOIN project_info p ON p.id=e.source_project_id WHERE p.id IS NULL OR m.project_id<=0;
IF failures>0 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT='Unmapped member identities; review required'; END IF;
SELECT count(*) INTO failures FROM (SELECT project_id FROM pm_collab_project_member WHERE member_role='MANAGER' GROUP BY project_id HAVING count(*)>1) conflicting;
IF failures>0 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT='Multiple managers; review required'; END IF;
SELECT count(*) INTO failures FROM pm_collab_project_member m JOIN pm_collab_project e ON e.id=m.project_id JOIN project_info p ON p.id=e.source_project_id
WHERE m.member_role='MANAGER' AND m.user_id<>p.creator_user_id AND p.del_flag='0';
IF failures>0 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT='Explicit manager differs from implicit creator; confirm manager mapping first'; END IF;
SELECT count(*) INTO failures FROM project_info p LEFT JOIN sys_user u ON u.user_id=p.creator_user_id
WHERE p.del_flag='0' AND p.project_code<>'LEGACY' AND (u.user_id IS NULL OR u.status<>'0' OR u.del_flag<>'0');
IF failures>0 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT='Missing/inactive creators; explicit migration mapping required'; END IF;
-- Preserve an immutable identity backup. An existing backup deliberately prevents accidental replay.
CREATE TABLE pm_member_identity_backup_20261021 AS SELECT m.*,e.source_project_id FROM pm_collab_project_member m JOIN pm_collab_project e ON e.id=m.project_id;
-- Validate the deployed FK name before applying. This is the name in the repository schema.
ALTER TABLE pm_collab_project_member DROP FOREIGN KEY pm_collab_project_member_ibfk_1;
-- Negative intermediate IDs prevent unique-key collisions when old and new numeric ranges overlap.
START TRANSACTION;
UPDATE pm_collab_project_member m JOIN pm_collab_project e ON e.id=m.project_id SET m.project_id=-e.source_project_id;
UPDATE pm_collab_project_member SET project_id=-project_id;
INSERT INTO pm_collab_project_member(project_id,user_id,member_role,create_by,create_time)
SELECT p.id,p.creator_user_id,IF(EXISTS(SELECT 1 FROM pm_member_identity_backup_20261021 b WHERE b.source_project_id=p.id AND b.member_role='MANAGER'),'MEMBER','MANAGER'),'migration',current_timestamp(6)
FROM project_info p WHERE p.del_flag='0' AND p.project_code<>'LEGACY'
ON DUPLICATE KEY UPDATE member_role=IF(VALUES(member_role)='MANAGER','MANAGER',member_role);
COMMIT;
ALTER TABLE pm_collab_project_member
ADD CONSTRAINT fk_project_member_source FOREIGN KEY(project_id) REFERENCES project_info(id),
ADD COLUMN manager_project_id bigint GENERATED ALWAYS AS (CASE WHEN member_role='MANAGER' THEN project_id ELSE NULL END) STORED,
ADD UNIQUE KEY uk_project_single_manager(manager_project_id);
END$$
DELIMITER ;
-- CALL cutover_project_member_ids_20261021();
-- On failure after any DDL, keep writers stopped; restore the verified backup or complete recovery.
-- Do not restart either application until the project_info FK and manager constraint are both present.