Fix ticket_watchers.ticket_id type mismatch and missing FK (#32)
Lint / PHP (phpcs PSR-12) (push) Successful in 24s
Lint / JS (eslint) (push) Successful in 9s
Lint / PHP requirements (version + extensions) (push) Successful in 27s
Lint / Notify on failure (push) Skipped
Security / PHP Security (semgrep) (push) Successful in 1m40s
Lint / Deploy (push) Successful in 2s
Lint / PHP (phpcs PSR-12) (push) Successful in 24s
Lint / JS (eslint) (push) Successful in 9s
Lint / PHP requirements (version + extensions) (push) Successful in 27s
Lint / Notify on failure (push) Skipped
Security / PHP Security (semgrep) (push) Successful in 1m40s
Lint / Deploy (push) Successful in 2s
ticket_watchers.ticket_id was int(11) while every other satellite table (ticket_comments, ticket_attachments, ticket_dependencies, custom_field_values) uses varchar(9)/varchar(10) matching tickets.ticket_id, and it had no FK constraint at all — unlike every other satellite table — so orphaned watcher rows could never be caught by referential integrity. Changed the column to varchar(9) with an ON DELETE CASCADE FK to tickets, in both 000_baseline.sql and a new idempotent 004_fix_ticket_watchers_type.sql (which also deletes any pre-existing orphaned watcher rows before adding the constraint, since orphans would otherwise make the ADD CONSTRAINT fail). Updated watch_ticket.php, NotificationHelper::notifyWatchers(), and notifications.php's audit-log JOIN to bind/compare ticket_id as a string instead of casting to int, including replacing a fragile CAST(entity_id AS UNSIGNED) with a direct string comparison. Verified against real MariaDB: applied 004 against a simulated pre-fix deployment with one valid and one orphaned watcher row — the orphan is removed, the column converts losslessly, the FK is added, and the migration is idempotent on re-run. Confirmed ON DELETE CASCADE actually removes watchers when their ticket is deleted, that inserting a watcher for a nonexistent ticket now fails with a real FK violation, and exercised the updated watch/unwatch and status-change-notification query paths end-to-end against the fixed schema. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015nCxwFFsy8ouMWzn56rPVP
This commit is contained in:
@@ -243,11 +243,12 @@ CREATE TABLE IF NOT EXISTS `ticket_templates` (
|
||||
|
||||
-- ============ ticket_watchers ============
|
||||
CREATE TABLE IF NOT EXISTS `ticket_watchers` (
|
||||
`ticket_id` int(11) NOT NULL,
|
||||
`ticket_id` varchar(9) NOT NULL,
|
||||
`user_id` int(11) NOT NULL,
|
||||
`created_at` timestamp NOT NULL DEFAULT current_timestamp(),
|
||||
PRIMARY KEY (`ticket_id`,`user_id`),
|
||||
KEY `idx_watcher_user` (`user_id`)
|
||||
KEY `idx_watcher_user` (`user_id`),
|
||||
CONSTRAINT `fk_watchers_ticket_id` FOREIGN KEY (`ticket_id`) REFERENCES `tickets` (`ticket_id`) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
|
||||
-- ============ tickets ============
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
-- Fix ticket_watchers.ticket_id type mismatch and missing FK to tickets
|
||||
--
|
||||
-- ticket_watchers.ticket_id was int(11), while every other satellite table
|
||||
-- (ticket_comments, ticket_attachments, ticket_dependencies,
|
||||
-- custom_field_values) stores it as varchar(9)/varchar(10) matching
|
||||
-- tickets.ticket_id. There was also no FK constraint at all, unlike every
|
||||
-- other satellite table, so orphaned watcher rows could never be caught by
|
||||
-- referential integrity. Ticket IDs are always 9-digit numeric strings
|
||||
-- (see TicketModel::create's sprintf('%09d', ...)), so the int -> varchar(9)
|
||||
-- conversion below is lossless for real data.
|
||||
--
|
||||
-- Safe to re-run.
|
||||
|
||||
-- Remove any watcher rows that no longer point at a real ticket (possible
|
||||
-- today precisely because there was no FK to prevent it) before adding the
|
||||
-- constraint, since orphans would make the ADD CONSTRAINT below fail.
|
||||
DELETE tw FROM `ticket_watchers` tw
|
||||
LEFT JOIN `tickets` t ON tw.`ticket_id` = t.`ticket_id`
|
||||
WHERE t.`ticket_id` IS NULL;
|
||||
|
||||
ALTER TABLE `ticket_watchers`
|
||||
MODIFY COLUMN `ticket_id` varchar(9) NOT NULL;
|
||||
|
||||
ALTER TABLE `ticket_watchers`
|
||||
DROP FOREIGN KEY IF EXISTS `fk_watchers_ticket_id`;
|
||||
|
||||
ALTER TABLE `ticket_watchers`
|
||||
ADD CONSTRAINT `fk_watchers_ticket_id` FOREIGN KEY (`ticket_id`) REFERENCES `tickets` (`ticket_id`) ON DELETE CASCADE;
|
||||
Reference in New Issue
Block a user