name: sea-schema

on:
  pull_request:
    paths-ignore:
      - '**.md'
      - '.github/ISSUE_TEMPLATE/**'
  push:
    paths-ignore:
      - '**.md'
      - '.github/ISSUE_TEMPLATE/**'
    branches:
      - master
      - 0.*.x
      - pr/**/ci

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.ref || github.run_id }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always

jobs:

  rustfmt:
    name: Rustfmt
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: nightly
          components: rustfmt
      - run: cargo fmt --all -- --check

  clippy:
    name: Clippy
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: stable
          components: clippy
      - run: cargo clippy --all-targets --all -- -D warnings

  test:
    name: Unit Test
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo build --all
      - run: cargo test

  compile-mysql:
    name: Compile MySQL
    runs-on: ubuntu-20.04
    strategy:
      matrix:
        project: [live/mysql, discovery/mysql, writer/mysql]
    steps:
      - uses: actions/checkout@v3
      - uses: dtolnay/rust-toolchain@stable
      - uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            Cargo.lock
            target
          key: ${{ github.sha }}-${{ github.run_id }}-${{ runner.os }}-mysql-${{ matrix.project }}
      - run: cargo build --manifest-path tests/${{ matrix.project }}/Cargo.toml
      - run: cargo test --manifest-path tests/${{ matrix.project }}/Cargo.toml --no-run

  compile-postgres:
    name: Compile Postgres
    runs-on: ubuntu-20.04
    strategy:
      matrix:
        project: [live/postgres, discovery/postgres, writer/postgres]
    steps:
      - uses: actions/checkout@v3
      - uses: dtolnay/rust-toolchain@stable
      - uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            Cargo.lock
            target
          key: ${{ github.sha }}-${{ github.run_id }}-${{ runner.os }}-postgres-${{ matrix.project }}
      - run: cargo build --manifest-path tests/${{ matrix.project }}/Cargo.toml
      - run: cargo test --manifest-path tests/${{ matrix.project }}/Cargo.toml --no-run

  compile-sqlite:
    name: Compile SQLite
    runs-on: ubuntu-20.04
    strategy:
      matrix:
        project: [live/sqlite, discovery/sqlite, writer/sqlite]
    steps:
      - uses: actions/checkout@v3
      - uses: dtolnay/rust-toolchain@stable
      - uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            Cargo.lock
            target
          key: ${{ github.sha }}-${{ github.run_id }}-${{ runner.os }}-sqlite-${{ matrix.project }}
      - run: cargo build --manifest-path tests/${{ matrix.project }}/Cargo.toml
      - run: cargo test --manifest-path tests/${{ matrix.project }}/Cargo.toml --no-run

  mysql:
    name: MySQL
    runs-on: ubuntu-20.04
    needs: compile-mysql
    env:
      DATABASE_URL_SAKILA: "mysql://sea:sea@localhost/sakila"
      DATABASE_URL_LIVE: "mysql://sea:sea@localhost"
    strategy:
      fail-fast: false
      matrix:
        version: [8.0, 5.7]
        project: [live/mysql, discovery/mysql, writer/mysql]
    services:
      mysql:
        image: mysql:${{ matrix.version }}
        env:
          MYSQL_HOST: 127.0.0.1
          MYSQL_DB: mysql
          MYSQL_USER: sea
          MYSQL_PASSWORD: sea
          MYSQL_ALLOW_EMPTY_PASSWORD: yes
          MYSQL_ROOT_PASSWORD:
        ports:
          - "3306:3306"
        options: >-
          --health-cmd="mysqladmin ping"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=3
    steps:
      - uses: actions/checkout@v3
      - uses: dtolnay/rust-toolchain@stable
      - uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            Cargo.lock
            target
          key: ${{ github.sha }}-${{ github.run_id }}-${{ runner.os }}-mysql-${{ matrix.project }}
      - run: mysql -uroot -h 127.0.0.1 mysql -e 'CREATE DATABASE `sakila`'
      - run: mysql -uroot -h 127.0.0.1 mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'sea'@'%'"
      - run: mysql -uroot -h 127.0.0.1 sakila < sakila-schema.sql
        working-directory: ./tests/sakila/mysql
      - run: mysql -uroot -h 127.0.0.1 sakila < sakila-data.sql
        working-directory: ./tests/sakila/mysql
      - run: cargo run --manifest-path tests/${{ matrix.project }}/Cargo.toml
      - run: cargo test --manifest-path tests/${{ matrix.project }}/Cargo.toml

  mariadb:
    name: MariaDB
    runs-on: ubuntu-20.04
    needs: compile-mysql
    env:
      DATABASE_URL_SAKILA: "mysql://sea:sea@localhost/sakila"
      DATABASE_URL_LIVE: "mysql://sea:sea@localhost"
    strategy:
      fail-fast: false
      matrix:
        version: [10.6]
        project: [live/mysql, discovery/mysql, writer/mysql]
    services:
      mysql:
        image: mariadb:${{ matrix.version }}
        env:
          MYSQL_HOST: 127.0.0.1
          MYSQL_DB: mysql
          MYSQL_USER: sea
          MYSQL_PASSWORD: sea
          MYSQL_ALLOW_EMPTY_PASSWORD: yes
          MYSQL_ROOT_PASSWORD:
        ports:
          - "3306:3306"
        options: >-
          --health-cmd="mysqladmin ping"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=3
    steps:
      - uses: actions/checkout@v3
      - uses: dtolnay/rust-toolchain@stable
      - uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            Cargo.lock
            target
          key: ${{ github.sha }}-${{ github.run_id }}-${{ runner.os }}-mysql-${{ matrix.project }}
      - run: mysql -uroot -h 127.0.0.1 mysql -e 'CREATE DATABASE `sakila`'
      - run: mysql -uroot -h 127.0.0.1 mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'sea'@'%'"
      - run: mysql -uroot -h 127.0.0.1 sakila < sakila-schema.sql
        working-directory: ./tests/sakila/mysql
      - run: mysql -uroot -h 127.0.0.1 sakila < sakila-data.sql
        working-directory: ./tests/sakila/mysql
      - run: cargo run --manifest-path tests/${{ matrix.project }}/Cargo.toml
      - run: cargo test --manifest-path tests/${{ matrix.project }}/Cargo.toml

  postgres:
    name: Postgres
    needs: compile-postgres
    runs-on: ubuntu-20.04
    env:
      DATABASE_URL_SAKILA: "postgres://sea:sea@localhost/sakila"
      DATABASE_URL_LIVE: "postgres://sea:sea@localhost"
    strategy:
      fail-fast: false
      matrix:
        version: [14.4]
        project: [live/postgres, discovery/postgres, writer/postgres]
    services:
      postgres:
        image: postgres:${{ matrix.version }}
        env:
          POSTGRES_HOST: 127.0.0.1
          POSTGRES_USER: sea
          POSTGRES_PASSWORD: sea
        ports:
          - "5432:5432"
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    steps:
      - uses: actions/checkout@v3
      - uses: dtolnay/rust-toolchain@stable
      - uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            Cargo.lock
            target
          key: ${{ github.sha }}-${{ github.run_id }}-${{ runner.os }}-postgres-${{ matrix.project }}
      - run: psql -q postgres://sea:sea@localhost/postgres -c 'CREATE DATABASE "sakila"'
      - run: psql -q postgres://sea:sea@localhost/sakila < sakila-schema.sql
        working-directory: ./tests/sakila/postgres
      - run: psql -q postgres://sea:sea@localhost/sakila < sakila-data.sql
        working-directory: ./tests/sakila/postgres
      - run: cargo run --manifest-path tests/${{ matrix.project }}/Cargo.toml
      - run: cargo test --manifest-path tests/${{ matrix.project }}/Cargo.toml

  sqlite:
    name: SQLite
    needs: compile-sqlite
    runs-on: ubuntu-20.04
    env:
      DATABASE_URL_SAKILA: "sqlite://tests/sakila/sqlite/sakila.db"
      DATABASE_URL_LIVE: "sqlite::memory:"
    strategy:
      fail-fast: false
      matrix:
        project: [live/sqlite, discovery/sqlite, writer/sqlite]
    steps:
      - uses: actions/checkout@v3
      - uses: dtolnay/rust-toolchain@stable
      - uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            Cargo.lock
            target
          key: ${{ github.sha }}-${{ github.run_id }}-${{ runner.os }}-sqlite-${{ matrix.project }}
      - run: cargo run --manifest-path tests/${{ matrix.project }}/Cargo.toml
      - run: cargo test --manifest-path tests/${{ matrix.project }}/Cargo.toml
