mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 10:31:13 -05:00
79 lines
2.8 KiB
PL/PgSQL
79 lines
2.8 KiB
PL/PgSQL
-- Phase 1: Update log_request_metadata to write to relational tables
|
|
-- Drop the specific overload with the full signature
|
|
DROP FUNCTION IF EXISTS public.log_request_metadata(
|
|
uuid, uuid, text, text, integer, integer, text, text,
|
|
text, text, uuid, uuid, text, jsonb, jsonb
|
|
);
|
|
|
|
-- Create updated function that writes to relational tables
|
|
CREATE FUNCTION public.log_request_metadata(
|
|
p_request_id uuid,
|
|
p_user_id uuid DEFAULT NULL,
|
|
p_endpoint text DEFAULT NULL,
|
|
p_method text DEFAULT NULL,
|
|
p_status_code integer DEFAULT NULL,
|
|
p_duration_ms integer DEFAULT NULL,
|
|
p_error_type text DEFAULT NULL,
|
|
p_error_message text DEFAULT NULL,
|
|
p_user_agent text DEFAULT NULL,
|
|
p_client_version text DEFAULT NULL,
|
|
p_parent_request_id uuid DEFAULT NULL,
|
|
p_trace_id uuid DEFAULT NULL,
|
|
p_error_stack text DEFAULT NULL,
|
|
p_breadcrumbs text DEFAULT '[]', -- JSON string instead of JSONB
|
|
p_environment_context text DEFAULT '{}' -- JSON string instead of JSONB
|
|
)
|
|
RETURNS void
|
|
LANGUAGE plpgsql
|
|
SECURITY DEFINER
|
|
SET search_path TO 'public'
|
|
AS $$
|
|
DECLARE
|
|
v_breadcrumb jsonb;
|
|
v_idx integer := 0;
|
|
BEGIN
|
|
-- Insert main metadata record (WITHOUT JSONB columns)
|
|
INSERT INTO request_metadata (
|
|
request_id, user_id, endpoint, method, status_code, duration_ms,
|
|
error_type, error_message, error_stack,
|
|
user_agent, client_version, parent_request_id, trace_id
|
|
) VALUES (
|
|
p_request_id, p_user_id, p_endpoint, p_method, p_status_code, p_duration_ms,
|
|
p_error_type, p_error_message, p_error_stack,
|
|
p_user_agent, p_client_version, p_parent_request_id, p_trace_id
|
|
);
|
|
|
|
-- Parse and insert breadcrumbs into relational table
|
|
IF p_breadcrumbs IS NOT NULL AND p_breadcrumbs != '[]' THEN
|
|
BEGIN
|
|
FOR v_breadcrumb IN SELECT * FROM jsonb_array_elements(p_breadcrumbs::jsonb)
|
|
LOOP
|
|
INSERT INTO request_breadcrumbs (
|
|
request_id, timestamp, category, message, level, sequence_order
|
|
) VALUES (
|
|
p_request_id,
|
|
COALESCE((v_breadcrumb->>'timestamp')::timestamptz, NOW()),
|
|
COALESCE(v_breadcrumb->>'category', 'unknown'),
|
|
COALESCE(v_breadcrumb->>'message', ''),
|
|
COALESCE(v_breadcrumb->>'level', 'info')::text,
|
|
v_idx
|
|
);
|
|
v_idx := v_idx + 1;
|
|
END LOOP;
|
|
EXCEPTION WHEN OTHERS THEN
|
|
RAISE NOTICE 'Failed to parse breadcrumbs: %', SQLERRM;
|
|
END;
|
|
END IF;
|
|
END;
|
|
$$;
|
|
|
|
-- Phase 5: Migrate contact_submissions.submitter_profile_data to FK
|
|
ALTER TABLE contact_submissions
|
|
ADD COLUMN IF NOT EXISTS submitter_profile_id uuid REFERENCES profiles(id) ON DELETE SET NULL;
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_contact_submissions_submitter_profile_id
|
|
ON contact_submissions(submitter_profile_id);
|
|
|
|
UPDATE contact_submissions
|
|
SET submitter_profile_id = user_id
|
|
WHERE user_id IS NOT NULL AND submitter_profile_id IS NULL; |