--
-- PostgreSQL database dump
--

\restrict u76b1lsSuckyRNpZbORH9drBRaNwzQbqR7X3xYnHxUdiczMnjtBCdx8KbLlDBsP

-- Dumped from database version 15.15
-- Dumped by pg_dump version 15.15

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

--
-- Name: set_requires_recrawl(); Type: FUNCTION; Schema: public; Owner: dutchie
--

CREATE FUNCTION public.set_requires_recrawl() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
BEGIN
  IF NEW.field_name IN ('website', 'menu_url') THEN
    NEW.requires_recrawl := TRUE;
  END IF;
  RETURN NEW;
END;
$$;


ALTER FUNCTION public.set_requires_recrawl() OWNER TO dutchie;

--
-- Name: update_api_token_updated_at(); Type: FUNCTION; Schema: public; Owner: dutchie
--

CREATE FUNCTION public.update_api_token_updated_at() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
BEGIN
    NEW.updated_at = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$;


ALTER FUNCTION public.update_api_token_updated_at() OWNER TO dutchie;

--
-- Name: update_brand_scrape_jobs_updated_at(); Type: FUNCTION; Schema: public; Owner: dutchie
--

CREATE FUNCTION public.update_brand_scrape_jobs_updated_at() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
BEGIN
  NEW.updated_at = NOW();
  RETURN NEW;
END;
$$;


ALTER FUNCTION public.update_brand_scrape_jobs_updated_at() OWNER TO dutchie;

--
-- Name: update_sandbox_timestamp(); Type: FUNCTION; Schema: public; Owner: dutchie
--

CREATE FUNCTION public.update_sandbox_timestamp() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
BEGIN
  NEW.updated_at = NOW();
  RETURN NEW;
END;
$$;


ALTER FUNCTION public.update_sandbox_timestamp() OWNER TO dutchie;

--
-- Name: update_schedule_updated_at(); Type: FUNCTION; Schema: public; Owner: dutchie
--

CREATE FUNCTION public.update_schedule_updated_at() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
BEGIN
  NEW.updated_at = NOW();
  RETURN NEW;
END;
$$;


ALTER FUNCTION public.update_schedule_updated_at() OWNER TO dutchie;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: api_token_usage; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.api_token_usage (
    id integer NOT NULL,
    token_id integer,
    endpoint character varying(255) NOT NULL,
    method character varying(10) NOT NULL,
    status_code integer,
    response_time_ms integer,
    request_size integer,
    response_size integer,
    ip_address inet,
    user_agent text,
    created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);


ALTER TABLE public.api_token_usage OWNER TO dutchie;

--
-- Name: [REDACTED] Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.[REDACTED]    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.[REDACTED]OWNER TO dutchie;

--
-- Name: [REDACTED] Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.[REDACTED]OWNED BY public.api_token_usage.id;


--
-- Name: api_tokens; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.api_tokens (
    id integer NOT NULL,
    name character varying(255) NOT NULL,
    token character varying(255) NOT NULL,
    description text,
    user_id integer,
    active boolean DEFAULT true,
    rate_limit integer DEFAULT 100,
    allowed_endpoints text[],
    expires_at timestamp without time zone,
    last_used_at timestamp without time zone,
    created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);


ALTER TABLE public.api_tokens OWNER TO dutchie;

--
-- Name: api_tokens_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.api_tokens_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.api_tokens_id_seq OWNER TO dutchie;

--
-- Name: api_tokens_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.api_tokens_id_seq OWNED BY public.api_tokens.id;


--
-- Name: azdhs_list; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.azdhs_list (
    id integer NOT NULL,
    name character varying(255) NOT NULL,
    company_name character varying(255),
    slug character varying(255),
    address character varying(500),
    city character varying(100),
    state character varying(2) DEFAULT 'AZ'::character varying,
    zip character varying(10),
    phone character varying(20),
    email character varying(255),
    status_line text,
    azdhs_url text,
    latitude numeric(10,8),
    longitude numeric(11,8),
    created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    website text,
    dba_name character varying(255),
    google_rating numeric(2,1),
    google_review_count integer
);


ALTER TABLE public.azdhs_list OWNER TO dutchie;

--
-- Name: azdhs_list_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.azdhs_list_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.azdhs_list_id_seq OWNER TO dutchie;

--
-- Name: azdhs_list_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.azdhs_list_id_seq OWNED BY public.azdhs_list.id;


--
-- Name: batch_history; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.batch_history (
    id integer NOT NULL,
    product_id integer,
    thc_percentage numeric(5,2),
    cbd_percentage numeric(5,2),
    terpenes text[],
    strain_type character varying(100),
    recorded_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);


ALTER TABLE public.batch_history OWNER TO dutchie;

--
-- Name: batch_history_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.batch_history_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.batch_history_id_seq OWNER TO dutchie;

--
-- Name: batch_history_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.batch_history_id_seq OWNED BY public.batch_history.id;


--
-- Name: brand_history; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.brand_history (
    id integer NOT NULL,
    dispensary_id integer NOT NULL,
    brand_name character varying(255) NOT NULL,
    event_type character varying(20) NOT NULL,
    event_at timestamp with time zone DEFAULT now(),
    product_count integer,
    metadata jsonb
);


ALTER TABLE public.brand_history OWNER TO dutchie;

--
-- Name: brand_history_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.brand_history_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.brand_history_id_seq OWNER TO dutchie;

--
-- Name: brand_history_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.brand_history_id_seq OWNED BY public.brand_history.id;


--
-- Name: brand_scrape_jobs; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.brand_scrape_jobs (
    id integer NOT NULL,
    dispensary_id integer NOT NULL,
    brand_slug text NOT NULL,
    brand_name text NOT NULL,
    status text DEFAULT 'pending'::text NOT NULL,
    worker_id text,
    started_at timestamp without time zone,
    completed_at timestamp without time zone,
    products_found integer DEFAULT 0,
    products_saved integer DEFAULT 0,
    error_message text,
    retry_count integer DEFAULT 0,
    created_at timestamp without time zone DEFAULT now(),
    updated_at timestamp without time zone DEFAULT now()
);


ALTER TABLE public.brand_scrape_jobs OWNER TO dutchie;

--
-- Name: brand_scrape_jobs_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.brand_scrape_jobs_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.brand_scrape_jobs_id_seq OWNER TO dutchie;

--
-- Name: brand_scrape_jobs_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.brand_scrape_jobs_id_seq OWNED BY public.brand_scrape_jobs.id;


--
-- Name: brands; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.brands (
    id integer NOT NULL,
    store_id integer NOT NULL,
    name character varying(255) NOT NULL,
    created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    first_seen_at timestamp with time zone DEFAULT now(),
    last_seen_at timestamp with time zone DEFAULT now(),
    dispensary_id integer
);


ALTER TABLE public.brands OWNER TO dutchie;

--
-- Name: brands_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.brands_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.brands_id_seq OWNER TO dutchie;

--
-- Name: brands_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.brands_id_seq OWNED BY public.brands.id;


--
-- Name: campaign_products; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.campaign_products (
    id integer NOT NULL,
    campaign_id integer,
    product_id integer,
    display_order integer DEFAULT 0,
    created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);


ALTER TABLE public.campaign_products OWNER TO dutchie;

--
-- Name: campaign_products_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.campaign_products_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.campaign_products_id_seq OWNER TO dutchie;

--
-- Name: campaign_products_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.campaign_products_id_seq OWNED BY public.campaign_products.id;


--
-- Name: campaigns; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.campaigns (
    id integer NOT NULL,
    name character varying(255) NOT NULL,
    slug character varying(255) NOT NULL,
    description text,
    display_style character varying(50) DEFAULT 'grid'::character varying,
    active boolean DEFAULT true,
    start_date timestamp without time zone,
    end_date timestamp without time zone,
    created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);


ALTER TABLE public.campaigns OWNER TO dutchie;

--
-- Name: campaigns_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.campaigns_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.campaigns_id_seq OWNER TO dutchie;

--
-- Name: campaigns_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.campaigns_id_seq OWNED BY public.campaigns.id;


--
-- Name: categories; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.categories (
    id integer NOT NULL,
    store_id integer,
    name character varying(255) NOT NULL,
    slug character varying(255) NOT NULL,
    dutchie_url text NOT NULL,
    scrape_enabled boolean DEFAULT true,
    last_scraped_at timestamp without time zone,
    created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    parent_id integer,
    display_order integer DEFAULT 0,
    description text,
    path character varying(500),
    dispensary_id integer
);


ALTER TABLE public.categories OWNER TO dutchie;

--
-- Name: categories_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.categories_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.categories_id_seq OWNER TO dutchie;

--
-- Name: categories_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.categories_id_seq OWNED BY public.categories.id;


--
-- Name: clicks; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.clicks (
    id integer NOT NULL,
    product_id integer,
    campaign_id integer,
    ip_address character varying(45),
    user_agent text,
    referrer text,
    clicked_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);


ALTER TABLE public.clicks OWNER TO dutchie;

--
-- Name: clicks_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.clicks_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.clicks_id_seq OWNER TO dutchie;

--
-- Name: clicks_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.clicks_id_seq OWNED BY public.clicks.id;


--
-- Name: crawl_jobs; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.crawl_jobs (
    id integer NOT NULL,
    store_id integer NOT NULL,
    job_type character varying(50) DEFAULT 'full_crawl'::character varying NOT NULL,
    trigger_type character varying(50) DEFAULT 'scheduled'::character varying NOT NULL,
    status character varying(20) DEFAULT 'pending'::character varying NOT NULL,
    priority integer DEFAULT 0,
    scheduled_at timestamp with time zone DEFAULT now() NOT NULL,
    started_at timestamp with time zone,
    completed_at timestamp with time zone,
    products_found integer,
    products_new integer,
    products_updated integer,
    error_message text,
    worker_id character varying(100),
    metadata jsonb DEFAULT '{}'::jsonb,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    orchestrator_run_id uuid,
    detection_result jsonb,
    in_stock_count integer,
    out_of_stock_count integer,
    limited_count integer,
    unknown_count integer,
    availability_changed_count integer,
    CONSTRAINT chk_crawl_job_status CHECK (((status)::text = ANY ((ARRAY['pending'::character varying, 'running'::character varying, 'completed'::character varying, 'failed'::character varying, 'cancelled'::character varying])::text[])))
);


ALTER TABLE public.crawl_jobs OWNER TO dutchie;

--
-- Name: COLUMN crawl_jobs.orchestrator_run_id; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.crawl_jobs.orchestrator_run_id IS 'Groups related jobs from same orchestrator run';


--
-- Name: crawl_jobs_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.crawl_jobs_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.crawl_jobs_id_seq OWNER TO dutchie;

--
-- Name: crawl_jobs_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.crawl_jobs_id_seq OWNED BY public.crawl_jobs.id;


--
-- Name: crawler_schedule; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.crawler_schedule (
    id integer NOT NULL,
    schedule_type character varying(50) NOT NULL,
    enabled boolean DEFAULT true NOT NULL,
    interval_hours integer,
    run_time time without time zone,
    description text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);


ALTER TABLE public.crawler_schedule OWNER TO dutchie;

