-- Add action_type column to submission_items to preserve original submission intent ALTER TABLE submission_items ADD COLUMN IF NOT EXISTS action_type text DEFAULT 'create'; -- Add check constraint to ensure valid values ALTER TABLE submission_items ADD CONSTRAINT submission_items_action_type_check CHECK (action_type IN ('create', 'edit', 'delete')); -- Create index for better query performance CREATE INDEX IF NOT EXISTS idx_submission_items_action_type ON submission_items(action_type); -- Backfill existing records based on original_data presence UPDATE submission_items SET action_type = CASE WHEN item_type = 'photo_delete' THEN 'delete' WHEN original_data IS NULL OR original_data::text = '{}' THEN 'create' ELSE 'edit' END WHERE action_type IS NULL;