d3ro-voice/server/supabase/migrations/20260821000007_command_bootstrap_race.sql
2026-08-29 18:33:45 +09:00

63 lines
2 KiB
PL/PgSQL

BEGIN;
CREATE OR REPLACE FUNCTION public.bootstrap_custom_instructions()
RETURNS SETOF public.custom_instructions
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = ''
AS $$
DECLARE
current_user_id uuid := auth.uid();
BEGIN
IF current_user_id IS NULL THEN
RAISE EXCEPTION 'authentication_required' USING ERRCODE = '42501';
END IF;
PERFORM pg_advisory_xact_lock(hashtextextended(current_user_id::text, 73035));
INSERT INTO public.custom_instructions (
user_id, builtin_key, name, description, prompt, icon, sort_order
)
VALUES
(
current_user_id, 'translate_en', 'Translate to English',
'Translate the input into natural English.',
'Translate the following text into natural English. Return only the translation.',
'translate', 10
),
(
current_user_id, 'summarize', 'Key summary',
'Summarize the key points in no more than three lines.',
'Summarize the following text in no more than three concise lines.',
'list', 20
),
(
current_user_id, 'formal', 'Professional rewrite',
'Rewrite in a formal business style while preserving meaning.',
'Rewrite the following text in a formal business style while preserving its meaning. Return only the rewritten text.',
'briefcase', 30
),
(
current_user_id, 'explain_code', 'Explain code',
'Explain responsibilities and caveats in Korean.',
'Explain the following code in Korean, including its responsibilities and important caveats.',
'code', 40
)
ON CONFLICT DO NOTHING;
INSERT INTO public.user_settings (user_id)
VALUES (current_user_id)
ON CONFLICT (user_id) DO NOTHING;
RETURN QUERY
SELECT instruction.*
FROM public.custom_instructions AS instruction
WHERE instruction.user_id = current_user_id
ORDER BY instruction.sort_order, instruction.created_at, instruction.id;
END;
$$;
REVOKE ALL ON FUNCTION public.bootstrap_custom_instructions() FROM PUBLIC;
GRANT EXECUTE ON FUNCTION public.bootstrap_custom_instructions() TO authenticated;
COMMIT;