--
-- Name: dispensaries; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.dispensaries (
    id integer NOT NULL,
    azdhs_id integer,
    name character varying(255) NOT NULL,
    company_name character varying(255),
    address character varying(500) NOT NULL,
    city character varying(100) NOT NULL,
    state character varying(2) NOT NULL,
    zip character varying(10),
    status_line character varying(100),
    azdhs_url text,
    latitude numeric(10,8),
    longitude numeric(11,8),
    dba_name character varying(255),
    phone character varying(20),
    email character varying(255),
    website text,
    google_rating numeric(2,1),
    google_review_count integer,
    menu_url text,
    scraper_template character varying(100),
    scraper_config jsonb,
    last_menu_scrape timestamp without time zone,
    menu_scrape_status character varying(50) DEFAULT 'pending'::character varying,
    slug character varying(255) NOT NULL,
    created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    menu_provider character varying(50),
    menu_provider_confidence smallint DEFAULT 0,
    crawler_mode character varying(20) DEFAULT 'production'::character varying,
    crawler_status character varying(30) DEFAULT 'idle'::character varying,
    last_menu_error_at timestamp with time zone,
    last_error_message text,
    provider_detection_data jsonb DEFAULT '{}'::jsonb,
    product_provider character varying(50),
    product_confidence smallint DEFAULT 0,
    product_crawler_mode character varying(20) DEFAULT 'sandbox'::character varying,
    last_product_scan_at timestamp with time zone,
    product_detection_data jsonb DEFAULT '{}'::jsonb,
    specials_provider character varying(50),
    specials_confidence smallint DEFAULT 0,
    specials_crawler_mode character varying(20) DEFAULT 'sandbox'::character varying,
    last_specials_scan_at timestamp with time zone,
    specials_detection_data jsonb DEFAULT '{}'::jsonb,
    brand_provider character varying(50),
    brand_confidence smallint DEFAULT 0,
    brand_crawler_mode character varying(20) DEFAULT 'sandbox'::character varying,
    last_brand_scan_at timestamp with time zone,
    brand_detection_data jsonb DEFAULT '{}'::jsonb,
    metadata_provider character varying(50),
    metadata_confidence smallint DEFAULT 0,
    metadata_crawler_mode character varying(20) DEFAULT 'sandbox'::character varying,
    last_metadata_scan_at timestamp with time zone,
    metadata_detection_data jsonb DEFAULT '{}'::jsonb,
    provider_type character varying(50) DEFAULT 'unknown'::character varying,
    scrape_enabled boolean DEFAULT false,
    last_crawl_at timestamp with time zone,
    next_crawl_at timestamp with time zone,
    crawl_status character varying(50) DEFAULT 'pending'::character varying,
    crawl_error text,
    consecutive_failures integer DEFAULT 0,
    total_crawls integer DEFAULT 0,
    successful_crawls integer DEFAULT 0,
    CONSTRAINT chk_crawler_mode CHECK (((crawler_mode)::text = ANY ((ARRAY['production'::character varying, 'sandbox'::character varying])::text[]))),
    CONSTRAINT chk_crawler_status CHECK (((crawler_status)::text = ANY ((ARRAY['idle'::character varying, 'queued_detection'::character varying, 'queued_crawl'::character varying, 'running'::character varying, 'ok'::character varying, 'error_needs_review'::character varying])::text[]))),
    CONSTRAINT chk_provider_confidence CHECK (((menu_provider_confidence >= 0) AND (menu_provider_confidence <= 100)))
);


ALTER TABLE public.dispensaries OWNER TO dutchie;

--
-- Name: COLUMN dispensaries.menu_provider; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.dispensaries.menu_provider IS 'Detected menu platform: dutchie, treez, jane, weedmaps, etc.';


--
-- Name: COLUMN dispensaries.menu_provider_confidence; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.dispensaries.menu_provider_confidence IS 'Confidence score 0-100 for provider detection';


--
-- Name: COLUMN dispensaries.crawler_mode; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.dispensaries.crawler_mode IS 'production = stable templates, sandbox = learning mode';


--
-- Name: COLUMN dispensaries.crawler_status; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.dispensaries.crawler_status IS 'Current state in crawl pipeline';


--
-- Name: COLUMN dispensaries.provider_detection_data; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.dispensaries.provider_detection_data IS 'JSON blob with detection signals and history';


--
-- Name: COLUMN dispensaries.product_provider; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.dispensaries.product_provider IS 'Provider for product intelligence (dutchie, treez, jane, etc.)';


--
-- Name: COLUMN dispensaries.product_crawler_mode; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.dispensaries.product_crawler_mode IS 'production or sandbox mode for product crawling';


--
-- Name: COLUMN dispensaries.specials_provider; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.dispensaries.specials_provider IS 'Provider for specials/deals intelligence';


--
-- Name: COLUMN dispensaries.brand_provider; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.dispensaries.brand_provider IS 'Provider for brand intelligence';


--
-- Name: COLUMN dispensaries.metadata_provider; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.dispensaries.metadata_provider IS 'Provider for metadata/taxonomy intelligence';


--
-- Name: store_crawl_schedule; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.store_crawl_schedule (
    id integer NOT NULL,
    store_id integer NOT NULL,
    enabled boolean DEFAULT true NOT NULL,
    interval_hours integer,
    daily_special_enabled boolean DEFAULT true,
    daily_special_time time without time zone,
    priority integer DEFAULT 0,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    last_status character varying(50),
    last_summary text,
    last_run_at timestamp with time zone,
    last_error text
);


ALTER TABLE public.store_crawl_schedule OWNER TO dutchie;

--
-- Name: COLUMN store_crawl_schedule.last_status; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.store_crawl_schedule.last_status IS 'Orchestrator result status: success, error, sandbox_only, detection_only';


--
-- Name: COLUMN store_crawl_schedule.last_summary; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.store_crawl_schedule.last_summary IS 'Human-readable summary of last orchestrator run';


--
-- Name: COLUMN store_crawl_schedule.last_run_at; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.store_crawl_schedule.last_run_at IS 'When orchestrator last ran for this store';


--
-- Name: stores; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.stores (
    id integer NOT NULL,
    name character varying(255) NOT NULL,
    slug character varying(255) NOT NULL,
    dutchie_url text NOT NULL,
    active boolean DEFAULT true,
    scrape_enabled boolean DEFAULT true,
    last_scraped_at timestamp without time zone,
    created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    logo_url text,
    timezone character varying(50) DEFAULT 'America/Phoenix'::character varying,
    dispensary_id integer
);


ALTER TABLE public.stores OWNER TO dutchie;

--
-- Name: COLUMN stores.dispensary_id; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.stores.dispensary_id IS 'FK to dispensaries table (master AZDHS directory)';


--
-- Name: crawl_schedule_status; Type: VIEW; Schema: public; Owner: dutchie
--

CREATE VIEW public.crawl_schedule_status AS
 SELECT s.id AS store_id,
    s.name AS store_name,
    s.slug AS store_slug,
    s.timezone,
    s.active,
    s.scrape_enabled,
    s.last_scraped_at,
    s.dispensary_id,
    d.name AS dispensary_name,
    d.company_name AS dispensary_company,
    d.city AS dispensary_city,
    d.state AS dispensary_state,
    d.slug AS dispensary_slug,
    d.address AS dispensary_address,
    d.menu_url AS dispensary_menu_url,
    d.product_provider,
    d.product_confidence,
    d.product_crawler_mode,
    COALESCE(scs.enabled, true) AS schedule_enabled,
    COALESCE(scs.interval_hours, cs_global.interval_hours, 4) AS interval_hours,
    COALESCE(scs.daily_special_enabled, true) AS daily_special_enabled,
    COALESCE(scs.daily_special_time, '00:01:00'::time without time zone) AS daily_special_time,
    COALESCE(scs.priority, 0) AS priority,
    scs.last_status,
    scs.last_summary,
    scs.last_run_at AS schedule_last_run,
    scs.last_error,
        CASE
            WHEN (s.last_scraped_at IS NULL) THEN now()
            ELSE ((s.last_scraped_at + ((COALESCE(scs.interval_hours, cs_global.interval_hours, 4) || ' hours'::text))::interval))::timestamp with time zone
        END AS next_scheduled_run,
    cj.id AS latest_job_id,
    cj.status AS latest_job_status,
    cj.job_type AS latest_job_type,
    cj.trigger_type AS latest_job_trigger,
    cj.started_at AS latest_job_started,
    cj.completed_at AS latest_job_completed,
    cj.products_found AS latest_products_found,
    cj.products_new AS latest_products_new,
    cj.products_updated AS latest_products_updated,
    cj.error_message AS latest_job_error
   FROM ((((public.stores s
     LEFT JOIN public.dispensaries d ON ((d.id = s.dispensary_id)))
     LEFT JOIN public.store_crawl_schedule scs ON ((scs.store_id = s.id)))
     LEFT JOIN public.crawler_schedule cs_global ON (((cs_global.schedule_type)::text = 'global_interval'::text)))
     LEFT JOIN LATERAL ( SELECT cj2.id,
            cj2.store_id,
            cj2.job_type,
            cj2.trigger_type,
            cj2.status,
            cj2.priority,
            cj2.scheduled_at,
            cj2.started_at,
            cj2.completed_at,
            cj2.products_found,
            cj2.products_new,
            cj2.products_updated,
            cj2.error_message,
            cj2.worker_id,
            cj2.metadata,
            cj2.created_at,
            cj2.updated_at,
            cj2.orchestrator_run_id,
            cj2.detection_result
           FROM public.crawl_jobs cj2
          WHERE (cj2.store_id = s.id)
          ORDER BY cj2.created_at DESC
         LIMIT 1) cj ON (true))
  WHERE (s.active = true);


ALTER TABLE public.crawl_schedule_status OWNER TO dutchie;

--
-- Name: crawler_sandboxes; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.crawler_sandboxes (
    id integer NOT NULL,
    dispensary_id integer NOT NULL,
    suspected_menu_provider character varying(50),
    mode character varying(30) DEFAULT 'detection'::character varying NOT NULL,
    raw_html_location text,
    screenshot_location text,
    analysis_json jsonb DEFAULT '{}'::jsonb,
    urls_tested jsonb DEFAULT '[]'::jsonb,
    menu_entry_points jsonb DEFAULT '[]'::jsonb,
    detection_signals jsonb DEFAULT '{}'::jsonb,
    status character varying(30) DEFAULT 'pending'::character varying NOT NULL,
    confidence_score smallint DEFAULT 0,
    failure_reason text,
    human_review_notes text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    analyzed_at timestamp with time zone,
    reviewed_at timestamp with time zone,
    category character varying(30) DEFAULT 'product'::character varying,
    template_name character varying(100),
    quality_score smallint DEFAULT 0,
    products_extracted integer DEFAULT 0,
    fields_missing integer DEFAULT 0,
    error_count integer DEFAULT 0,
    CONSTRAINT chk_sandbox_mode CHECK (((mode)::text = ANY ((ARRAY['detection'::character varying, 'template_learning'::character varying, 'validation'::character varying])::text[]))),
    CONSTRAINT chk_sandbox_status CHECK (((status)::text = ANY ((ARRAY['pending'::character varying, 'analyzing'::character varying, 'template_ready'::character varying, 'needs_human_review'::character varying, 'moved_to_production'::character varying, 'failed'::character varying])::text[])))
);


ALTER TABLE public.crawler_sandboxes OWNER TO dutchie;

--
-- Name: TABLE crawler_sandboxes; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON TABLE public.crawler_sandboxes IS 'Learning/testing environment for unknown menu providers';


--
-- Name: COLUMN crawler_sandboxes.category; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.crawler_sandboxes.category IS 'Intelligence category: product, specials, brand, metadata';


--
-- Name: COLUMN crawler_sandboxes.quality_score; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.crawler_sandboxes.quality_score IS 'Quality score 0-100 for sandbox run results';


--
-- Name: crawler_sandboxes_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.crawler_sandboxes_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.crawler_sandboxes_id_seq OWNER TO dutchie;

--
-- Name: crawler_sandboxes_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.crawler_sandboxes_id_seq OWNED BY public.crawler_sandboxes.id;


--
-- Name: crawler_schedule_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.crawler_schedule_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.crawler_schedule_id_seq OWNER TO dutchie;

--
-- Name: crawler_schedule_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.crawler_schedule_id_seq OWNED BY public.crawler_schedule.id;


