-- Подписка «сообщить о поступлении» CREATE TABLE IF NOT EXISTS product_stock_alerts ( id SERIAL PRIMARY KEY, product_id INTEGER NOT NULL REFERENCES products(id) ON DELETE CASCADE, email TEXT NOT NULL, user_id INTEGER REFERENCES users(id) ON DELETE SET NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), notified_at TIMESTAMPTZ, UNIQUE (product_id, email) ); CREATE INDEX IF NOT EXISTS idx_stock_alerts_product_pending ON product_stock_alerts (product_id) WHERE notified_at IS NULL;