hades-chart/hades/templates/job-dbinit.yaml
Sagar Patil 91b0f32c79 fix: use correct external DB username and allow dbinit for shared MySQL
- Change database.username from "hades_user" to "mysql_user" to match
  the shared MySQL user created by Backstage environment provisioning
- Remove mysql.enabled=true condition from dbinit job so SQL migrations
  run against external/shared MySQL databases too

Without these fixes, deploying hades with mysql.enabled=false (Backstage
shared MySQL) causes access denied because hades_user doesn't exist in
the shared MySQL, and dbinit is skipped so no tables are created.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-28 14:00:13 +05:30

138 lines
4.5 KiB
YAML

{{- if .Values.dbInit.enabled }}
apiVersion: batch/v1
kind: Job
metadata:
name: {{ include "hades.fullname" . }}-dbinit
labels:
{{- include "hades.labels" . | nindent 4 }}
app.kubernetes.io/component: dbinit
annotations:
# ArgoCD sync wave - same wave as MySQL (0), wait-for-db handles timing
argocd.argoproj.io/sync-wave: "0"
argocd.argoproj.io/hook-delete-policy: BeforeHookCreation
# Helm hooks (for non-ArgoCD deployments)
"helm.sh/hook": post-install,post-upgrade
"helm.sh/hook-weight": "-5"
"helm.sh/hook-delete-policy": before-hook-creation
spec:
backoffLimit: 5
ttlSecondsAfterFinished: 300
template:
metadata:
labels:
app.kubernetes.io/component: dbinit
{{- include "hades.selectorLabels" . | nindent 8 }}
spec:
restartPolicy: OnFailure
initContainers:
# Wait for MySQL to be ready
- name: wait-for-db
image: busybox:1.36
command:
- sh
- -c
- |
echo "Waiting for database to be ready..."
until nc -z {{ include "hades.databaseHost" . }} {{ include "hades.databasePort" . }}; do
echo "Database is unavailable - sleeping"
sleep 2
done
echo "Database is up!"
sleep 10
containers:
- name: dbinit
image: mysql:8.0
env:
- name: MYSQL_HOST
value: {{ include "hades.databaseHost" . | quote }}
- name: MYSQL_PORT
value: {{ include "hades.databasePort" . | quote }}
- name: MYSQL_DATABASE
value: {{ include "hades.databaseName" . | quote }}
- name: MYSQL_USER
value: {{ include "hades.databaseUsername" . | quote }}
- name: MYSQL_PWD
valueFrom:
secretKeyRef:
name: {{ include "hades.databaseSecretName" . }}
key: mysql-password
command:
- /bin/bash
- -c
- |
set -ex
echo "========================================="
echo "Hades Database Migration"
echo "========================================="
echo "Host: ${MYSQL_HOST}:${MYSQL_PORT}"
echo "Database: ${MYSQL_DATABASE}"
echo "User: ${MYSQL_USER}"
echo "========================================="
# Test connection
echo "Testing database connection..."
if ! mysql -h"${MYSQL_HOST}" -P"${MYSQL_PORT}" -u"${MYSQL_USER}" -e "SELECT 1" "${MYSQL_DATABASE}" &> /dev/null; then
echo "ERROR: Cannot connect to database"
exit 1
fi
echo "✓ Database connection successful"
echo ""
# Apply migrations in order
TOTAL=0
SUCCESS=0
FAILED=0
for sql_file in /migrations/*.sql; do
if [ ! -f "$sql_file" ]; then
continue
fi
filename=$(basename "$sql_file")
TOTAL=$((TOTAL + 1))
echo -n "Applying $filename... "
if mysql -h"${MYSQL_HOST}" -P"${MYSQL_PORT}" -u"${MYSQL_USER}" "${MYSQL_DATABASE}" < "$sql_file" 2>/tmp/migration_error.log; then
echo "✓ SUCCESS"
SUCCESS=$((SUCCESS + 1))
else
echo "✗ FAILED"
echo "Error details:"
cat /tmp/migration_error.log
FAILED=$((FAILED + 1))
# Stop on first error
echo ""
echo "========================================="
echo "Migration failed at: $filename"
echo "========================================="
exit 1
fi
done
echo ""
echo "========================================="
echo "Migration Summary:"
echo " Total: $TOTAL"
echo " Success: $SUCCESS"
echo " Failed: $FAILED"
echo "========================================="
if [ $FAILED -eq 0 ]; then
echo "✓ All migrations completed successfully!"
exit 0
else
echo "✗ Some migrations failed"
exit 1
fi
volumeMounts:
- name: sql-migrations
mountPath: /migrations
volumes:
- name: sql-migrations
configMap:
name: {{ include "hades.fullname" . }}-sql-migrations
{{- end }}