--
-- Name: crawler_templates; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.crawler_templates (
    id integer NOT NULL,
    provider character varying(50) NOT NULL,
    name character varying(100) NOT NULL,
    version integer DEFAULT 1,
    is_active boolean DEFAULT true NOT NULL,
    is_default_for_provider boolean DEFAULT false,
    selector_config jsonb DEFAULT '{}'::jsonb NOT NULL,
    navigation_config jsonb DEFAULT '{}'::jsonb,
    transform_config jsonb DEFAULT '{}'::jsonb,
    validation_rules jsonb DEFAULT '{}'::jsonb,
    test_urls jsonb DEFAULT '[]'::jsonb,
    expected_structure jsonb DEFAULT '{}'::jsonb,
    dispensaries_using integer DEFAULT 0,
    success_rate numeric(5,2) DEFAULT 0,
    last_successful_crawl timestamp with time zone,
    last_failed_crawl timestamp with time zone,
    notes text,
    created_by character varying(100),
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    environment character varying(20) DEFAULT 'production'::character varying
);


ALTER TABLE public.crawler_templates OWNER TO dutchie;

--
-- Name: TABLE crawler_templates; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON TABLE public.crawler_templates IS 'Reusable scraping configurations per menu provider';


--
-- Name: COLUMN crawler_templates.environment; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.crawler_templates.environment IS 'Template environment: production or sandbox';


--
-- Name: crawler_templates_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.crawler_templates_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.crawler_templates_id_seq OWNER TO dutchie;

--
-- Name: crawler_templates_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.crawler_templates_id_seq OWNED BY public.crawler_templates.id;


--
-- Name: products; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.products (
    id integer NOT NULL,
    store_id integer,
    category_id integer,
    dutchie_product_id character varying(255),
    name character varying(500) NOT NULL,
    slug character varying(500) NOT NULL,
    description text,
    price numeric(10,2),
    original_price numeric(10,2),
    strain_type character varying(100),
    thc_percentage numeric(10,4),
    cbd_percentage numeric(10,4),
    brand character varying(255),
    weight character varying(100),
    image_url text,
    local_image_path text,
    dutchie_url text NOT NULL,
    in_stock boolean DEFAULT true,
    is_special boolean DEFAULT false,
    metadata jsonb,
    first_seen_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    last_seen_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    dispensary_id integer,
    variant character varying(255),
    special_ends_at timestamp without time zone,
    special_text text,
    special_type character varying(100),
    terpenes text[],
    effects text[],
    flavors text[],
    regular_price numeric(10,2),
    sale_price numeric(10,2),
    stock_quantity integer,
    stock_status text,
    discount_type character varying(50),
    discount_value character varying(100),
    availability_status character varying(20) DEFAULT 'unknown'::character varying,
    availability_raw jsonb,
    last_seen_in_stock_at timestamp with time zone,
    last_seen_out_of_stock_at timestamp with time zone,
    enterprise_product_id character varying(255),
    sku character varying(100),
    brand_external_id character varying(255),
    brand_logo_url text,
    subcategory character varying(100),
    canonical_category character varying(255),
    rec_price numeric(10,2),
    med_price numeric(10,2),
    rec_special_price numeric(10,2),
    med_special_price numeric(10,2),
    is_on_special boolean DEFAULT false,
    special_name text,
    discount_percent numeric(10,2),
    special_data jsonb,
    inventory_quantity integer,
    inventory_available integer,
    is_below_threshold boolean DEFAULT false,
    status character varying(20) DEFAULT 'Active'::character varying,
    cannabinoids jsonb,
    weight_mg integer,
    net_weight_value numeric(10,2),
    net_weight_unit character varying(20),
    options text[],
    raw_options text[],
    additional_images text[],
    is_featured boolean DEFAULT false,
    medical_only boolean DEFAULT false,
    rec_only boolean DEFAULT false,
    source_created_at timestamp with time zone,
    source_updated_at timestamp with time zone,
    raw_data jsonb,
    external_id character varying(255)
);


ALTER TABLE public.products OWNER TO dutchie;

--
-- Name: COLUMN products.availability_status; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.products.availability_status IS 'Normalized status: in_stock, out_of_stock, limited, unknown';


--
-- Name: COLUMN products.availability_raw; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.products.availability_raw IS 'Raw availability payload from provider for debugging';


--
-- Name: COLUMN products.last_seen_in_stock_at; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.products.last_seen_in_stock_at IS 'Last time product was seen in stock';


--
-- Name: COLUMN products.last_seen_out_of_stock_at; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.products.last_seen_out_of_stock_at IS 'Last time product was seen out of stock';


--
-- Name: COLUMN products.enterprise_product_id; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.products.enterprise_product_id IS 'Dutchie enterpriseProductId - shared across dispensaries';


--
-- Name: COLUMN products.brand_external_id; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.products.brand_external_id IS 'Dutchie brand.id / brandId field';


--
-- Name: COLUMN products.canonical_category; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.products.canonical_category IS 'Dutchie POSMetaData.canonicalCategory - pipe-separated category path';


--
-- Name: COLUMN products.rec_special_price; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.products.rec_special_price IS 'Dutchie recSpecialPrices[0] - discounted recreational price';


--
-- Name: COLUMN products.special_data; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.products.special_data IS 'Full Dutchie specialData JSONB including all active specials';


--
-- Name: COLUMN products.cannabinoids; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.products.cannabinoids IS 'Full Dutchie cannabinoidsV2 array as JSONB';


--
-- Name: COLUMN products.raw_data; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON COLUMN public.products.raw_data IS 'Complete Dutchie GraphQL response for this product';


--
-- Name: current_specials; Type: VIEW; Schema: public; Owner: dutchie
--

CREATE VIEW public.current_specials AS
 SELECT p.id,
    p.store_id,
    p.name,
    p.brand,
    p.subcategory,
    p.strain_type,
    p.rec_price,
    p.rec_special_price,
    p.discount_percent,
    p.special_name,
    p.image_url,
    p.slug,
    p.thc_percentage,
    p.cbd_percentage,
    s.name AS store_name,
    s.slug AS store_slug
   FROM (public.products p
     JOIN public.stores s ON ((s.id = p.store_id)))
  WHERE ((p.is_on_special = true) AND ((p.status)::text = 'Active'::text) AND (p.rec_special_price IS NOT NULL));


ALTER TABLE public.current_specials OWNER TO dutchie;

--
-- Name: derived_brands; Type: VIEW; Schema: public; Owner: dutchie
--

CREATE VIEW public.derived_brands AS
 SELECT p.store_id,
    p.brand AS brand_name,
    p.brand_external_id,
    max(p.brand_logo_url) AS brand_logo_url,
    count(*) AS product_count,
    count(*) FILTER (WHERE ((p.status)::text = 'Active'::text)) AS active_count,
    count(*) FILTER (WHERE p.is_on_special) AS special_count,
    min(p.rec_price) AS min_price,
    max(p.rec_price) AS max_price,
    avg(p.rec_price) AS avg_price,
    array_agg(DISTINCT p.subcategory) FILTER (WHERE (p.subcategory IS NOT NULL)) AS categories,
    max(p.updated_at) AS last_updated
   FROM public.products p
  WHERE (p.brand IS NOT NULL)
  GROUP BY p.store_id, p.brand, p.brand_external_id;


ALTER TABLE public.derived_brands OWNER TO dutchie;

--
-- Name: derived_categories; Type: VIEW; Schema: public; Owner: dutchie
--

CREATE VIEW public.derived_categories AS
 SELECT p.store_id,
    p.subcategory AS category_name,
    count(*) AS product_count,
    count(*) FILTER (WHERE ((p.status)::text = 'Active'::text)) AS active_count,
    count(*) FILTER (WHERE p.is_on_special) AS special_count,
    min(p.rec_price) AS min_price,
    max(p.rec_price) AS max_price,
    array_agg(DISTINCT p.brand) FILTER (WHERE (p.brand IS NOT NULL)) AS brands,
    max(p.updated_at) AS last_updated
   FROM public.products p
  WHERE (p.subcategory IS NOT NULL)
  GROUP BY p.store_id, p.subcategory;


ALTER TABLE public.derived_categories OWNER TO dutchie;

--
-- Name: dispensaries_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.dispensaries_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.dispensaries_id_seq OWNER TO dutchie;

--
-- Name: dispensaries_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.dispensaries_id_seq OWNED BY public.dispensaries.id;


--
-- Name: dispensary_brand_stats; Type: VIEW; Schema: public; Owner: dutchie
--

CREATE VIEW public.dispensary_brand_stats AS
 SELECT d.id AS dispensary_id,
    COALESCE(d.dba_name, d.name) AS dispensary_name,
    count(DISTINCT p.brand) FILTER (WHERE (p.last_seen_at >= (now() - '7 days'::interval))) AS current_brands,
    count(DISTINCT p.brand) AS total_brands_ever,
    ( SELECT count(DISTINCT bh.brand_name) AS count
           FROM public.brand_history bh
          WHERE ((bh.dispensary_id = d.id) AND ((bh.event_type)::text = 'added'::text) AND (bh.event_at >= (now() - '7 days'::interval)))) AS new_brands_7d,
    ( SELECT count(DISTINCT bh.brand_name) AS count
           FROM public.brand_history bh
          WHERE ((bh.dispensary_id = d.id) AND ((bh.event_type)::text = 'dropped'::text) AND (bh.event_at >= (now() - '7 days'::interval)))) AS dropped_brands_7d
   FROM (public.dispensaries d
     LEFT JOIN public.products p ON ((p.dispensary_id = d.id)))
  GROUP BY d.id, d.dba_name, d.name;


ALTER TABLE public.dispensary_brand_stats OWNER TO dutchie;

--
-- Name: dispensary_changes; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.dispensary_changes (
    id integer NOT NULL,
    dispensary_id integer NOT NULL,
    field_name character varying(100) NOT NULL,
    old_value text,
    new_value text,
    source character varying(50) NOT NULL,
    confidence_score character varying(20),
    change_notes text,
    status character varying(20) DEFAULT 'pending'::character varying NOT NULL,
    requires_recrawl boolean DEFAULT false,
    created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    reviewed_at timestamp without time zone,
    reviewed_by integer,
    rejection_reason text,
    CONSTRAINT dispensary_changes_status_check CHECK (((status)::text = ANY ((ARRAY['pending'::character varying, 'approved'::character varying, 'rejected'::character varying])::text[])))
);


ALTER TABLE public.dispensary_changes OWNER TO dutchie;

--
-- Name: dispensary_changes_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.dispensary_changes_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.dispensary_changes_id_seq OWNER TO dutchie;

--
-- Name: dispensary_changes_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.dispensary_changes_id_seq OWNED BY public.dispensary_changes.id;


--
-- Name: dispensary_crawl_jobs; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.dispensary_crawl_jobs (
    id integer NOT NULL,
    dispensary_id integer NOT NULL,
    schedule_id integer,
    job_type character varying(50) DEFAULT 'orchestrator'::character varying NOT NULL,
    trigger_type character varying(50) DEFAULT 'scheduled'::character varying NOT NULL,
    status character varying(20) DEFAULT 'pending'::character varying NOT NULL,
    priority integer DEFAULT 0,
    scheduled_at timestamp with time zone DEFAULT now(),
    started_at timestamp with time zone,
    completed_at timestamp with time zone,
    duration_ms integer,
    detection_ran boolean DEFAULT false,
    crawl_ran boolean DEFAULT false,
    crawl_type character varying(20),
    products_found integer,
    products_new integer,
    products_updated integer,
    detected_provider character varying(50),
    detected_confidence smallint,
    detected_mode character varying(20),
    error_message text,
    worker_id character varying(100),
    run_id uuid,
    created_at timestamp with time zone DEFAULT now(),
    updated_at timestamp with time zone DEFAULT now(),
    in_stock_count integer,
    out_of_stock_count integer,
    limited_count integer,
    unknown_count integer,
    availability_changed_count integer
);


