name: Continuous Integration
on:
  push:
    paths-ignore:
      - 'doc/**'
    tags-ignore:
      - '*.*'
  pull_request:
    paths-ignore:
      - 'doc/**'

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2.3.4
      - name: Run linters
        uses: golangci/golangci-lint-action@v2.5.2
        with:
          version: v1.28

  unit:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        go: ['1.16', '1.15']
    steps:
      - uses: actions/checkout@v2.3.4
      - uses: actions/setup-go@v2
        with:
          go-version: ${{ matrix.go }}
      - uses: actions/cache@v2.1.6
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-
      - name: Run dialect tests
        run: go test -race ./...
        working-directory: dialect
      - name: Run schema tests
        run: go test -race ./...
        working-directory: schema
      - name: Run loader tests
        run: go test -race ./...
        working-directory: entc/load
      - name: Run codegen tests
        run: go test -race ./...
        working-directory: entc/gen
      - name: Run example tests
        working-directory: examples
        run: go test -race ./...

  generate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2.3.4
      - uses: actions/setup-go@v2
      - uses: actions/cache@v2.1.6
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-
      - name: Run go generate
        run: go generate ./...
      - name: Check generated files
        run: |
          status=$(git status --porcelain)
          if [ -n "$status" ]; then
            echo "you need to run 'go generate ./...' and commit the changes"
            echo "$status"
            exit 1
          fi

  integration:
    runs-on: ubuntu-latest
    services:
      mysql56:
        image: mysql:5.6.35
        env:
          MYSQL_DATABASE: test
          MYSQL_ROOT_PASSWORD: pass
        ports:
          - 3306:3306
        options: >-
          --health-cmd "mysqladmin ping -ppass"
          --health-interval 10s
          --health-start-period 10s
          --health-timeout 5s
          --health-retries 10
      mysql57:
        image: mysql:5.7.26
        env:
          MYSQL_DATABASE: test
          MYSQL_ROOT_PASSWORD: pass
        ports:
          - 3307:3306
        options: >-
          --health-cmd "mysqladmin ping -ppass"
          --health-interval 10s
          --health-start-period 10s
          --health-timeout 5s
          --health-retries 10
      mysql8:
        image: mysql:8
        env:
          MYSQL_DATABASE: test
          MYSQL_ROOT_PASSWORD: pass
        ports:
          - 3308:3306
        options: >-
          --health-cmd "mysqladmin ping -ppass"
          --health-interval 10s
          --health-start-period 10s
          --health-timeout 5s
          --health-retries 10
      maria:
        image: mariadb
        env:
          MYSQL_DATABASE: test
          MYSQL_ROOT_PASSWORD: pass
        ports:
          - 4306:3306
        options: >-
          --health-cmd "mysqladmin ping -ppass"
          --health-interval 10s
          --health-start-period 10s
          --health-timeout 5s
          --health-retries 10
      maria102:
        image: mariadb:10.2.32
        env:
          MYSQL_DATABASE: test
          MYSQL_ROOT_PASSWORD: pass
        ports:
          - 4307:3306
        options: >-
          --health-cmd "mysqladmin ping -ppass"
          --health-interval 10s
          --health-start-period 10s
          --health-timeout 5s
          --health-retries 10
      maria103:
        image: mariadb:10.3.13
        env:
          MYSQL_DATABASE: test
          MYSQL_ROOT_PASSWORD: pass
        ports:
          - 4308:3306
        options: >-
          --health-cmd "mysqladmin ping -ppass"
          --health-interval 10s
          --health-start-period 10s
          --health-timeout 5s
          --health-retries 10
      postgres10:
        image: postgres:10
        env:
          POSTGRES_DB: test
          POSTGRES_PASSWORD: pass
        ports:
          - 5430:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
      postgres11:
        image: postgres:11
        env:
          POSTGRES_DB: test
          POSTGRES_PASSWORD: pass
        ports:
          - 5431:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
      postgres12:
        image: postgres:12.3
        env:
          POSTGRES_DB: test
          POSTGRES_PASSWORD: pass
        ports:
          - 5433:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
      postgres13:
        image: postgres:13.1
        env:
          POSTGRES_DB: test
          POSTGRES_PASSWORD: pass
        ports:
          - 5434:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
      gremlin-server:
        image: entgo/gremlin-server
        ports:
          - 8182:8182
        options: >-
          --health-cmd "netstat -an | grep -q 8182"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    steps:
      - uses: actions/checkout@v2.3.4
      - uses: actions/setup-go@v2
      - uses: actions/cache@v2.1.6
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-
      - name: Run integration tests
        working-directory: entc/integration
        run: go test -race -count=2 -tags='json1' ./...

  migration:
    runs-on: ubuntu-latest
    if: ${{ github.ref != 'refs/heads/master' }}
    services:
      mysql56:
        image: mysql:5.6.35
        env:
          MYSQL_DATABASE: test
          MYSQL_ROOT_PASSWORD: pass
        ports:
          - 3306:3306
        options: >-
          --health-cmd "mysqladmin ping -ppass"
          --health-interval 10s
          --health-start-period 10s
          --health-timeout 5s
          --health-retries 10
      mysql57:
        image: mysql:5.7.26
        env:
          MYSQL_DATABASE: test
          MYSQL_ROOT_PASSWORD: pass
        ports:
          - 3307:3306
        options: >-
          --health-cmd "mysqladmin ping -ppass"
          --health-interval 10s
          --health-start-period 10s
          --health-timeout 5s
          --health-retries 10
      mysql8:
        image: mysql:8
        env:
          MYSQL_DATABASE: test
          MYSQL_ROOT_PASSWORD: pass
        ports:
          - 3308:3306
        options: >-
          --health-cmd "mysqladmin ping -ppass"
          --health-interval 10s
          --health-start-period 10s
          --health-timeout 5s
          --health-retries 10
      maria:
        image: mariadb
        env:
          MYSQL_DATABASE: test
          MYSQL_ROOT_PASSWORD: pass
        ports:
          - 4306:3306
        options: >-
          --health-cmd "mysqladmin ping -ppass"
          --health-interval 10s
          --health-start-period 10s
          --health-timeout 5s
          --health-retries 10
      maria102:
        image: mariadb:10.2.32
        env:
          MYSQL_DATABASE: test
          MYSQL_ROOT_PASSWORD: pass
        ports:
          - 4307:3306
        options: >-
          --health-cmd "mysqladmin ping -ppass"
          --health-interval 10s
          --health-start-period 10s
          --health-timeout 5s
          --health-retries 10
      maria103:
        image: mariadb:10.3.13
        env:
          MYSQL_DATABASE: test
          MYSQL_ROOT_PASSWORD: pass
        ports:
          - 4308:3306
        options: >-
          --health-cmd "mysqladmin ping -ppass"
          --health-interval 10s
          --health-start-period 10s
          --health-timeout 5s
          --health-retries 10
      postgres10:
        image: postgres:10
        env:
          POSTGRES_DB: test
          POSTGRES_PASSWORD: pass
        ports:
          - 5430:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
      postgres11:
        image: postgres:11
        env:
          POSTGRES_DB: test
          POSTGRES_PASSWORD: pass
        ports:
          - 5431:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
      postgres12:
        image: postgres:12.3
        env:
          POSTGRES_DB: test
          POSTGRES_PASSWORD: pass
        ports:
          - 5433:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
      postgres13:
        image: postgres:13.1
        env:
          POSTGRES_DB: test
          POSTGRES_PASSWORD: pass
        ports:
          - 5434:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
      gremlin-server:
        image: entgo/gremlin-server
        ports:
          - 8182:8182
        options: >-
          --health-cmd "netstat -an | grep -q 8182"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    steps:
      - uses: actions/checkout@v2.3.4
        with:
          fetch-depth: 0
      - uses: actions/setup-go@v2
      - uses: actions/cache@v2.1.6
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-
      - name: Checkout origin/master
        run: git checkout origin/master
      - name: Run integration on origin/master
        working-directory: entc/integration
        run: go test -race -count=2 -tags='json1' ./...
      - name: Checkout previous HEAD
        run: git checkout -
      - name: Run integration on HEAD
        working-directory: entc/integration
        run: go test -race -count=2 -tags='json1' ./...
