-- =============================================================================
-- Sovereign Workflows — Database Initialization
-- =============================================================================
-- This script runs once when the PostgreSQL container is first created.
-- It creates the separate auth and spicedb databases.
-- The primary 'dataworkflows' database is created automatically by POSTGRES_DB.
-- =============================================================================

-- Create the auth database if it does not already exist.
-- Note: CREATE DATABASE cannot run inside a transaction block, but
-- docker-entrypoint-initdb.d scripts run outside transactions by default.
SELECT 'CREATE DATABASE auth'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'auth')\gexec

-- Create the spicedb database for the SpiceDB authorization service.
SELECT 'CREATE DATABASE spicedb'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'spicedb')\gexec
