#!/bin/sh
set -e

DB_NAME="${DB_MYSQLDB_DATABASE:-n8n}"
DB_USER="${DB_MYSQLDB_USER:-n8n}"
DB_PASS="${DB_MYSQLDB_PASSWORD:-n8npass}"

echo "[init] creating n8n database '$DB_NAME' and user '$DB_USER'..."

mysql -u root -p"$MARIADB_ROOT_PASSWORD" <<SQL
CREATE DATABASE IF NOT EXISTS \`${DB_NAME}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER IF NOT EXISTS '${DB_USER}'@'%' IDENTIFIED BY '${DB_PASS}';
GRANT ALL PRIVILEGES ON \`${DB_NAME}\`.* TO '${DB_USER}'@'%';
FLUSH PRIVILEGES;
SQL

echo "[init] n8n DB/user ready."
