name: CI

on:
  push:
  pull_request:

jobs:
  test:
    name: PostgreSQL ${{ matrix.pg }}
    runs-on: ubuntu-latest
    container: pgxn/pgxn-tools
    strategy:
      fail-fast: false
      matrix:
        pg: [15, 16, 17, 18]
    steps:
      - name: Start PostgreSQL ${{ matrix.pg }}
        run: pg-start ${{ matrix.pg }}
      - name: Check out the code
        uses: actions/checkout@v5
      - name: Build and run the regression tests
        run: pg-build-test

  docker:
    name: Docker image
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - name: Build the image
        run: docker build -t pg_synonym .
      - name: Smoke test
        run: |
          docker run -d --name pg -e POSTGRES_PASSWORD=postgres pg_synonym
          for i in $(seq 1 30); do
            if docker exec pg psql -U postgres -h localhost -v ON_ERROR_STOP=1 \
                 -c "CREATE EXTENSION IF NOT EXISTS pg_synonym" \
                 -c "CREATE TABLE IF NOT EXISTS t (id int)" \
                 -c "SELECT synonym.create('s', 't')" \
                 -c "ALTER TABLE t ALTER COLUMN id TYPE bigint" \
                 -c "SELECT status = 'ok' AS ok FROM synonym.list" ; then
              exit 0
            fi
            sleep 2
          done
          docker logs pg
          exit 1
