botserver/migrations/6.0.7.sql
Rodrigo Rodriguez (Pragmatismo) 72f7081acb feat(schedule): optimize set_schedule with update-first approach
Changed the set_schedule function to first attempt updating existing records before inserting new ones. This improves efficiency by avoiding unnecessary insert conflicts and subsequent updates. The logic now:
1. Tries to update matching existing schedule first
2. Only performs insert if no matching record was found
3. Maintains same functionality but with better performance
2025-11-05 09:05:25 -03:00

19 lines
667 B
SQL

-- Migration 6.0.7: Fix clicks table primary key
-- Required by Diesel before we can run other migrations
-- Create new table with proper structure
CREATE TABLE IF NOT EXISTS public.new_clicks (
id SERIAL PRIMARY KEY,
campaign_id text NOT NULL,
email text NOT NULL,
updated_at timestamptz DEFAULT now() NULL,
CONSTRAINT new_clicks_campaign_id_email_key UNIQUE (campaign_id, email)
);
-- Copy data from old table
INSERT INTO public.new_clicks (campaign_id, email, updated_at)
SELECT campaign_id, email, updated_at FROM public.clicks;
-- Drop old table and rename new one
DROP TABLE public.clicks;
ALTER TABLE public.new_clicks RENAME TO clicks;