ALTER TABLE public.dispensary_crawl_jobs OWNER TO dutchie;

--
-- Name: dispensary_crawl_jobs_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.dispensary_crawl_jobs_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.dispensary_crawl_jobs_id_seq OWNER TO dutchie;

--
-- Name: dispensary_crawl_jobs_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.dispensary_crawl_jobs_id_seq OWNED BY public.dispensary_crawl_jobs.id;


--
-- Name: dispensary_crawl_schedule; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.dispensary_crawl_schedule (
    id integer NOT NULL,
    dispensary_id integer NOT NULL,
    is_active boolean DEFAULT true NOT NULL,
    interval_minutes integer DEFAULT 240 NOT NULL,
    priority integer DEFAULT 0 NOT NULL,
    last_run_at timestamp with time zone,
    next_run_at timestamp with time zone,
    last_status character varying(50),
    last_summary text,
    last_error text,
    last_duration_ms integer,
    consecutive_failures integer DEFAULT 0,
    total_runs integer DEFAULT 0,
    successful_runs integer DEFAULT 0,
    created_at timestamp with time zone DEFAULT now(),
    updated_at timestamp with time zone DEFAULT now()
);


ALTER TABLE public.dispensary_crawl_schedule OWNER TO dutchie;

--
-- Name: dispensary_crawl_schedule_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.dispensary_crawl_schedule_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.dispensary_crawl_schedule_id_seq OWNER TO dutchie;

--
-- Name: dispensary_crawl_schedule_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.dispensary_crawl_schedule_id_seq OWNED BY public.dispensary_crawl_schedule.id;


--
-- Name: dispensary_crawl_status; Type: VIEW; Schema: public; Owner: dutchie
--

CREATE VIEW public.dispensary_crawl_status AS
 SELECT d.id AS dispensary_id,
    COALESCE(d.dba_name, d.name) AS dispensary_name,
    d.city,
    d.state,
    d.slug,
    d.website,
    d.menu_url,
    COALESCE(d.product_provider, d.provider_type) AS product_provider,
    d.provider_type,
    d.product_confidence,
    d.product_crawler_mode,
    d.last_product_scan_at,
    COALESCE(dcs.is_active, d.scrape_enabled, false) AS schedule_active,
    COALESCE(dcs.interval_minutes, 240) AS interval_minutes,
    COALESCE(dcs.priority, 0) AS priority,
    COALESCE(dcs.last_run_at, d.last_crawl_at) AS last_run_at,
    COALESCE(dcs.next_run_at, d.next_crawl_at) AS next_run_at,
    COALESCE(dcs.last_status, d.crawl_status) AS last_status,
    dcs.last_summary,
    COALESCE(dcs.last_error, d.crawl_error) AS last_error,
    COALESCE(dcs.consecutive_failures, d.consecutive_failures, 0) AS consecutive_failures,
    COALESCE(dcs.total_runs, d.total_crawls, 0) AS total_runs,
    COALESCE(dcs.successful_runs, d.successful_crawls, 0) AS successful_runs,
    dcj.id AS latest_job_id,
    dcj.job_type AS latest_job_type,
    dcj.status AS latest_job_status,
    dcj.started_at AS latest_job_started,
    dcj.products_found AS latest_products_found
   FROM ((public.dispensaries d
     LEFT JOIN public.dispensary_crawl_schedule dcs ON ((dcs.dispensary_id = d.id)))
     LEFT JOIN LATERAL ( SELECT dispensary_crawl_jobs.id,
            dispensary_crawl_jobs.dispensary_id,
            dispensary_crawl_jobs.schedule_id,
            dispensary_crawl_jobs.job_type,
            dispensary_crawl_jobs.trigger_type,
            dispensary_crawl_jobs.status,
            dispensary_crawl_jobs.priority,
            dispensary_crawl_jobs.scheduled_at,
            dispensary_crawl_jobs.started_at,
            dispensary_crawl_jobs.completed_at,
            dispensary_crawl_jobs.duration_ms,
            dispensary_crawl_jobs.detection_ran,
            dispensary_crawl_jobs.crawl_ran,
            dispensary_crawl_jobs.crawl_type,
            dispensary_crawl_jobs.products_found,
            dispensary_crawl_jobs.products_new,
            dispensary_crawl_jobs.products_updated,
            dispensary_crawl_jobs.detected_provider,
            dispensary_crawl_jobs.detected_confidence,
            dispensary_crawl_jobs.detected_mode,
            dispensary_crawl_jobs.error_message,
            dispensary_crawl_jobs.worker_id,
            dispensary_crawl_jobs.run_id,
            dispensary_crawl_jobs.created_at,
            dispensary_crawl_jobs.updated_at,
            dispensary_crawl_jobs.in_stock_count,
            dispensary_crawl_jobs.out_of_stock_count,
            dispensary_crawl_jobs.limited_count,
            dispensary_crawl_jobs.unknown_count,
            dispensary_crawl_jobs.availability_changed_count
           FROM public.dispensary_crawl_jobs
          WHERE (dispensary_crawl_jobs.dispensary_id = d.id)
          ORDER BY dispensary_crawl_jobs.created_at DESC
         LIMIT 1) dcj ON (true))
  ORDER BY
        CASE
            WHEN (d.scrape_enabled = true) THEN 0
            ELSE 1
        END, COALESCE(dcs.priority, 0) DESC, COALESCE(d.dba_name, d.name);


ALTER TABLE public.dispensary_crawl_status OWNER TO dutchie;

--
-- Name: dutchie_product_snapshots; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.dutchie_product_snapshots (
    id integer NOT NULL,
    dutchie_product_id integer NOT NULL,
    dispensary_id integer NOT NULL,
    platform_dispensary_id character varying(100) NOT NULL,
    external_product_id character varying(100) NOT NULL,
    pricing_type character varying(20) DEFAULT 'unknown'::character varying,
    crawl_mode character varying(20) DEFAULT 'mode_a'::character varying,
    status character varying(50),
    featured boolean DEFAULT false,
    special boolean DEFAULT false,
    medical_only boolean DEFAULT false,
    rec_only boolean DEFAULT false,
    is_present_in_feed boolean DEFAULT true,
    stock_status character varying(20) DEFAULT 'unknown'::character varying,
    rec_min_price_cents integer,
    rec_max_price_cents integer,
    rec_min_special_price_cents integer,
    med_min_price_cents integer,
    med_max_price_cents integer,
    med_min_special_price_cents integer,
    wholesale_min_price_cents integer,
    total_quantity_available integer,
    total_kiosk_quantity_available integer,
    manual_inventory boolean DEFAULT false,
    is_below_threshold boolean DEFAULT false,
    is_below_kiosk_threshold boolean DEFAULT false,
    options jsonb,
    raw_payload jsonb NOT NULL,
    crawled_at timestamp with time zone NOT NULL,
    created_at timestamp with time zone DEFAULT now(),
    updated_at timestamp with time zone DEFAULT now()
);


ALTER TABLE public.dutchie_product_snapshots OWNER TO dutchie;

--
-- Name: dutchie_product_snapshots_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.dutchie_product_snapshots_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.dutchie_product_snapshots_id_seq OWNER TO dutchie;

--
-- Name: dutchie_product_snapshots_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.dutchie_product_snapshots_id_seq OWNED BY public.dutchie_product_snapshots.id;


--
-- Name: dutchie_products; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.dutchie_products (
    id integer NOT NULL,
    dispensary_id integer NOT NULL,
    platform character varying(20) DEFAULT 'dutchie'::character varying NOT NULL,
    external_product_id character varying(100) NOT NULL,
    platform_dispensary_id character varying(100) NOT NULL,
    c_name character varying(500),
    name character varying(500) NOT NULL,
    brand_name character varying(255),
    brand_id character varying(100),
    brand_logo_url text,
    type character varying(100),
    subcategory character varying(100),
    strain_type character varying(50),
    provider character varying(100),
    thc numeric(10,4),
    thc_content numeric(10,4),
    cbd numeric(10,4),
    cbd_content numeric(10,4),
    cannabinoids_v2 jsonb,
    effects jsonb,
    status character varying(50),
    medical_only boolean DEFAULT false,
    rec_only boolean DEFAULT false,
    featured boolean DEFAULT false,
    coming_soon boolean DEFAULT false,
    certificate_of_analysis_enabled boolean DEFAULT false,
    is_below_threshold boolean DEFAULT false,
    is_below_kiosk_threshold boolean DEFAULT false,
    options_below_threshold boolean DEFAULT false,
    options_below_kiosk_threshold boolean DEFAULT false,
    stock_status character varying(20) DEFAULT 'unknown'::character varying,
    total_quantity_available integer DEFAULT 0,
    primary_image_url text,
    images jsonb,
    measurements jsonb,
    weight character varying(50),
    past_c_names text[],
    created_at_dutchie timestamp with time zone,
    updated_at_dutchie timestamp with time zone,
    latest_raw_payload jsonb,
    created_at timestamp with time zone DEFAULT now(),
    updated_at timestamp with time zone DEFAULT now()
);


ALTER TABLE public.dutchie_products OWNER TO dutchie;

--
-- Name: dutchie_products_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.dutchie_products_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.dutchie_products_id_seq OWNER TO dutchie;

--
-- Name: dutchie_products_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.dutchie_products_id_seq OWNED BY public.dutchie_products.id;


--
-- Name: failed_proxies; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.failed_proxies (
    id integer NOT NULL,
    host character varying(255) NOT NULL,
    port integer NOT NULL,
    protocol character varying(10) NOT NULL,
    username character varying(255),
    password character varying(255),
    failure_count integer NOT NULL,
    last_error text,
    failed_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    city character varying(100),
    state character varying(100),
    country character varying(100),
    country_code character varying(2),
    location_updated_at timestamp without time zone
);


ALTER TABLE public.failed_proxies OWNER TO dutchie;

--
-- Name: failed_proxies_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.failed_proxies_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.failed_proxies_id_seq OWNER TO dutchie;

--
-- Name: failed_proxies_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.failed_proxies_id_seq OWNED BY public.failed_proxies.id;


--
-- Name: jobs; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.jobs (
    id integer NOT NULL,
    type character varying(50) NOT NULL,
    status character varying(50) DEFAULT 'pending'::character varying,
    store_id integer,
    progress integer DEFAULT 0,
    total_items integer,
    processed_items integer DEFAULT 0,
    error text,
    started_at timestamp without time zone,
    completed_at timestamp without time zone,
    created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);


ALTER TABLE public.jobs OWNER TO dutchie;

--
-- Name: jobs_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.jobs_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.jobs_id_seq OWNER TO dutchie;

--
-- Name: jobs_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.jobs_id_seq OWNED BY public.jobs.id;


--
-- Name: price_history; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.price_history (
    id integer NOT NULL,
    product_id integer,
    regular_price numeric(10,2),
    sale_price numeric(10,2),
    recorded_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);


ALTER TABLE public.price_history OWNER TO dutchie;

--
-- Name: price_history_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.price_history_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.price_history_id_seq OWNER TO dutchie;

--
-- Name: price_history_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.price_history_id_seq OWNED BY public.price_history.id;


--
-- Name: product_categories; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.product_categories (
    id integer NOT NULL,
    product_id integer,
    category_slug character varying(255) NOT NULL,
    first_seen_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    last_seen_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);


ALTER TABLE public.product_categories OWNER TO dutchie;

--
-- Name: product_categories_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.product_categories_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.product_categories_id_seq OWNER TO dutchie;

--
-- Name: product_categories_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.product_categories_id_seq OWNED BY public.product_categories.id;


--
-- Name: products_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.products_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.products_id_seq OWNER TO dutchie;

