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:
parent
7c69284f07
commit
e3bd06ff54
4 changed files with 32 additions and 34 deletions
|
|
@ -44,3 +44,35 @@ BEGIN
|
|||
END IF;
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
@ -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;
|
||||
|
|
@ -6,11 +6,6 @@ use uuid::Uuid;
|
|||
use std::sync::Arc;
|
||||
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) {
|
||||
let state_clone = Arc::clone(&state);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue