feat: auto-run migrations via psql init container on scheduler startup
This commit is contained in:
@@ -8,6 +8,7 @@ ENV PYTHONPATH=/app
|
|||||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
gcc \
|
gcc \
|
||||||
libpq-dev \
|
libpq-dev \
|
||||||
|
postgresql-client \
|
||||||
curl \
|
curl \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
@@ -19,6 +20,7 @@ RUN pip install --no-cache-dir -r requirements.txt
|
|||||||
COPY services/ /app/services/
|
COPY services/ /app/services/
|
||||||
COPY tests/ /app/tests/
|
COPY tests/ /app/tests/
|
||||||
COPY conftest.py /app/conftest.py
|
COPY conftest.py /app/conftest.py
|
||||||
|
COPY infra/migrations/ /app/infra/migrations/
|
||||||
|
|
||||||
RUN useradd -m -u 1000 stonks && \
|
RUN useradd -m -u 1000 stonks && \
|
||||||
chown -R stonks:stonks /app
|
chown -R stonks:stonks /app
|
||||||
|
|||||||
@@ -28,6 +28,38 @@ spec:
|
|||||||
{{- end }}
|
{{- end }}
|
||||||
securityContext:
|
securityContext:
|
||||||
{{- include "stonks.podSecurityContext" $root | nindent 8 }}
|
{{- include "stonks.podSecurityContext" $root | nindent 8 }}
|
||||||
|
{{- if eq $svc.image "scheduler" }}
|
||||||
|
initContainers:
|
||||||
|
- name: run-migrations
|
||||||
|
image: {{ $root.Values.image.registry }}/{{ $svc.image }}:{{ $root.Values.image.tag }}
|
||||||
|
imagePullPolicy: {{ $root.Values.image.pullPolicy }}
|
||||||
|
command: ["sh", "-c"]
|
||||||
|
args:
|
||||||
|
- |
|
||||||
|
for f in $(ls /app/infra/migrations/*.sql 2>/dev/null | sort); do
|
||||||
|
echo "Applying $(basename $f)..."
|
||||||
|
PGPASSWORD="$POSTGRES_PASSWORD" psql \
|
||||||
|
-h "$POSTGRES_HOST" -p "$POSTGRES_PORT" \
|
||||||
|
-U "$POSTGRES_USER" -d "$POSTGRES_DB" \
|
||||||
|
-f "$f" -v ON_ERROR_STOP=0 2>&1 | tail -1 || true
|
||||||
|
done
|
||||||
|
echo "Migrations complete."
|
||||||
|
securityContext:
|
||||||
|
{{- include "stonks.containerSecurityContext" $root | nindent 12 }}
|
||||||
|
envFrom:
|
||||||
|
- configMapRef:
|
||||||
|
name: stonks-config
|
||||||
|
{{- range $svc.secrets }}
|
||||||
|
- secretRef:
|
||||||
|
name: {{ . }}
|
||||||
|
{{- end }}
|
||||||
|
resources:
|
||||||
|
requests: { cpu: 50m, memory: 64Mi }
|
||||||
|
limits: { cpu: 200m, memory: 128Mi }
|
||||||
|
volumeMounts:
|
||||||
|
- name: tmp
|
||||||
|
mountPath: /tmp
|
||||||
|
{{- end }}
|
||||||
containers:
|
containers:
|
||||||
- name: {{ $svc.image }}
|
- name: {{ $svc.image }}
|
||||||
image: {{ $root.Values.image.registry }}/{{ $svc.image }}:{{ $root.Values.image.tag }}
|
image: {{ $root.Values.image.registry }}/{{ $svc.image }}:{{ $root.Values.image.tag }}
|
||||||
|
|||||||
+15
-4
@@ -314,20 +314,31 @@ for db in stonks_beta stonks_paper; do
|
|||||||
echo " ✓ Database $db exists"
|
echo " ✓ Database $db exists"
|
||||||
done
|
done
|
||||||
|
|
||||||
# Run migrations on beta and paper databases
|
# Run migrations on all three databases (idempotent — IF NOT EXISTS in DDL)
|
||||||
REPO_ROOT="${SCRIPT_DIR}/../stonks-oracle"
|
REPO_ROOT="${SCRIPT_DIR}/../stonks-oracle"
|
||||||
if [ -d "$REPO_ROOT/infra/migrations" ]; then
|
if [ -d "$REPO_ROOT/infra/migrations" ]; then
|
||||||
for db in stonks_beta stonks_paper; do
|
for db in stonks stonks_beta stonks_paper; do
|
||||||
echo " Running migrations on $db..."
|
echo " Running migrations on $db..."
|
||||||
for migration in $(ls "$REPO_ROOT/infra/migrations/"*.sql 2>/dev/null | sort); do
|
for migration in $(ls "$REPO_ROOT/infra/migrations/"*.sql 2>/dev/null | sort); do
|
||||||
kubectl exec -n postgresql-service "$PG_POD" -c postgres -- \
|
kubectl exec -i -n postgresql-service "$PG_POD" -c postgres -- \
|
||||||
psql -U stonks -d "$db" -f - < "$migration" > /dev/null 2>&1 || true
|
psql -U postgres -d "$db" < "$migration" > /dev/null 2>&1 || true
|
||||||
done
|
done
|
||||||
echo " ✓ Migrations applied to $db"
|
echo " ✓ Migrations applied to $db"
|
||||||
done
|
done
|
||||||
else
|
else
|
||||||
echo " ⚠ Migrations directory not found at $REPO_ROOT/infra/migrations — skipping"
|
echo " ⚠ Migrations directory not found at $REPO_ROOT/infra/migrations — skipping"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Seed symbol registry data on all three databases
|
||||||
|
if [ -d "$REPO_ROOT/services/symbol_registry" ]; then
|
||||||
|
for db in stonks stonks_beta stonks_paper; do
|
||||||
|
echo " Seeding $db..."
|
||||||
|
POSTGRES_HOST=postgresql-rw.postgresql-service.svc.cluster.local \
|
||||||
|
POSTGRES_PASSWORD='St0nks0racl3!' POSTGRES_USER=stonks POSTGRES_DB="$db" \
|
||||||
|
python3 -m services.symbol_registry.seed 2>/dev/null || true
|
||||||
|
echo " ✓ Seed data applied to $db"
|
||||||
|
done
|
||||||
|
fi
|
||||||
echo ""
|
echo ""
|
||||||
|
|
||||||
# -------------------------------------------------------
|
# -------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user