--
-- Name: products_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.products_id_seq OWNED BY public.products.id;


--
-- Name: proxies; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.proxies (
    id integer NOT NULL,
    host character varying(255) NOT NULL,
    port integer NOT NULL,
    protocol character varying(10) NOT NULL,
    username character varying(255),
    password character varying(255),
    active boolean DEFAULT true,
    is_anonymous boolean DEFAULT false,
    last_tested_at timestamp without time zone,
    test_result character varying(50),
    response_time_ms integer,
    created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    failure_count integer DEFAULT 0,
    city character varying(100),
    state character varying(100),
    country character varying(100),
    country_code character varying(2),
    location_updated_at timestamp without time zone
);


ALTER TABLE public.proxies OWNER TO dutchie;

--
-- Name: proxies_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.proxies_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.proxies_id_seq OWNER TO dutchie;

--
-- Name: proxies_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.proxies_id_seq OWNED BY public.proxies.id;


--
-- Name: proxy_test_jobs; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.proxy_test_jobs (
    id integer NOT NULL,
    status character varying(20) DEFAULT 'pending'::character varying NOT NULL,
    total_proxies integer DEFAULT 0 NOT NULL,
    tested_proxies integer DEFAULT 0 NOT NULL,
    passed_proxies integer DEFAULT 0 NOT NULL,
    failed_proxies integer DEFAULT 0 NOT NULL,
    started_at timestamp without time zone,
    completed_at timestamp without time zone,
    created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);


ALTER TABLE public.proxy_test_jobs OWNER TO dutchie;

--
-- Name: proxy_test_jobs_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.proxy_test_jobs_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.proxy_test_jobs_id_seq OWNER TO dutchie;

--
-- Name: proxy_test_jobs_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.proxy_test_jobs_id_seq OWNED BY public.proxy_test_jobs.id;


--
-- Name: sandbox_crawl_jobs; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.sandbox_crawl_jobs (
    id integer NOT NULL,
    dispensary_id integer NOT NULL,
    sandbox_id integer,
    job_type character varying(30) DEFAULT 'detection'::character varying NOT NULL,
    status character varying(20) DEFAULT 'pending'::character varying NOT NULL,
    priority integer DEFAULT 0,
    scheduled_at timestamp with time zone DEFAULT now() NOT NULL,
    started_at timestamp with time zone,
    completed_at timestamp with time zone,
    worker_id character varying(100),
    result_summary jsonb DEFAULT '{}'::jsonb,
    error_message text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    category character varying(30) DEFAULT 'product'::character varying,
    template_name character varying(100),
    CONSTRAINT chk_sandbox_job_status CHECK (((status)::text = ANY ((ARRAY['pending'::character varying, 'running'::character varying, 'completed'::character varying, 'failed'::character varying, 'cancelled'::character varying])::text[]))),
    CONSTRAINT chk_sandbox_job_type CHECK (((job_type)::text = ANY ((ARRAY['detection'::character varying, 'template_test'::character varying, 'deep_crawl'::character varying])::text[])))
);


ALTER TABLE public.sandbox_crawl_jobs OWNER TO dutchie;

--
-- Name: TABLE sandbox_crawl_jobs; Type: COMMENT; Schema: public; Owner: dutchie
--

COMMENT ON TABLE public.sandbox_crawl_jobs IS 'Job queue for sandbox crawl operations (separate from production)';


--
-- Name: sandbox_crawl_jobs_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.sandbox_crawl_jobs_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.sandbox_crawl_jobs_id_seq OWNER TO dutchie;

--
-- Name: sandbox_crawl_jobs_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.sandbox_crawl_jobs_id_seq OWNED BY public.sandbox_crawl_jobs.id;


--
-- Name: settings; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.settings (
    key character varying(255) NOT NULL,
    value text NOT NULL,
    description text,
    updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);


ALTER TABLE public.settings OWNER TO dutchie;

--
-- Name: specials; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.specials (
    id integer NOT NULL,
    store_id integer NOT NULL,
    product_id integer,
    name character varying(255) NOT NULL,
    description text,
    discount_amount numeric(10,2),
    discount_percentage numeric(5,2),
    special_price numeric(10,2),
    original_price numeric(10,2),
    valid_date date NOT NULL,
    created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);


ALTER TABLE public.specials OWNER TO dutchie;

--
-- Name: specials_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.specials_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.specials_id_seq OWNER TO dutchie;

--
-- Name: specials_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.specials_id_seq OWNED BY public.specials.id;


--
-- Name: store_crawl_schedule_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.store_crawl_schedule_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.store_crawl_schedule_id_seq OWNER TO dutchie;

--
-- Name: store_crawl_schedule_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.store_crawl_schedule_id_seq OWNED BY public.store_crawl_schedule.id;


--
-- Name: stores_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.stores_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.stores_id_seq OWNER TO dutchie;

--
-- Name: stores_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.stores_id_seq OWNED BY public.stores.id;


--
-- Name: users; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.users (
    id integer NOT NULL,
    email character varying(255) NOT NULL,
    password_hash character varying(255) NOT NULL,
    role character varying(50) DEFAULT 'admin'::character varying,
    created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);


ALTER TABLE public.users OWNER TO dutchie;

--
-- Name: users_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.users_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.users_id_seq OWNER TO dutchie;

--
-- Name: users_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id;


--
-- Name: wp_dutchie_api_permissions; Type: TABLE; Schema: public; Owner: dutchie
--

CREATE TABLE public.wp_dutchie_api_permissions (
    id integer NOT NULL,
    user_name character varying(255) NOT NULL,
    api_key character varying(255) NOT NULL,
    allowed_ips text,
    allowed_domains text,
    is_active smallint DEFAULT 1,
    created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    last_used_at timestamp without time zone,
    store_id integer,
    store_name character varying(255)
);


ALTER TABLE public.wp_dutchie_api_permissions OWNER TO dutchie;

--
-- Name: wp_dutchie_api_permissions_id_seq; Type: SEQUENCE; Schema: public; Owner: dutchie
--

CREATE SEQUENCE public.wp_dutchie_api_permissions_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.wp_dutchie_api_permissions_id_seq OWNER TO dutchie;

--
-- Name: wp_dutchie_api_permissions_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dutchie
--

ALTER SEQUENCE public.wp_dutchie_api_permissions_id_seq OWNED BY public.wp_dutchie_api_permissions.id;


--
-- Name: api_token_usage id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.api_token_usage ALTER COLUMN id SET DEFAULT nextval('public.[REDACTED]::regclass);


--
-- Name: api_tokens id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.api_tokens ALTER COLUMN id SET DEFAULT nextval('public.api_tokens_id_seq'::regclass);


--
-- Name: azdhs_list id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.azdhs_list ALTER COLUMN id SET DEFAULT nextval('public.azdhs_list_id_seq'::regclass);


--
-- Name: batch_history id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.batch_history ALTER COLUMN id SET DEFAULT nextval('public.batch_history_id_seq'::regclass);


--
-- Name: brand_history id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.brand_history ALTER COLUMN id SET DEFAULT nextval('public.brand_history_id_seq'::regclass);


--
-- Name: brand_scrape_jobs id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.brand_scrape_jobs ALTER COLUMN id SET DEFAULT nextval('public.brand_scrape_jobs_id_seq'::regclass);


--
-- Name: brands id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.brands ALTER COLUMN id SET DEFAULT nextval('public.brands_id_seq'::regclass);


--
-- Name: campaign_products id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.campaign_products ALTER COLUMN id SET DEFAULT nextval('public.campaign_products_id_seq'::regclass);


--
-- Name: campaigns id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.campaigns ALTER COLUMN id SET DEFAULT nextval('public.campaigns_id_seq'::regclass);


--
-- Name: categories id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.categories ALTER COLUMN id SET DEFAULT nextval('public.categories_id_seq'::regclass);


--
-- Name: clicks id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.clicks ALTER COLUMN id SET DEFAULT nextval('public.clicks_id_seq'::regclass);


--
-- Name: crawl_jobs id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.crawl_jobs ALTER COLUMN id SET DEFAULT nextval('public.crawl_jobs_id_seq'::regclass);


--
-- Name: crawler_sandboxes id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.crawler_sandboxes ALTER COLUMN id SET DEFAULT nextval('public.crawler_sandboxes_id_seq'::regclass);


--
-- Name: crawler_schedule id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.crawler_schedule ALTER COLUMN id SET DEFAULT nextval('public.crawler_schedule_id_seq'::regclass);


--
-- Name: crawler_templates id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.crawler_templates ALTER COLUMN id SET DEFAULT nextval('public.crawler_templates_id_seq'::regclass);


--
-- Name: dispensaries id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dispensaries ALTER COLUMN id SET DEFAULT nextval('public.dispensaries_id_seq'::regclass);


--
-- Name: dispensary_changes id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dispensary_changes ALTER COLUMN id SET DEFAULT nextval('public.dispensary_changes_id_seq'::regclass);


--
-- Name: dispensary_crawl_jobs id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dispensary_crawl_jobs ALTER COLUMN id SET DEFAULT nextval('public.dispensary_crawl_jobs_id_seq'::regclass);


--
-- Name: dispensary_crawl_schedule id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dispensary_crawl_schedule ALTER COLUMN id SET DEFAULT nextval('public.dispensary_crawl_schedule_id_seq'::regclass);


--
-- Name: dutchie_product_snapshots id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dutchie_product_snapshots ALTER COLUMN id SET DEFAULT nextval('public.dutchie_product_snapshots_id_seq'::regclass);


--
-- Name: dutchie_products id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dutchie_products ALTER COLUMN id SET DEFAULT nextval('public.dutchie_products_id_seq'::regclass);


--
-- Name: failed_proxies id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.failed_proxies ALTER COLUMN id SET DEFAULT nextval('public.failed_proxies_id_seq'::regclass);


--
-- Name: jobs id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.jobs ALTER COLUMN id SET DEFAULT nextval('public.jobs_id_seq'::regclass);


--
-- Name: price_history id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.price_history ALTER COLUMN id SET DEFAULT nextval('public.price_history_id_seq'::regclass);


--
-- Name: product_categories id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.product_categories ALTER COLUMN id SET DEFAULT nextval('public.product_categories_id_seq'::regclass);


--
-- Name: products id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.products ALTER COLUMN id SET DEFAULT nextval('public.products_id_seq'::regclass);


--
-- Name: proxies id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.proxies ALTER COLUMN id SET DEFAULT nextval('public.proxies_id_seq'::regclass);


--
-- Name: proxy_test_jobs id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.proxy_test_jobs ALTER COLUMN id SET DEFAULT nextval('public.proxy_test_jobs_id_seq'::regclass);


--
-- Name: sandbox_crawl_jobs id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.sandbox_crawl_jobs ALTER COLUMN id SET DEFAULT nextval('public.sandbox_crawl_jobs_id_seq'::regclass);


--
-- Name: specials id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.specials ALTER COLUMN id SET DEFAULT nextval('public.specials_id_seq'::regclass);


--
-- Name: store_crawl_schedule id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.store_crawl_schedule ALTER COLUMN id SET DEFAULT nextval('public.store_crawl_schedule_id_seq'::regclass);


--
-- Name: stores id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.stores ALTER COLUMN id SET DEFAULT nextval('public.stores_id_seq'::regclass);


--
-- Name: users id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.users ALTER COLUMN id SET DEFAULT nextval('public.users_id_seq'::regclass);


--
-- Name: wp_dutchie_api_permissions id; Type: DEFAULT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.wp_dutchie_api_permissions ALTER COLUMN id SET DEFAULT nextval('public.wp_dutchie_api_permissions_id_seq'::regclass);


--
-- Name: api_token_usage [REDACTED] Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.api_token_usage
    ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);


--
-- Name: api_tokens api_tokens_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.api_tokens
    ADD CONSTRAINT api_tokens_pkey PRIMARY KEY (id);


