apiVersion: batch/v1
kind: Job
metadata:
  name: lti-db-fresh-manual
  namespace: dev-lti
spec:
  backoffLimit: 0
  ttlSecondsAfterFinished: 300
  template:
    spec:
      restartPolicy: Never
      containers:
        - name: fresh
          image: 886436954922.dkr.ecr.ap-southeast-3.amazonaws.com/mbugroup/lti-api:dev-f8ca404b
          imagePullPolicy: IfNotPresent
          command: ["/bin/sh", "-c"]
          args:
            - |
              set -eu
              if [ "${ALLOW_DB_RESET:-}" != "YES" ]; then
                echo "Refusing to reset DB. Set ALLOW_DB_RESET=YES to continue."
                exit 1
              fi

              DATABASE_URL="postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}?sslmode=${DB_SSLMODE:-disable}"
              echo "Resetting schema public on ${DB_HOST}:${DB_PORT}/${DB_NAME}"

              psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -c "DROP SCHEMA IF EXISTS public CASCADE;"
              psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -c "CREATE SCHEMA public;"

              echo "Running migrations after reset"
              /app/migrate -path /app/migrations -database "$DATABASE_URL" up
          env:
            - name: ALLOW_DB_RESET
              value: "NO"
          envFrom:
            - secretRef:
                name: lti-env-dev
