Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/backend-utils.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211-- ============================================================================-- Migration 014: Helpful Views-- Create views for common query patterns to simplify application code-- ============================================================================-- Uses owner_id for collections (semantic naming)-- ============================================================================
-- ############################################################################-- SECTION 1: COLLECTION VIEWS-- ############################################################################
-- Active collections (excludes soft-deleted)CREATE VIEW active_collections ASSELECT * FROM collectionsWHERE deleted_at IS NULL;
-- Collections with computed item countsCREATE VIEW collections_with_counts ASSELECT c.*, COUNT(ci.id) as actual_item_countFROM collections cLEFT JOIN collection_items ci ON c.id = ci.collection_idWHERE c.deleted_at IS NULLGROUP BY c.id;
-- User collections with owner profile infoCREATE VIEW user_collections ASSELECT c.*, p.username as owner_username, p.display_name as owner_display_name, p.avatar_url as owner_avatar_urlFROM collections cJOIN profiles p ON c.owner_id = p.idWHERE c.deleted_at IS NULL;
-- ############################################################################-- SECTION 2: USER PRODUCT STATE VIEWS-- ############################################################################
-- User owned productsCREATE VIEW user_owned_products ASSELECT ups.user_id, p.*, ups.ownership_type_code, ups.purchased_atFROM user_product_state upsJOIN products p ON ups.product_id = p.idWHERE ups.purchased_at IS NOT NULLAND p.archived_at IS NULL;
-- User wishlistCREATE VIEW user_wishlist ASSELECT ups.user_id, p.*, ups.wishlisted_at, ups.wishlist_priority, ups.wishlist_notesFROM user_product_state upsJOIN products p ON ups.product_id = p.idWHERE ups.wishlisted_at IS NOT NULLAND ups.purchased_at IS NULLAND p.archived_at IS NULLORDER BY ups.wishlist_priority DESC, ups.wishlisted_at DESC;
-- ############################################################################-- SECTION 3: PRODUCT VIEWS-- ############################################################################
-- Products with aggregated statisticsCREATE VIEW products_with_stats ASSELECT p.*, COUNT(DISTINCT ups.user_id) FILTER (WHERE ups.purchased_at IS NOT NULL) as owners_count, COUNT(DISTINCT ups.user_id) FILTER (WHERE ups.wishlisted_at IS NOT NULL AND ups.purchased_at IS NULL) as wishlist_count, AVG(r.rating) FILTER (WHERE r.deleted_at IS NULL AND r.moderation_status_code = 'approved') as computed_avg_rating, COUNT(DISTINCT r.id) FILTER (WHERE r.deleted_at IS NULL AND r.moderation_status_code = 'approved') as computed_review_countFROM products pLEFT JOIN user_product_state ups ON p.id = ups.product_idLEFT JOIN reviews r ON p.id::text = r.target_id AND r.target_type_code = 'product'WHERE p.archived_at IS NULLGROUP BY p.id;
-- Available products (currently purchasable)CREATE VIEW available_products ASSELECT p.*FROM products pJOIN product_statuses ps ON p.status_code = ps.codeWHERE p.archived_at IS NULLAND ps.is_available_for_purchase = TRUEAND (p.featured_until IS NULL OR p.featured_until > NOW());
-- In-stock SKUsCREATE VIEW in_stock_skus ASSELECT s.*, p.title as product_title, p.slug as product_slugFROM skus sJOIN products p ON s.product_id = p.idWHERE (s.stock_quantity > 0 OR s.backorder_allowed_until > NOW())AND (s.available_until IS NULL OR s.available_until > NOW())AND p.archived_at IS NULL;
-- ############################################################################-- SECTION 4: UGC VIEWS-- ############################################################################
-- Active comments with reply countsCREATE VIEW active_comments_with_replies ASSELECT c.*, COUNT(replies.id) as actual_reply_countFROM comments cLEFT JOIN comments replies ON c.id = replies.parent_comment_id AND replies.deleted_at IS NULLWHERE c.deleted_at IS NULLGROUP BY c.id;
-- Approved reviews with vote countsCREATE VIEW approved_reviews ASSELECT r.*, COUNT(rv.user_id) FILTER (WHERE rv.is_helpful = TRUE) as computed_helpful_count, COUNT(rv.user_id) FILTER (WHERE rv.is_helpful = FALSE) as computed_unhelpful_countFROM reviews rLEFT JOIN review_votes rv ON r.id = rv.review_idWHERE r.deleted_at IS NULLAND r.moderation_status_code = 'approved'GROUP BY r.id;
-- ############################################################################-- SECTION 5: ORDER VIEWS-- ############################################################################
-- Active orders by user (non-cancelled, non-completed)CREATE VIEW active_orders_by_user ASSELECT o.*, COUNT(oi.id) as item_count, STRING_AGG(DISTINCT oi.product_title, ', ') as product_titlesFROM orders oLEFT JOIN order_items oi ON o.id = oi.order_idWHERE o.cancelled_at IS NULLAND o.delivered_at IS NULLGROUP BY o.id;
-- ############################################################################-- SECTION 6: NEPTUNE SYNC VIEWS-- ############################################################################
-- Pending sync events summaryCREATE VIEW neptune_sync_pending_summary ASSELECT direction, entity_type, COUNT(*) as pending_count, MIN(created_at) as oldest_pending, MAX(created_at) as newest_pendingFROM neptune_sync_eventsWHERE sync_status = 'pending'GROUP BY direction, entity_typeORDER BY pending_count DESC;
-- Failed sync events (retryable)CREATE VIEW neptune_sync_retryable ASSELECT *FROM neptune_sync_eventsWHERE sync_status = 'failed'AND retry_count < max_retriesAND (next_retry_at IS NULL OR next_retry_at <= NOW())ORDER BY created_at ASC;
-- Sync cursor statusCREATE VIEW neptune_sync_cursor_status ASSELECT id, entity_type, direction, sync_type, last_synced_at, last_commit_num, consecutive_errors, last_error, is_activeFROM neptune_sync_cursorsORDER BY entity_type, direction;
-- ############################################################################-- COMMENTS-- ############################################################################-- These views are read-only representations.-- They cannot be used for INSERT, UPDATE, or DELETE operations.-- They are designed to simplify SELECT queries in your application.-- Always use the base tables for write operations.-- ############################################################################
-- ############################################################################-- END OF MIGRATION-- ############################################################################