name: Test Go

on: [push, pull_request]

permissions:
  contents: read

jobs:
  test:
    strategy:
      matrix:
        go-version: [ '1.24.x', '1.25.x' ]
        os: [ubuntu-latest]
    runs-on: ${{ matrix.os }}

    steps:
      - name: Fetch repo
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          persist-credentials: false

      - name: Install Go
        uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0
        with:
          go-version: ${{ matrix.go-version }}

      - name: Run tests
        run: go test -v -race ./...

  test-mysql:
    env:
      DB_DATABASE: test_tessera
      DB_USER: root
      DB_PASSWORD: root

    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          persist-credentials: false
      - name: Start MySQL
        run: |
          sudo /etc/init.d/mysql start
          mysql -e "CREATE DATABASE IF NOT EXISTS $DB_DATABASE;" -u$DB_USER -p$DB_PASSWORD
      - name: Test with Go
        run: go test -v -race ./storage/mysql/... -is_mysql_test_optional=false

  test-aws-mysql:
    env:
      DB_DATABASE: test_tessera
      DB_USER: root
      DB_PASSWORD: root

    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          persist-credentials: false
      - name: Start MySQL
        run: |
          sudo /etc/init.d/mysql start
          mysql -e "CREATE DATABASE IF NOT EXISTS $DB_DATABASE;" -u$DB_USER -p$DB_PASSWORD
      - name: Test with Go
        # Parallel tests are disabled for the MySQL test database to always be in a known state.
        run: go test -p=1 -v -race ./storage/aws/... -is_mysql_test_optional=false
