feat(db): add unique constraint to system_automations and fix clicks PK

- Added migration 6.0.6 to enforce a unique constraint on `(bot_id, kind, param)` in `system_automations`, preventing “no unique or exclusion constraint matching the ON CONFLICT specification” errors, and created a supporting index.
- Added migration 6.0.7 to replace the `clicks` table with a correctly defined primary key and a unique `(campaign_id, email)` constraint, satisfying Diesel
This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2025-11-05 10:19:02 -03:00
parent 7c69284f07
commit e3bd06ff54
4 changed files with 32 additions and 34 deletions

View file

@ -44,3 +44,35 @@ BEGIN
END IF; END IF;
END END
$$; $$;
-- Migration 6.0.6: Add unique constraint for system_automations
-- Fixes error: "there is no unique or exclusion constraint matching the ON CONFLICT specification"
ALTER TABLE public.system_automations
ADD CONSTRAINT system_automations_bot_kind_param_unique
UNIQUE (bot_id, kind, param);
-- Add index for the new constraint
CREATE INDEX IF NOT EXISTS idx_system_automations_bot_kind_param
ON public.system_automations (bot_id, kind, param);
-- 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;

View file

@ -1,10 +0,0 @@
-- Migration 6.0.6: Add unique constraint for system_automations
-- Fixes error: "there is no unique or exclusion constraint matching the ON CONFLICT specification"
ALTER TABLE public.system_automations
ADD CONSTRAINT system_automations_bot_kind_param_unique
UNIQUE (bot_id, kind, param);
-- Add index for the new constraint
CREATE INDEX IF NOT EXISTS idx_system_automations_bot_kind_param
ON public.system_automations (bot_id, kind, param);

View file

@ -1,19 +0,0 @@
-- 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;

View file

@ -6,11 +6,6 @@ use uuid::Uuid;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
/// Registers the `LLM` keyword for Rhai scripts.
/// Example usage inside Rhai:
/// ```rhai
/// result = LLM "Summarize the following text about AI:";
/// ```
pub fn llm_keyword(state: Arc<AppState>, _user: UserSession, engine: &mut Engine) { pub fn llm_keyword(state: Arc<AppState>, _user: UserSession, engine: &mut Engine) {
let state_clone = Arc::clone(&state); let state_clone = Arc::clone(&state);