db: core-db schema — brands, users, memberships, platforms

This commit is contained in:
2026-05-06 15:49:00 +00:00
parent cf41a25721
commit a47bd31374
+76
View File
@@ -0,0 +1,76 @@
-- manar-core-db schema: registry layer
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
CREATE TABLE IF NOT EXISTS brands (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
slug TEXT UNIQUE NOT NULL,
name TEXT NOT NULL,
timezone TEXT NOT NULL DEFAULT 'Asia/Riyadh',
status TEXT NOT NULL DEFAULT 'active',
trust_level INT NOT NULL DEFAULT 1,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email TEXT UNIQUE NOT NULL,
name TEXT,
role TEXT NOT NULL DEFAULT 'brand_admin',
hashed_pw TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
last_login TIMESTAMPTZ
);
CREATE TABLE IF NOT EXISTS brand_memberships (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
brand_id UUID NOT NULL REFERENCES brands(id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
role TEXT NOT NULL DEFAULT 'viewer',
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
UNIQUE (brand_id, user_id)
);
CREATE TABLE IF NOT EXISTS platforms (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
slug TEXT UNIQUE NOT NULL,
name TEXT NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE
);
CREATE TABLE IF NOT EXISTS audit_log_global (
id BIGSERIAL PRIMARY KEY,
brand_id UUID REFERENCES brands(id),
user_id UUID REFERENCES users(id),
action TEXT NOT NULL,
entity_type TEXT,
entity_id TEXT,
payload JSONB,
ip TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS super_admin_actions (
id BIGSERIAL PRIMARY KEY,
user_id UUID REFERENCES users(id),
action TEXT NOT NULL,
target_type TEXT,
target_id TEXT,
notes TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
INSERT INTO platforms (slug, name) VALUES
('google_ads', 'Google Ads'),
('meta', 'Meta Ads'),
('tiktok', 'TikTok Ads'),
('snap', 'Snap Ads'),
('x', 'X Ads'),
('linkedin', 'LinkedIn Ads')
ON CONFLICT (slug) DO NOTHING;
INSERT INTO brands (slug, name, timezone, trust_level) VALUES
('alsalama', 'Al Salama Medical', 'Asia/Riyadh', 1),
('asktam', 'Ask Tam', 'Asia/Riyadh', 1)
ON CONFLICT (slug) DO NOTHING;