apiVersion: v1
kind: Pod
metadata:
  name: create-ci-tables
spec:
  tolerations:
   - key: preemptible
     value: "true"
  containers:
  - name: create-ci-tables
    image: "{{ base_image.image }}"
    env:
     - name: CREATE_TABLES_SQL
       value: |
         CREATE TABLE IF NOT EXISTS authorized_shas (
           sha VARCHAR(100) NOT NULL
         ) ENGINE = InnoDB;
         CREATE INDEX authorized_shas_sha ON authorized_shas (sha);
    command:
     - /bin/bash
     - -c
     - |
       set -ex
       # index creation isn't idempotent, don't create if a table exists
       T=$(echo "SHOW TABLES LIKE 'authorized_shas';" | mysql --defaults-extra-file=/secrets/ci-admin/sql-config.cnf -s)
       if [ "$T" != "authorized_shas" ]; then
         echo "$CREATE_TABLES_SQL" | mysql --defaults-extra-file=/secrets/ci-admin/sql-config.cnf
       fi
    volumeMounts:
      - mountPath: /secrets/ci-admin
        readOnly: true
        name: ci-admin-secret
  volumes:
    - name: ci-admin-secret
      secret:
        secretName: "{{ ci_database.admin_secret_name }}"
  restartPolicy: Never
