35 lines
1.1 KiB
PL/PgSQL
35 lines
1.1 KiB
PL/PgSQL
-- Migration: 002_seed_cocotte_org.sql
|
|
-- Purpose: Seed the initial Cocotte org with transquinnftw as owner.
|
|
-- This is the inaugural Org tenant; transquinnftw remains a
|
|
-- full Person tenant in parallel (dual model — DESIGN.md §2).
|
|
--
|
|
-- Idempotent: safe to re-run; uses ON CONFLICT clauses.
|
|
-- Prereq: 001_add_orgs.sql applied; user `transquinnftw` exists.
|
|
|
|
BEGIN;
|
|
|
|
-- Cocotte org. Owner trigger from migration 001 will auto-insert
|
|
-- transquinnftw into org_members with role='owner'.
|
|
INSERT INTO orgs (slug, name, owner_id)
|
|
SELECT 'cocotte', 'Cocotte', u.id
|
|
FROM users u
|
|
WHERE u.slug = 'transquinnftw'
|
|
ON CONFLICT (slug) DO NOTHING;
|
|
|
|
-- Sanity: verify membership was created by the trigger
|
|
DO $$
|
|
DECLARE
|
|
member_count INTEGER;
|
|
BEGIN
|
|
SELECT COUNT(*) INTO member_count
|
|
FROM org_members om
|
|
JOIN orgs o ON o.id = om.org_id
|
|
JOIN users u ON u.id = om.user_id
|
|
WHERE o.slug = 'cocotte' AND u.slug = 'transquinnftw' AND om.role = 'owner';
|
|
|
|
IF member_count <> 1 THEN
|
|
RAISE EXCEPTION 'cocotte owner seed failed: expected 1 owner row, got %', member_count;
|
|
END IF;
|
|
END $$;
|
|
|
|
COMMIT;
|