-- Run as a PostgreSQL superuser (e.g. postgres).
-- Example:
--   "C:\Program Files\PostgreSQL\18\bin\psql.exe" -U postgres -f scripts/setup-local-db.sql

DO $$
BEGIN
  IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'archi') THEN
    CREATE ROLE archi LOGIN PASSWORD 'archi';
  ELSE
    ALTER ROLE archi WITH LOGIN PASSWORD 'archi';
  END IF;
END
$$;

SELECT 'CREATE DATABASE archi OWNER archi'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'archi')\gexec

GRANT ALL PRIVILEGES ON DATABASE archi TO archi;
