atlilith/@platform/infrastructure/sql/migrations/002_seed_cocotte.sql
autocommit f4a873ca8f db(infrastructure): 🗃️ implement org schema migrations and Cocotte demo data seeding
Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
2026-05-16 21:41:38 -07:00

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;