Skip to content

Add position field to multi_panel_targets table#13

Merged
BenHall-1 merged 1 commit intoTicketsBot-cloud:masterfrom
biast12:RM-86
Dec 13, 2025
Merged

Add position field to multi_panel_targets table#13
BenHall-1 merged 1 commit intoTicketsBot-cloud:masterfrom
biast12:RM-86

Conversation

@biast12
Copy link
Copy Markdown
Contributor

@biast12 biast12 commented Nov 18, 2025

Introduces a 'position' column to the multi_panel_targets table with a unique constraint per multi_panel_id. Updates queries and the Insert method to handle position, and ensures results are ordered by position.

For PR: TicketsBot-cloud/dashboard#63

Migration query:

-- Add position column if it doesn't exist
ALTER TABLE multi_panel_targets
ADD COLUMN IF NOT EXISTS position int NOT NULL DEFAULT 0;

-- Update existing records to set position based on panel_id order
-- This is a best-effort ordering for existing data
WITH ranked_panels AS (
    SELECT
        multi_panel_id,
        panel_id,
        ROW_NUMBER() OVER (PARTITION BY multi_panel_id ORDER BY panel_id) - 1 AS new_position
    FROM multi_panel_targets
)
UPDATE multi_panel_targets
SET position = ranked_panels.new_position
FROM ranked_panels
WHERE multi_panel_targets.multi_panel_id = ranked_panels.multi_panel_id
  AND multi_panel_targets.panel_id = ranked_panels.panel_id
  AND multi_panel_targets.position <> ranked_panels.new_position;

-- Add unique constraint to ensure each position is unique within a multi-panel
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint
        WHERE conname = 'multi_panel_targets_multi_panel_id_position_key'
    ) THEN
        ALTER TABLE multi_panel_targets
        ADD CONSTRAINT multi_panel_targets_multi_panel_id_position_key
        UNIQUE (multi_panel_id, position);
    END IF;
END $$;

Introduces a 'position' column to the multi_panel_targets table with a unique constraint per multi_panel_id. Updates queries and the Insert method to handle position, and ensures results are ordered by position.
@BenHall-1 BenHall-1 merged commit a4ff495 into TicketsBot-cloud:master Dec 13, 2025
@biast12 biast12 deleted the RM-86 branch December 13, 2025 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants