-- CHORUS Services Database Initialization

-- Create databases for each service
CREATE DATABASE chorus_whoosh;
CREATE DATABASE chorus_slurp;
CREATE DATABASE chorus_rl_tuner;  
CREATE DATABASE chorus_monitoring;
CREATE DATABASE chorus_website;

-- Grant permissions
GRANT ALL PRIVILEGES ON DATABASE chorus_whoosh TO chorus;
GRANT ALL PRIVILEGES ON DATABASE chorus_slurp TO chorus;
GRANT ALL PRIVILEGES ON DATABASE chorus_rl_tuner TO chorus;
GRANT ALL PRIVILEGES ON DATABASE chorus_monitoring TO chorus;
GRANT ALL PRIVILEGES ON DATABASE chorus_website TO chorus;

-- Connect to chorus_website database to set up migration tracking
\c chorus_website;

-- Create migration tracking table
CREATE TABLE IF NOT EXISTS schema_migrations (
    version VARCHAR(255) PRIMARY KEY,
    applied_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Grant permissions on migration table
GRANT SELECT, INSERT ON schema_migrations TO chorus;