Skip to content

Commit 1ff4dde

Browse files
committed
fix: make DATABASE_PHASE2.sql self-contained (add handle_updated_at and is_admin prerequisites)
1 parent a96ac11 commit 1ff4dde

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

DATABASE_PHASE2.sql

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
11
-- ============================================================
22
-- LMXEngine · DINA COSMETIC · PHASE 2 SCHEMA EXPANSION
33
-- Run this in Supabase SQL Editor → New Query
4+
-- SELF-CONTAINED: Works even if DATABASE.sql was not run first.
45
-- Safe to run multiple times (uses IF NOT EXISTS / ON CONFLICT)
56
-- ============================================================
67

8+
-- ─────────────────────────────────────────────
9+
-- 0. PREREQUISITES — ensure core helpers exist
10+
-- ─────────────────────────────────────────────
11+
12+
-- is_admin() helper (idempotent)
13+
CREATE OR REPLACE FUNCTION public.is_admin()
14+
RETURNS boolean
15+
LANGUAGE sql
16+
SECURITY DEFINER
17+
STABLE
18+
SET search_path = public
19+
AS $$
20+
SELECT EXISTS (
21+
SELECT 1 FROM public.profiles
22+
WHERE id = auth.uid() AND role = 'admin'
23+
);
24+
$$;
25+
26+
-- updated_at trigger function (idempotent)
27+
CREATE OR REPLACE FUNCTION public.handle_updated_at()
28+
RETURNS TRIGGER
29+
LANGUAGE plpgsql
30+
SECURITY DEFINER
31+
SET search_path = public
32+
AS $$
33+
BEGIN
34+
NEW.updated_at = now();
35+
RETURN NEW;
36+
END;
37+
$$;
38+
739
-- ─────────────────────────────────────────────
840
-- 1. FIX ORDERS TABLE (add missing user_id column)
941
-- ─────────────────────────────────────────────

0 commit comments

Comments
 (0)