Blog Post
عنوان المحادثة: شوف هيكل قاعدة البيانات في ملف الأدمن وشوف فين الأخطاء والإصلاح المناس...
التاريخ: 12.02.2026
التصنيف: 📊 البيانات وتحليل البيانات
إجمالي الرسائل: 3 | ياسر: 2 | M: 1
M
-- Drop all tables first (in correct order due to dependencies)DROP TABLE IF EXISTS logs CASCADE;DROP TABLE IF EXISTS rate_limits CASCADE;DROP TABLE IF EXISTS fees_archive CASCADE;DROP TABLE IF EXISTS supplier_performance CASCADE;DROP TABLE IF EXISTS quotes CASCADE;DROP TABLE IF EXISTS request_assignments CASCADE;DROP TABLE IF EXISTS customer_requests CASCADE;DROP TABLE IF EXISTS active_draft_orders CASCADE;DROP TABLE IF EXISTS customers CASCADE;DROP TABLE IF EXISTS suppliers CASCADE;-- DROP TABLE IF EXISTS system_config CASCADE;DROP VIEW IF EXISTS dashboard_stats CASCADE;DROP FUNCTION IF EXISTS update_updated_at_column() CASCADE;-- Core tables with proper constraintsCREATE TABLE IF NOT EXISTS suppliers ( id SERIAL PRIMARY KEY, company_name TEXT NOT NULL, city TEXT, seller_name TEXT, phone_number TEXT UNIQUE NOT NULL, location TEXT, rating_avg NUMERIC(3, 1) DEFAULT 0 CHECK (rating_avg >= 0 AND rating_avg brands TEXT[] DEFAULT '{}', maps TEXT, telegram_chat_id TEXT, rotation_index INTEGER DEFAULT 0, is_held BOOLEAN DEFAULT FALSE, hold_until TIMESTAMP, created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP);CREATE INDEX IF NOT EXISTS idx_suppliers_city ON suppliers(city);CREATE INDEX IF NOT EXISTS idx_suppliers_held ON suppliers(is_held, hold_until);CREATE INDEX IF NOT EXISTS idx_suppliers_rotation ON suppliers(rotation_index);CREATE INDEX IF NOT EXISTS idx_suppliers_telegram ON suppliers(telegram_chat_id);CREATE TABLE IF NOT EXISTS customers ( id SERIAL PRIMARY KEY, phone_number BIGINT UNIQUE NOT NULL, name TEXT NOT NULL, telegram_chat_id TEXT, is_okay BOOLEAN DEFAULT FALSE, is_debtable BOOLEAN DEFAULT FALSE, is_waiting BOOLEAN DEFAULT FALSE, created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, CONSTRAINT valid_phone CHECK (phone_number > 0));CREATE INDEX IF NOT EXISTS idx_customers_phone ON customers(phone_number);CREATE INDEX IF NOT EXISTS idx_customers_telegram ON customers(telegram_chat_id);CREATE INDEX IF NOT EXISTS idx_customers_debt ON customers(is_debtable) WHERE is_debtable = TRUE;CREATE TABLE IF NOT EXISTS customer_requests ( id BIGSERIAL PRIMARY KEY, customer_id INTEGER REFERENCES customers(id) ON DELETE CASCADE, request_details JSONB NOT NULL DEFAULT '{}', request_result TEXT, status TEXT DEFAULT 'open' CHECK (status IN ('open', 'closed', 'cancelled')), offers_count INTEGER DEFAULT 0 CHECK (offers_count >= 0), created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, closes_at TIMESTAMP WITH TIME ZONE, CONSTRAINT valid_request CHECK (jsonb_typeof(request_details) = 'object'));CREATE INDEX IF NOT EXISTS idx_requests_status ON customer_requests(status);CREATE INDEX IF NOT EXISTS idx_requests_customer ON customer_requests(customer_id);CREATE INDEX IF NOT EXISTS idx_requests_created ON customer_requests(created_at);CREATE TABLE IF NOT EXISTS request_assignments ( id BIGSERIAL PRIMARY KEY, request_id BIGINT REFERENCES customer_requests(id) ON DELETE CASCADE, supplier_id INTEGER REFERENCES suppliers(id) ON DELETE CASCADE, status TEXT DEFAULT 'pending' CHECK (status IN ('pending', 'responded', 'ignored', 'not_available')), assigned_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, responded_at TIMESTAMP WITH TIME ZONE, wa_message_id TEXT, telegram_message_id TEXT, created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, UNIQUE(request_id, supplier_id));
12.02.2026 01:12