apiVersion: batch/v1
kind: Job
metadata:
  name: policy-migration-1764684243
  namespace: policy-service
spec:
  template:
    metadata:
      labels:
        app: policy-migration
    spec:
      serviceAccountName: policy-service-sa
      restartPolicy: Never
      containers:
      - name: migration
        image: us-central1-docker.pkg.dev/archie-v3-build/policy-service/api:latest
        command:
        - /bin/bash
        - -c
        - |
          set -e
          echo "Waiting for Cloud SQL Proxy to be ready..."
          sleep 5
          echo "Creating policy_service schema if it doesn't exist..."
          python -c "import psycopg2; conn = psycopg2.connect('postgresql://policy_service:TempMigrationPass123!@localhost:5432/archie_platform'); cur = conn.cursor(); cur.execute('CREATE SCHEMA IF NOT EXISTS policy_service'); cur.execute('CREATE TABLE IF NOT EXISTS public.organizations (id VARCHAR PRIMARY KEY)'); cur.execute('CREATE TABLE IF NOT EXISTS public.customers (id VARCHAR PRIMARY KEY)'); cur.execute('CREATE TABLE IF NOT EXISTS public.users (id VARCHAR PRIMARY KEY)'); conn.commit(); cur.close(); conn.close(); print('Schema and stub tables created successfully')"
          echo "Running Alembic migrations..."
          python -m alembic upgrade head
          echo "Migrations complete!"
        env:
        - name: DATABASE_URL
          value: "postgresql://policy_service:TempMigrationPass123!@localhost:5432/archie_platform"
      - name: cloud-sql-proxy
        image: gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.8.0
        args:
        - "--structured-logs"
        - "--port=5432"
        - "--private-ip"
        - "archie-v3-build:us-central1:dev-policy-service-db"
        securityContext:
          runAsNonRoot: true
  backoffLimit: 3
