Files
cannaiq/backend/migrations/034_dispensary_failure_tracking.sql
Kelly 66e07b2009 fix(monitor): remove non-existent worker columns from job_run_logs query
The job_run_logs table tracks scheduled job orchestration, not individual
worker jobs. Worker info (worker_id, worker_hostname) belongs on
dispensary_crawl_jobs, not job_run_logs.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 18:45:05 -07:00

57 lines
1.7 KiB
SQL

-- Migration: Add failure tracking to dispensaries
-- Tracks consecutive crawl failures and flags problematic dispensaries for review
-- Add failure tracking columns to dispensaries
ALTER TABLE dispensaries ADD COLUMN IF NOT EXISTS consecutive_failures INTEGER DEFAULT 0;
ALTER TABLE dispensaries ADD COLUMN IF NOT EXISTS last_failure_at TIMESTAMPTZ;
ALTER TABLE dispensaries ADD COLUMN IF NOT EXISTS last_failure_reason TEXT;
ALTER TABLE dispensaries ADD COLUMN IF NOT EXISTS failed_at TIMESTAMPTZ; -- NULL = active, set = failed/suspended
ALTER TABLE dispensaries ADD COLUMN IF NOT EXISTS failure_notes TEXT; -- Admin notes about the failure
-- Index for finding failed dispensaries
CREATE INDEX IF NOT EXISTS idx_dispensaries_failed
ON dispensaries(failed_at) WHERE failed_at IS NOT NULL;
-- Index for finding dispensaries with failures
CREATE INDEX IF NOT EXISTS idx_dispensaries_consecutive_failures
ON dispensaries(consecutive_failures) WHERE consecutive_failures > 0;
-- View for failed dispensaries (for admin dashboard)
CREATE OR REPLACE VIEW v_failed_dispensaries AS
SELECT
id,
name,
city,
state,
menu_url,
menu_type,
platform_dispensary_id,
consecutive_failures,
last_failure_at,
last_failure_reason,
failed_at,
failure_notes,
last_crawl_at,
updated_at
FROM dispensaries
WHERE failed_at IS NOT NULL
ORDER BY failed_at DESC;
-- View for dispensaries needing attention (high failures but not yet failed)
CREATE OR REPLACE VIEW v_dispensaries_at_risk AS
SELECT
id,
name,
city,
state,
menu_url,
menu_type,
consecutive_failures,
last_failure_at,
last_failure_reason,
last_crawl_at
FROM dispensaries
WHERE consecutive_failures >= 2
AND failed_at IS NULL
ORDER BY consecutive_failures DESC, last_failure_at DESC;