--
-- Name: api_tokens [REDACTED] Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.api_tokens
    ADD CONSTRAINT [REDACTED]UNIQUE (token);


--
-- Name: azdhs_list azdhs_list_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.azdhs_list
    ADD CONSTRAINT azdhs_list_pkey PRIMARY KEY (id);


--
-- Name: batch_history batch_history_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.batch_history
    ADD CONSTRAINT batch_history_pkey PRIMARY KEY (id);


--
-- Name: brand_history brand_history_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.brand_history
    ADD CONSTRAINT brand_history_pkey PRIMARY KEY (id);


--
-- Name: brand_scrape_jobs brand_scrape_jobs_dispensary_id_brand_slug_key; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.brand_scrape_jobs
    ADD CONSTRAINT brand_scrape_jobs_dispensary_id_brand_slug_key UNIQUE (dispensary_id, brand_slug);


--
-- Name: brand_scrape_jobs brand_scrape_jobs_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.brand_scrape_jobs
    ADD CONSTRAINT brand_scrape_jobs_pkey PRIMARY KEY (id);


--
-- Name: brands brands_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.brands
    ADD CONSTRAINT brands_pkey PRIMARY KEY (id);


--
-- Name: campaign_products campaign_products_campaign_id_product_id_key; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.campaign_products
    ADD CONSTRAINT campaign_products_campaign_id_product_id_key UNIQUE (campaign_id, product_id);


--
-- Name: campaign_products campaign_products_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.campaign_products
    ADD CONSTRAINT campaign_products_pkey PRIMARY KEY (id);


--
-- Name: campaigns campaigns_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.campaigns
    ADD CONSTRAINT campaigns_pkey PRIMARY KEY (id);


--
-- Name: campaigns campaigns_slug_key; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.campaigns
    ADD CONSTRAINT campaigns_slug_key UNIQUE (slug);


--
-- Name: categories categories_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.categories
    ADD CONSTRAINT categories_pkey PRIMARY KEY (id);


--
-- Name: categories categories_store_id_slug_key; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.categories
    ADD CONSTRAINT categories_store_id_slug_key UNIQUE (store_id, slug);


--
-- Name: clicks clicks_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.clicks
    ADD CONSTRAINT clicks_pkey PRIMARY KEY (id);


--
-- Name: crawl_jobs crawl_jobs_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.crawl_jobs
    ADD CONSTRAINT crawl_jobs_pkey PRIMARY KEY (id);


--
-- Name: crawler_sandboxes crawler_sandboxes_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.crawler_sandboxes
    ADD CONSTRAINT crawler_sandboxes_pkey PRIMARY KEY (id);


--
-- Name: crawler_schedule crawler_schedule_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.crawler_schedule
    ADD CONSTRAINT crawler_schedule_pkey PRIMARY KEY (id);


--
-- Name: crawler_templates crawler_templates_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.crawler_templates
    ADD CONSTRAINT crawler_templates_pkey PRIMARY KEY (id);


--
-- Name: dispensaries dispensaries_azdhs_id_key; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dispensaries
    ADD CONSTRAINT dispensaries_azdhs_id_key UNIQUE (azdhs_id);


--
-- Name: dispensaries dispensaries_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dispensaries
    ADD CONSTRAINT dispensaries_pkey PRIMARY KEY (id);


--
-- Name: dispensaries dispensaries_slug_key; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dispensaries
    ADD CONSTRAINT dispensaries_slug_key UNIQUE (slug);


--
-- Name: dispensary_changes dispensary_changes_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dispensary_changes
    ADD CONSTRAINT dispensary_changes_pkey PRIMARY KEY (id);


--
-- Name: dispensary_crawl_jobs dispensary_crawl_jobs_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dispensary_crawl_jobs
    ADD CONSTRAINT dispensary_crawl_jobs_pkey PRIMARY KEY (id);


--
-- Name: dispensary_crawl_schedule dispensary_crawl_schedule_dispensary_id_key; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dispensary_crawl_schedule
    ADD CONSTRAINT dispensary_crawl_schedule_dispensary_id_key UNIQUE (dispensary_id);


--
-- Name: dispensary_crawl_schedule dispensary_crawl_schedule_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dispensary_crawl_schedule
    ADD CONSTRAINT dispensary_crawl_schedule_pkey PRIMARY KEY (id);


--
-- Name: dutchie_product_snapshots dutchie_product_snapshots_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dutchie_product_snapshots
    ADD CONSTRAINT dutchie_product_snapshots_pkey PRIMARY KEY (id);


--
-- Name: dutchie_products dutchie_products_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dutchie_products
    ADD CONSTRAINT dutchie_products_pkey PRIMARY KEY (id);


--
-- Name: failed_proxies failed_proxies_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.failed_proxies
    ADD CONSTRAINT failed_proxies_pkey PRIMARY KEY (id);


--
-- Name: jobs jobs_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.jobs
    ADD CONSTRAINT jobs_pkey PRIMARY KEY (id);


--
-- Name: price_history price_history_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.price_history
    ADD CONSTRAINT price_history_pkey PRIMARY KEY (id);


--
-- Name: product_categories product_categories_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.product_categories
    ADD CONSTRAINT product_categories_pkey PRIMARY KEY (id);


--
-- Name: product_categories product_categories_product_id_category_slug_key; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.product_categories
    ADD CONSTRAINT product_categories_product_id_category_slug_key UNIQUE (product_id, category_slug);


--
-- Name: products products_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.products
    ADD CONSTRAINT products_pkey PRIMARY KEY (id);


--
-- Name: products products_store_id_slug_unique; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.products
    ADD CONSTRAINT products_store_id_slug_unique UNIQUE (store_id, slug);


--
-- Name: proxies proxies_host_port_protocol_key; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.proxies
    ADD CONSTRAINT proxies_host_port_protocol_key UNIQUE (host, port, protocol);


--
-- Name: proxies proxies_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.proxies
    ADD CONSTRAINT proxies_pkey PRIMARY KEY (id);


--
-- Name: proxy_test_jobs proxy_test_jobs_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.proxy_test_jobs
    ADD CONSTRAINT proxy_test_jobs_pkey PRIMARY KEY (id);


--
-- Name: sandbox_crawl_jobs sandbox_crawl_jobs_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.sandbox_crawl_jobs
    ADD CONSTRAINT sandbox_crawl_jobs_pkey PRIMARY KEY (id);


--
-- Name: settings settings_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.settings
    ADD CONSTRAINT settings_pkey PRIMARY KEY (key);


--
-- Name: specials specials_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.specials
    ADD CONSTRAINT specials_pkey PRIMARY KEY (id);


--
-- Name: store_crawl_schedule store_crawl_schedule_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.store_crawl_schedule
    ADD CONSTRAINT store_crawl_schedule_pkey PRIMARY KEY (id);


--
-- Name: stores stores_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.stores
    ADD CONSTRAINT stores_pkey PRIMARY KEY (id);


--
-- Name: stores stores_slug_key; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.stores
    ADD CONSTRAINT stores_slug_key UNIQUE (slug);


--
-- Name: dutchie_products uk_dutchie_products; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dutchie_products
    ADD CONSTRAINT uk_dutchie_products UNIQUE (dispensary_id, external_product_id);


--
-- Name: crawler_schedule uq_crawler_schedule_type; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.crawler_schedule
    ADD CONSTRAINT uq_crawler_schedule_type UNIQUE (schedule_type);


--
-- Name: store_crawl_schedule uq_store_crawl_schedule_store; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.store_crawl_schedule
    ADD CONSTRAINT uq_store_crawl_schedule_store UNIQUE (store_id);


--
-- Name: crawler_templates uq_template_name; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.crawler_templates
    ADD CONSTRAINT uq_template_name UNIQUE (provider, name, version);


--
-- Name: users users_email_key; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.users
    ADD CONSTRAINT users_email_key UNIQUE (email);


--
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.users
    ADD CONSTRAINT users_pkey PRIMARY KEY (id);


--
-- Name: wp_dutchie_api_permissions wp_dutchie_api_permissions_api_key_key; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.wp_dutchie_api_permissions
    ADD CONSTRAINT wp_dutchie_api_permissions_api_key_key UNIQUE (api_key);


--
-- Name: wp_dutchie_api_permissions wp_dutchie_api_permissions_pkey; Type: CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.wp_dutchie_api_permissions
    ADD CONSTRAINT wp_dutchie_api_permissions_pkey PRIMARY KEY (id);


--
-- Name: failed_proxies_host_port_protocol_idx; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE UNIQUE INDEX failed_proxies_host_port_protocol_idx ON public.failed_proxies USING btree (host, port, protocol);


--
-- Name: idx_api_token_usage_created_at; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_api_token_usage_created_at ON public.api_token_usage USING btree (created_at);


--
-- Name: idx_api_token_usage_endpoint; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_api_token_usage_endpoint ON public.api_token_usage USING btree (endpoint);


--
-- Name: idx_api_token_usage_token_id; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_api_token_usage_token_id ON public.api_token_usage USING btree (token_id);


--
-- Name: idx_api_tokens_active; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_api_tokens_active ON public.api_tokens USING btree (active);


--
-- Name: idx_api_tokens_token; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_api_tokens_token ON public.api_tokens USING btree (token);


--
-- Name: idx_batch_history_product; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_batch_history_product ON public.batch_history USING btree (product_id, recorded_at DESC);


--
-- Name: idx_batch_history_recorded; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_batch_history_recorded ON public.batch_history USING btree (recorded_at DESC);


--
-- Name: idx_brand_history_brand; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_brand_history_brand ON public.brand_history USING btree (brand_name, event_at DESC);


--
-- Name: idx_brand_history_dispensary; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_brand_history_dispensary ON public.brand_history USING btree (dispensary_id, event_at DESC);


--
-- Name: idx_brand_history_event; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_brand_history_event ON public.brand_history USING btree (event_type, event_at DESC);


--
-- Name: idx_brand_jobs_dispensary; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_brand_jobs_dispensary ON public.brand_scrape_jobs USING btree (dispensary_id);


--
-- Name: idx_brand_jobs_status; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_brand_jobs_status ON public.brand_scrape_jobs USING btree (status);


--
-- Name: idx_brand_jobs_worker; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_brand_jobs_worker ON public.brand_scrape_jobs USING btree (worker_id) WHERE (worker_id IS NOT NULL);


--
-- Name: idx_brands_dispensary; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_brands_dispensary ON public.brands USING btree (dispensary_id);


--
-- Name: idx_brands_last_seen; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_brands_last_seen ON public.brands USING btree (last_seen_at DESC);


--
-- Name: idx_brands_store_id; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_brands_store_id ON public.brands USING btree (store_id);


--
-- Name: idx_brands_store_name; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE UNIQUE INDEX idx_brands_store_name ON public.brands USING btree (store_id, name);


--
-- Name: idx_categories_dispensary_id; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_categories_dispensary_id ON public.categories USING btree (dispensary_id);


--
-- Name: idx_categories_parent_id; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_categories_parent_id ON public.categories USING btree (parent_id);


--
-- Name: idx_categories_path; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_categories_path ON public.categories USING btree (path);


--
-- Name: idx_clicks_campaign_id; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_clicks_campaign_id ON public.clicks USING btree (campaign_id);


--
-- Name: idx_clicks_clicked_at; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_clicks_clicked_at ON public.clicks USING btree (clicked_at);


--
-- Name: idx_clicks_product_id; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_clicks_product_id ON public.clicks USING btree (product_id);


--
-- Name: idx_crawl_jobs_pending; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_crawl_jobs_pending ON public.crawl_jobs USING btree (scheduled_at) WHERE ((status)::text = 'pending'::text);


--
-- Name: idx_crawl_jobs_status; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_crawl_jobs_status ON public.crawl_jobs USING btree (status);


