-- Migration 072: Create compatibility views for store_products and store_product_snapshots -- These views provide backward-compatible column names for API routes -- v_products view - aliases store_products columns to match legacy dutchie_products naming CREATE OR REPLACE VIEW v_products AS SELECT id, dispensary_id, provider_product_id as external_product_id, provider_product_id as dutchie_id, name_raw as name, brand_name_raw as brand_name, category_raw as type, subcategory_raw as subcategory, strain_type, thc_percent as thc, cbd_percent as cbd, stock_status, is_in_stock, stock_quantity, image_url, primary_image_url, images, effects, description, is_on_special, featured, medical_only, rec_only, external_product_id as external_id, provider, created_at, updated_at FROM store_products; -- v_product_snapshots view - aliases store_product_snapshots columns to match legacy naming CREATE OR REPLACE VIEW v_product_snapshots AS SELECT id, store_product_id, dispensary_id, provider, provider_product_id, crawl_run_id, captured_at as crawled_at, name_raw, brand_name_raw, category_raw, subcategory_raw, -- Convert price_rec (dollars) to rec_min_price_cents (cents) CASE WHEN price_rec IS NOT NULL THEN (price_rec * 100)::integer END as rec_min_price_cents, CASE WHEN price_rec IS NOT NULL THEN (price_rec * 100)::integer END as rec_max_price_cents, CASE WHEN price_rec_special IS NOT NULL THEN (price_rec_special * 100)::integer END as rec_min_special_price_cents, CASE WHEN price_med IS NOT NULL THEN (price_med * 100)::integer END as med_min_price_cents, CASE WHEN price_med IS NOT NULL THEN (price_med * 100)::integer END as med_max_price_cents, CASE WHEN price_med_special IS NOT NULL THEN (price_med_special * 100)::integer END as med_min_special_price_cents, is_on_special as special, discount_percent, is_in_stock, stock_quantity, stock_status, stock_quantity as total_quantity_available, thc_percent, cbd_percent, image_url, raw_data as options, created_at FROM store_product_snapshots; -- Add indexes for the views' underlying tables CREATE INDEX IF NOT EXISTS idx_store_products_dispensary ON store_products(dispensary_id); CREATE INDEX IF NOT EXISTS idx_store_products_stock ON store_products(stock_status); CREATE INDEX IF NOT EXISTS idx_store_snapshots_product ON store_product_snapshots(store_product_id); CREATE INDEX IF NOT EXISTS idx_store_snapshots_captured ON store_product_snapshots(captured_at DESC);