-- Bootstraps the `mule_sidecar` database + user used by the Go sidecar
-- service (per-user heap sharing + folder mutations, stood up in M4).
--
-- MariaDB runs every .sql in /docker-entrypoint-initdb.d ONCE, on first
-- boot of a fresh data volume. Subsequent boots are no-ops.
--
-- The sidecar's MariaDB user is intentionally scoped to `mule_sidecar.*`
-- only — it never has access to PhotoPrism's schema.

CREATE DATABASE IF NOT EXISTS mule_sidecar
  CHARACTER SET utf8mb4
  COLLATE utf8mb4_unicode_ci;

-- The password here is substituted at compose build time via envsubst,
-- but MariaDB's init script doesn't expand vars in .sql files. So we use
-- a literal placeholder that the user replaces locally — or, simpler,
-- we let the M4 sidecar bring-up script create the user via SQL with the
-- env-var password. Keeping a placeholder here makes the schema visible
-- in source control without leaking creds.
--
-- TODO (M4): replace this block with an entrypoint that templates the
-- password from $SIDECAR_DB_PASSWORD before MariaDB reads the file.
CREATE USER IF NOT EXISTS 'sidecar'@'%' IDENTIFIED BY 'replace-at-m4-bringup';
GRANT ALL PRIVILEGES ON mule_sidecar.* TO 'sidecar'@'%';
FLUSH PRIVILEGES;