--
-- Name: idx_crawl_jobs_store_status; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_crawl_jobs_store_status ON public.crawl_jobs USING btree (store_id, status);


--
-- Name: idx_crawl_jobs_store_time; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_crawl_jobs_store_time ON public.crawl_jobs USING btree (store_id, created_at DESC);


--
-- Name: idx_disp_brand_mode; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_disp_brand_mode ON public.dispensaries USING btree (brand_crawler_mode);


--
-- Name: idx_disp_brand_provider; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_disp_brand_provider ON public.dispensaries USING btree (brand_provider);


--
-- Name: idx_disp_metadata_mode; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_disp_metadata_mode ON public.dispensaries USING btree (metadata_crawler_mode);


--
-- Name: idx_disp_metadata_provider; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_disp_metadata_provider ON public.dispensaries USING btree (metadata_provider);


--
-- Name: idx_disp_product_mode; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_disp_product_mode ON public.dispensaries USING btree (product_crawler_mode);


--
-- Name: idx_disp_product_provider; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_disp_product_provider ON public.dispensaries USING btree (product_provider);


--
-- Name: idx_disp_specials_mode; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_disp_specials_mode ON public.dispensaries USING btree (specials_crawler_mode);


--
-- Name: idx_disp_specials_provider; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_disp_specials_provider ON public.dispensaries USING btree (specials_provider);


--
-- Name: idx_dispensaries_azdhs_id; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensaries_azdhs_id ON public.dispensaries USING btree (azdhs_id);


--
-- Name: idx_dispensaries_city; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensaries_city ON public.dispensaries USING btree (city);


--
-- Name: idx_dispensaries_crawl_status; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensaries_crawl_status ON public.dispensaries USING btree (crawl_status);


--
-- Name: idx_dispensaries_crawler_mode; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensaries_crawler_mode ON public.dispensaries USING btree (crawler_mode);


--
-- Name: idx_dispensaries_crawler_status; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensaries_crawler_status ON public.dispensaries USING btree (crawler_status);


--
-- Name: idx_dispensaries_dutchie_production; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensaries_dutchie_production ON public.dispensaries USING btree (id) WHERE (((menu_provider)::text = 'dutchie'::text) AND ((crawler_mode)::text = 'production'::text));


--
-- Name: idx_dispensaries_location; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensaries_location ON public.dispensaries USING btree (latitude, longitude) WHERE ((latitude IS NOT NULL) AND (longitude IS NOT NULL));


--
-- Name: idx_dispensaries_menu_status; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensaries_menu_status ON public.dispensaries USING btree (menu_scrape_status);


--
-- Name: idx_dispensaries_needs_detection; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensaries_needs_detection ON public.dispensaries USING btree (id) WHERE ((menu_provider IS NULL) OR (menu_provider_confidence < 70));


--
-- Name: idx_dispensaries_next_crawl; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensaries_next_crawl ON public.dispensaries USING btree (next_crawl_at) WHERE (scrape_enabled = true);


--
-- Name: idx_dispensaries_provider; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensaries_provider ON public.dispensaries USING btree (menu_provider);


--
-- Name: idx_dispensaries_provider_confidence; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensaries_provider_confidence ON public.dispensaries USING btree (menu_provider_confidence);


--
-- Name: idx_dispensaries_sandbox; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensaries_sandbox ON public.dispensaries USING btree (id) WHERE ((crawler_mode)::text = 'sandbox'::text);


--
-- Name: idx_dispensaries_slug; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensaries_slug ON public.dispensaries USING btree (slug);


--
-- Name: idx_dispensaries_state; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensaries_state ON public.dispensaries USING btree (state);


--
-- Name: idx_dispensary_changes_created_at; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensary_changes_created_at ON public.dispensary_changes USING btree (created_at DESC);


--
-- Name: idx_dispensary_changes_dispensary_status; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensary_changes_dispensary_status ON public.dispensary_changes USING btree (dispensary_id, status);


--
-- Name: idx_dispensary_changes_requires_recrawl; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensary_changes_requires_recrawl ON public.dispensary_changes USING btree (requires_recrawl) WHERE (requires_recrawl = true);


--
-- Name: idx_dispensary_changes_status; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensary_changes_status ON public.dispensary_changes USING btree (status);


--
-- Name: idx_dispensary_crawl_jobs_dispensary; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensary_crawl_jobs_dispensary ON public.dispensary_crawl_jobs USING btree (dispensary_id, created_at DESC);


--
-- Name: idx_dispensary_crawl_jobs_pending; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensary_crawl_jobs_pending ON public.dispensary_crawl_jobs USING btree (priority DESC, scheduled_at) WHERE ((status)::text = 'pending'::text);


--
-- Name: idx_dispensary_crawl_jobs_recent; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensary_crawl_jobs_recent ON public.dispensary_crawl_jobs USING btree (created_at DESC);


--
-- Name: idx_dispensary_crawl_jobs_status; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensary_crawl_jobs_status ON public.dispensary_crawl_jobs USING btree (status);


--
-- Name: idx_dispensary_crawl_schedule_active; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensary_crawl_schedule_active ON public.dispensary_crawl_schedule USING btree (is_active);


--
-- Name: idx_dispensary_crawl_schedule_next_run; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensary_crawl_schedule_next_run ON public.dispensary_crawl_schedule USING btree (next_run_at) WHERE (is_active = true);


--
-- Name: idx_dispensary_crawl_schedule_priority; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensary_crawl_schedule_priority ON public.dispensary_crawl_schedule USING btree (priority DESC, next_run_at);


--
-- Name: idx_dispensary_crawl_schedule_status; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_dispensary_crawl_schedule_status ON public.dispensary_crawl_schedule USING btree (last_status);


--
-- Name: idx_jobs_status; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_jobs_status ON public.jobs USING btree (status);


--
-- Name: idx_jobs_store_id; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_jobs_store_id ON public.jobs USING btree (store_id);


--
-- Name: idx_jobs_type; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_jobs_type ON public.jobs USING btree (type);


--
-- Name: idx_price_history_product; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_price_history_product ON public.price_history USING btree (product_id, recorded_at DESC);


--
-- Name: idx_price_history_recorded; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_price_history_recorded ON public.price_history USING btree (recorded_at DESC);


--
-- Name: idx_product_categories_product; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_product_categories_product ON public.product_categories USING btree (product_id);


--
-- Name: idx_product_categories_slug; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_product_categories_slug ON public.product_categories USING btree (category_slug, last_seen_at DESC);


--
-- Name: idx_products_availability_by_dispensary; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_products_availability_by_dispensary ON public.products USING btree (dispensary_id, availability_status) WHERE (dispensary_id IS NOT NULL);


--
-- Name: idx_products_availability_by_store; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_products_availability_by_store ON public.products USING btree (store_id, availability_status) WHERE (store_id IS NOT NULL);


--
-- Name: idx_products_availability_status; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_products_availability_status ON public.products USING btree (availability_status);


--
-- Name: idx_products_brand_external; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_products_brand_external ON public.products USING btree (brand_external_id);


--
-- Name: idx_products_dispensary_id; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_products_dispensary_id ON public.products USING btree (dispensary_id);


--
-- Name: idx_products_enterprise; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_products_enterprise ON public.products USING btree (enterprise_product_id);


--
-- Name: idx_products_is_special; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_products_is_special ON public.products USING btree (is_on_special);


--
-- Name: idx_products_sku; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_products_sku ON public.products USING btree (sku);


--
-- Name: idx_products_status; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_products_status ON public.products USING btree (status);


--
-- Name: idx_products_stock_quantity; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_products_stock_quantity ON public.products USING btree (stock_quantity) WHERE (stock_quantity IS NOT NULL);


--
-- Name: idx_products_stock_status; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_products_stock_status ON public.products USING btree (stock_status);


--
-- Name: idx_products_subcategory; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_products_subcategory ON public.products USING btree (subcategory);


--
-- Name: idx_proxies_location; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_proxies_location ON public.proxies USING btree (country_code, state, city);


--
-- Name: idx_proxy_test_jobs_created_at; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_proxy_test_jobs_created_at ON public.proxy_test_jobs USING btree (created_at DESC);


--
-- Name: idx_proxy_test_jobs_status; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_proxy_test_jobs_status ON public.proxy_test_jobs USING btree (status);


--
-- Name: idx_sandbox_active_per_dispensary; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE UNIQUE INDEX idx_sandbox_active_per_dispensary ON public.crawler_sandboxes USING btree (dispensary_id) WHERE ((status)::text <> ALL ((ARRAY['moved_to_production'::character varying, 'failed'::character varying])::text[]));


--
-- Name: idx_sandbox_category; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_sandbox_category ON public.crawler_sandboxes USING btree (category);


--
-- Name: idx_sandbox_dispensary; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_sandbox_dispensary ON public.crawler_sandboxes USING btree (dispensary_id);


--
-- Name: idx_sandbox_job_category; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_sandbox_job_category ON public.sandbox_crawl_jobs USING btree (category);


--
-- Name: idx_sandbox_job_dispensary; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_sandbox_job_dispensary ON public.sandbox_crawl_jobs USING btree (dispensary_id);


--
-- Name: idx_sandbox_job_pending; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_sandbox_job_pending ON public.sandbox_crawl_jobs USING btree (scheduled_at) WHERE ((status)::text = 'pending'::text);


--
-- Name: idx_sandbox_job_status; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_sandbox_job_status ON public.sandbox_crawl_jobs USING btree (status);


--
-- Name: idx_sandbox_mode; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_sandbox_mode ON public.crawler_sandboxes USING btree (mode);


--
-- Name: idx_sandbox_status; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_sandbox_status ON public.crawler_sandboxes USING btree (status);


--
-- Name: idx_sandbox_suspected_provider; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_sandbox_suspected_provider ON public.crawler_sandboxes USING btree (suspected_menu_provider);


--
-- Name: idx_sandbox_template; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_sandbox_template ON public.crawler_sandboxes USING btree (template_name);


--
-- Name: idx_specials_product_id; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_specials_product_id ON public.specials USING btree (product_id);


--
-- Name: idx_specials_store_date; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_specials_store_date ON public.specials USING btree (store_id, valid_date DESC);


--
-- Name: idx_stores_dispensary_id; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_stores_dispensary_id ON public.stores USING btree (dispensary_id);


--
-- Name: idx_template_active; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_template_active ON public.crawler_templates USING btree (is_active);


--
-- Name: idx_template_default; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_template_default ON public.crawler_templates USING btree (provider, is_default_for_provider) WHERE (is_default_for_provider = true);


--
-- Name: idx_template_provider; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_template_provider ON public.crawler_templates USING btree (provider);


--
-- Name: idx_wp_api_permissions_store_id; Type: INDEX; Schema: public; Owner: dutchie
--

CREATE INDEX idx_wp_api_permissions_store_id ON public.wp_dutchie_api_permissions USING btree (store_id);


--
-- Name: api_tokens [REDACTED] Type: TRIGGER; Schema: public; Owner: dutchie
--

CREATE TRIGGER [REDACTED]BEFORE UPDATE ON public.api_tokens FOR EACH ROW EXECUTE FUNCTION public.update_api_token_updated_at();


--
-- Name: crawl_jobs trigger_crawl_jobs_updated_at; Type: TRIGGER; Schema: public; Owner: dutchie
--

CREATE TRIGGER trigger_crawl_jobs_updated_at BEFORE UPDATE ON public.crawl_jobs FOR EACH ROW EXECUTE FUNCTION public.update_schedule_updated_at();


--
-- Name: crawler_schedule trigger_crawler_schedule_updated_at; Type: TRIGGER; Schema: public; Owner: dutchie
--

