diff --git a/docker/Dockerfile b/docker/Dockerfile index 78f9f11..9ccb043 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -8,6 +8,7 @@ ENV PYTHONPATH=/app RUN apt-get update && apt-get install -y --no-install-recommends \ gcc \ libpq-dev \ + postgresql-client \ curl \ && rm -rf /var/lib/apt/lists/* @@ -19,6 +20,7 @@ RUN pip install --no-cache-dir -r requirements.txt COPY services/ /app/services/ COPY tests/ /app/tests/ COPY conftest.py /app/conftest.py +COPY infra/migrations/ /app/infra/migrations/ RUN useradd -m -u 1000 stonks && \ chown -R stonks:stonks /app diff --git a/infra/helm/stonks-oracle/templates/deployments.yaml b/infra/helm/stonks-oracle/templates/deployments.yaml index 898b2f1..2de060a 100644 --- a/infra/helm/stonks-oracle/templates/deployments.yaml +++ b/infra/helm/stonks-oracle/templates/deployments.yaml @@ -28,6 +28,38 @@ spec: {{- end }} securityContext: {{- 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: - name: {{ $svc.image }} image: {{ $root.Values.image.registry }}/{{ $svc.image }}:{{ $root.Values.image.tag }} diff --git a/pipelines/runmefirst.sh b/pipelines/runmefirst.sh index 44cc4df..d97ff7b 100755 --- a/pipelines/runmefirst.sh +++ b/pipelines/runmefirst.sh @@ -314,20 +314,31 @@ for db in stonks_beta stonks_paper; do echo " ✓ Database $db exists" 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" 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..." for migration in $(ls "$REPO_ROOT/infra/migrations/"*.sql 2>/dev/null | sort); do - kubectl exec -n postgresql-service "$PG_POD" -c postgres -- \ - psql -U stonks -d "$db" -f - < "$migration" > /dev/null 2>&1 || true + kubectl exec -i -n postgresql-service "$PG_POD" -c postgres -- \ + psql -U postgres -d "$db" < "$migration" > /dev/null 2>&1 || true done echo " ✓ Migrations applied to $db" done else echo " ⚠ Migrations directory not found at $REPO_ROOT/infra/migrations — skipping" 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 "" # -------------------------------------------------------