CREATE TRIGGER trigger_crawler_schedule_updated_at BEFORE UPDATE ON public.crawler_schedule FOR EACH ROW EXECUTE FUNCTION public.update_schedule_updated_at();


--
-- Name: sandbox_crawl_jobs trigger_sandbox_job_updated_at; Type: TRIGGER; Schema: public; Owner: dutchie
--

CREATE TRIGGER trigger_sandbox_job_updated_at BEFORE UPDATE ON public.sandbox_crawl_jobs FOR EACH ROW EXECUTE FUNCTION public.update_sandbox_timestamp();


--
-- Name: crawler_sandboxes trigger_sandbox_updated_at; Type: TRIGGER; Schema: public; Owner: dutchie
--

CREATE TRIGGER trigger_sandbox_updated_at BEFORE UPDATE ON public.crawler_sandboxes FOR EACH ROW EXECUTE FUNCTION public.update_sandbox_timestamp();


--
-- Name: dispensary_changes trigger_set_requires_recrawl; Type: TRIGGER; Schema: public; Owner: dutchie
--

CREATE TRIGGER trigger_set_requires_recrawl BEFORE INSERT ON public.dispensary_changes FOR EACH ROW EXECUTE FUNCTION public.set_requires_recrawl();


--
-- Name: store_crawl_schedule trigger_store_crawl_schedule_updated_at; Type: TRIGGER; Schema: public; Owner: dutchie
--

CREATE TRIGGER trigger_store_crawl_schedule_updated_at BEFORE UPDATE ON public.store_crawl_schedule FOR EACH ROW EXECUTE FUNCTION public.update_schedule_updated_at();


--
-- Name: crawler_templates trigger_template_updated_at; Type: TRIGGER; Schema: public; Owner: dutchie
--

CREATE TRIGGER trigger_template_updated_at BEFORE UPDATE ON public.crawler_templates FOR EACH ROW EXECUTE FUNCTION public.update_sandbox_timestamp();


--
-- Name: brand_scrape_jobs trigger_update_brand_scrape_jobs_timestamp; Type: TRIGGER; Schema: public; Owner: dutchie
--

CREATE TRIGGER trigger_update_brand_scrape_jobs_timestamp BEFORE UPDATE ON public.brand_scrape_jobs FOR EACH ROW EXECUTE FUNCTION public.update_brand_scrape_jobs_updated_at();


--
-- Name: api_token_usage [REDACTED] Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.api_token_usage
    ADD CONSTRAINT [REDACTED]FOREIGN KEY (token_id) REFERENCES public.api_tokens(id) ON DELETE CASCADE;


--
-- Name: api_tokens [REDACTED] Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.api_tokens
    ADD CONSTRAINT [REDACTED]FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;


--
-- Name: batch_history batch_history_product_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.batch_history
    ADD CONSTRAINT batch_history_product_id_fkey FOREIGN KEY (product_id) REFERENCES public.products(id) ON DELETE CASCADE;


--
-- Name: brand_history brand_history_dispensary_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.brand_history
    ADD CONSTRAINT brand_history_dispensary_id_fkey FOREIGN KEY (dispensary_id) REFERENCES public.dispensaries(id) ON DELETE CASCADE;


--
-- Name: brand_scrape_jobs brand_scrape_jobs_dispensary_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.brand_scrape_jobs
    ADD CONSTRAINT brand_scrape_jobs_dispensary_id_fkey FOREIGN KEY (dispensary_id) REFERENCES public.dispensaries(id);


--
-- Name: brands brands_dispensary_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.brands
    ADD CONSTRAINT brands_dispensary_id_fkey FOREIGN KEY (dispensary_id) REFERENCES public.dispensaries(id);


--
-- Name: brands brands_store_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.brands
    ADD CONSTRAINT brands_store_id_fkey FOREIGN KEY (store_id) REFERENCES public.stores(id) ON DELETE CASCADE;


--
-- Name: campaign_products campaign_products_campaign_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.campaign_products
    ADD CONSTRAINT campaign_products_campaign_id_fkey FOREIGN KEY (campaign_id) REFERENCES public.campaigns(id) ON DELETE CASCADE;


--
-- Name: campaign_products campaign_products_product_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.campaign_products
    ADD CONSTRAINT campaign_products_product_id_fkey FOREIGN KEY (product_id) REFERENCES public.products(id) ON DELETE CASCADE;


--
-- Name: categories categories_dispensary_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.categories
    ADD CONSTRAINT categories_dispensary_id_fkey FOREIGN KEY (dispensary_id) REFERENCES public.dispensaries(id) ON DELETE CASCADE;


--
-- Name: categories categories_parent_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.categories
    ADD CONSTRAINT categories_parent_id_fkey FOREIGN KEY (parent_id) REFERENCES public.categories(id) ON DELETE CASCADE;


--
-- Name: categories categories_store_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.categories
    ADD CONSTRAINT categories_store_id_fkey FOREIGN KEY (store_id) REFERENCES public.stores(id) ON DELETE CASCADE;


--
-- Name: clicks clicks_campaign_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.clicks
    ADD CONSTRAINT clicks_campaign_id_fkey FOREIGN KEY (campaign_id) REFERENCES public.campaigns(id) ON DELETE SET NULL;


--
-- Name: clicks clicks_product_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.clicks
    ADD CONSTRAINT clicks_product_id_fkey FOREIGN KEY (product_id) REFERENCES public.products(id) ON DELETE CASCADE;


--
-- Name: crawl_jobs crawl_jobs_store_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.crawl_jobs
    ADD CONSTRAINT crawl_jobs_store_id_fkey FOREIGN KEY (store_id) REFERENCES public.stores(id) ON DELETE CASCADE;


--
-- Name: crawler_sandboxes crawler_sandboxes_dispensary_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.crawler_sandboxes
    ADD CONSTRAINT crawler_sandboxes_dispensary_id_fkey FOREIGN KEY (dispensary_id) REFERENCES public.dispensaries(id) ON DELETE CASCADE;


--
-- Name: dispensaries dispensaries_azdhs_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dispensaries
    ADD CONSTRAINT dispensaries_azdhs_id_fkey FOREIGN KEY (azdhs_id) REFERENCES public.azdhs_list(id);


--
-- Name: dispensary_changes dispensary_changes_dispensary_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dispensary_changes
    ADD CONSTRAINT dispensary_changes_dispensary_id_fkey FOREIGN KEY (dispensary_id) REFERENCES public.dispensaries(id) ON DELETE CASCADE;


--
-- Name: dispensary_changes dispensary_changes_reviewed_by_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dispensary_changes
    ADD CONSTRAINT dispensary_changes_reviewed_by_fkey FOREIGN KEY (reviewed_by) REFERENCES public.users(id);


--
-- Name: dispensary_crawl_jobs dispensary_crawl_jobs_dispensary_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dispensary_crawl_jobs
    ADD CONSTRAINT dispensary_crawl_jobs_dispensary_id_fkey FOREIGN KEY (dispensary_id) REFERENCES public.dispensaries(id) ON DELETE CASCADE;


--
-- Name: dispensary_crawl_jobs dispensary_crawl_jobs_schedule_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dispensary_crawl_jobs
    ADD CONSTRAINT dispensary_crawl_jobs_schedule_id_fkey FOREIGN KEY (schedule_id) REFERENCES public.dispensary_crawl_schedule(id) ON DELETE SET NULL;


--
-- Name: dispensary_crawl_schedule dispensary_crawl_schedule_dispensary_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dispensary_crawl_schedule
    ADD CONSTRAINT dispensary_crawl_schedule_dispensary_id_fkey FOREIGN KEY (dispensary_id) REFERENCES public.dispensaries(id) ON DELETE CASCADE;


--
-- Name: dutchie_product_snapshots dutchie_product_snapshots_dispensary_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dutchie_product_snapshots
    ADD CONSTRAINT dutchie_product_snapshots_dispensary_id_fkey FOREIGN KEY (dispensary_id) REFERENCES public.dispensaries(id) ON DELETE CASCADE;


--
-- Name: dutchie_product_snapshots dutchie_product_snapshots_dutchie_product_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dutchie_product_snapshots
    ADD CONSTRAINT dutchie_product_snapshots_dutchie_product_id_fkey FOREIGN KEY (dutchie_product_id) REFERENCES public.dutchie_products(id) ON DELETE CASCADE;


--
-- Name: dutchie_products dutchie_products_dispensary_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.dutchie_products
    ADD CONSTRAINT dutchie_products_dispensary_id_fkey FOREIGN KEY (dispensary_id) REFERENCES public.dispensaries(id) ON DELETE CASCADE;


--
-- Name: jobs jobs_store_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.jobs
    ADD CONSTRAINT jobs_store_id_fkey FOREIGN KEY (store_id) REFERENCES public.stores(id) ON DELETE CASCADE;


--
-- Name: price_history price_history_product_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.price_history
    ADD CONSTRAINT price_history_product_id_fkey FOREIGN KEY (product_id) REFERENCES public.products(id) ON DELETE CASCADE;


--
-- Name: product_categories product_categories_product_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.product_categories
    ADD CONSTRAINT product_categories_product_id_fkey FOREIGN KEY (product_id) REFERENCES public.products(id) ON DELETE CASCADE;


--
-- Name: products products_category_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.products
    ADD CONSTRAINT products_category_id_fkey FOREIGN KEY (category_id) REFERENCES public.categories(id) ON DELETE SET NULL;


--
-- Name: products products_dispensary_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.products
    ADD CONSTRAINT products_dispensary_id_fkey FOREIGN KEY (dispensary_id) REFERENCES public.dispensaries(id) ON DELETE CASCADE;


--
-- Name: products products_store_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.products
    ADD CONSTRAINT products_store_id_fkey FOREIGN KEY (store_id) REFERENCES public.stores(id) ON DELETE CASCADE;


--
-- Name: sandbox_crawl_jobs sandbox_crawl_jobs_dispensary_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.sandbox_crawl_jobs
    ADD CONSTRAINT sandbox_crawl_jobs_dispensary_id_fkey FOREIGN KEY (dispensary_id) REFERENCES public.dispensaries(id) ON DELETE CASCADE;


--
-- Name: sandbox_crawl_jobs sandbox_crawl_jobs_sandbox_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.sandbox_crawl_jobs
    ADD CONSTRAINT sandbox_crawl_jobs_sandbox_id_fkey FOREIGN KEY (sandbox_id) REFERENCES public.crawler_sandboxes(id) ON DELETE SET NULL;


--
-- Name: specials specials_product_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.specials
    ADD CONSTRAINT specials_product_id_fkey FOREIGN KEY (product_id) REFERENCES public.products(id) ON DELETE CASCADE;


--
-- Name: specials specials_store_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.specials
    ADD CONSTRAINT specials_store_id_fkey FOREIGN KEY (store_id) REFERENCES public.stores(id) ON DELETE CASCADE;


--
-- Name: store_crawl_schedule store_crawl_schedule_store_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.store_crawl_schedule
    ADD CONSTRAINT store_crawl_schedule_store_id_fkey FOREIGN KEY (store_id) REFERENCES public.stores(id) ON DELETE CASCADE;


--
-- Name: stores stores_dispensary_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.stores
    ADD CONSTRAINT stores_dispensary_id_fkey FOREIGN KEY (dispensary_id) REFERENCES public.dispensaries(id) ON DELETE SET NULL;


--
-- Name: wp_dutchie_api_permissions wp_dutchie_api_permissions_store_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dutchie
--

ALTER TABLE ONLY public.wp_dutchie_api_permissions
    ADD CONSTRAINT wp_dutchie_api_permissions_store_id_fkey FOREIGN KEY (store_id) REFERENCES public.stores(id);


--
-- PostgreSQL database dump complete
--

\unrestrict u76b1lsSuckyRNpZbORH9drBRaNwzQbqR7X3xYnHxUdiczMnjtBCdx8KbLlDBsP

