diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8873f95e..463d41dd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -48,59 +48,175 @@ jobs: test-postgres: name: postgres integration test runs-on: ubuntu-24.04 + env: + MIX_ENV: test + ELIXIR_ASSERT_TIMEOUT: 10000 + ECTO_ADAPTER: pg + PG_URL: postgres:postgres@127.0.0.1 + POOL_COUNT: ${{ matrix.pool_count }} strategy: fail-fast: false matrix: - elixirbase: - - "1.15.6-erlang-25.3.2.6-alpine-3.18.4" + elixir: ["1.15.6"] + otp: ["25.3.2.6"] postgres: - "16.2-alpine" - "11.11-alpine" - "9.6-alpine" - pool_count: - - "1" + pool_count: ["1"] include: - - elixirbase: "1.15.6-erlang-25.3.2.6-alpine-3.18.4" + - elixir: "1.15.6" + otp: "25.3.2.6" postgres: "16.2-alpine" pool_count: "4" - steps: - - uses: earthly/actions-setup@v1 - - uses: actions/checkout@v7 - - name: test ecto_sql + services: + postgres: + image: postgres:${{ matrix.postgres }} env: - POOL_COUNT: ${{ matrix.pool_count || '1' }} - run: earthly -P --ci --build-arg ELIXIR_BASE=${{matrix.elixirbase}} --build-arg POSTGRES=${{matrix.postgres}} +integration-test-postgres + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: postgres + ports: + - 5432:5432 + options: >- + --health-cmd pg_isready + --health-interval 2s + --health-timeout 5s + --health-retries 30 + steps: + - name: Checkout + uses: actions/checkout@v7 + - name: Install Elixir and Erlang + uses: erlef/setup-beam@v1 + with: + elixir-version: ${{ matrix.elixir }} + otp-version: ${{ matrix.otp }} + - name: Restore deps and _build cache + uses: actions/cache@v6 + with: + path: | + deps + _build + key: ${{ runner.os }}-${{ matrix.elixir }}-${{ matrix.otp }}-${{ hashFiles('**/mix.lock') }} + restore-keys: | + ${{ runner.os }}-${{ matrix.elixir }}-${{ matrix.otp }}- + - name: Install dependencies + run: mix deps.get + - name: Run tests + run: mix test test-mysql: name: mysql integration test runs-on: ubuntu-24.04 + env: + MIX_ENV: test + ELIXIR_ASSERT_TIMEOUT: 10000 + ECTO_ADAPTER: myxql + MYSQL_URL: root:root@127.0.0.1 strategy: fail-fast: false matrix: - elixirbase: - - "1.15.6-erlang-25.3.2.6-alpine-3.18.4" + elixir: ["1.15.6"] + otp: ["25.3.2.6"] mysql: - "5.7" - "8.0" steps: - - uses: earthly/actions-setup@v1 - - uses: actions/checkout@v7 - - name: test ecto_sql - run: earthly -P --ci --build-arg ELIXIR_BASE=${{matrix.elixirbase}} --build-arg MYSQL=${{matrix.mysql}} +integration-test-mysql + - name: Start mysql + run: | + # The default authentication plugin for MySQL 8 is sha 256 but it + # doesn't come with the docker image, so we fall back to the 5.7 way + docker run --name mysql -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root "mysql:${{ matrix.mysql }}" \ + --sql_mode="ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION,ANSI_QUOTES" \ + --default-authentication-plugin=mysql_native_password + # The mysqldump 8 client on the runner queries column statistics, + # which do not exist in MySQL 5.7 + printf '[mysqldump]\ncolumn-statistics=0\n' > ~/.my.cnf + - name: Checkout + uses: actions/checkout@v7 + - name: Install Elixir and Erlang + uses: erlef/setup-beam@v1 + with: + elixir-version: ${{ matrix.elixir }} + otp-version: ${{ matrix.otp }} + - name: Restore deps and _build cache + uses: actions/cache@v6 + with: + path: | + deps + _build + key: ${{ runner.os }}-${{ matrix.elixir }}-${{ matrix.otp }}-${{ hashFiles('**/mix.lock') }} + restore-keys: | + ${{ runner.os }}-${{ matrix.elixir }}-${{ matrix.otp }}- + - name: Install dependencies + run: mix deps.get + - name: Wait for mysql + run: | + for i in $(seq 1 60); do + mysqladmin ping --host=127.0.0.1 --port=3306 --protocol=TCP --silent && exit 0 + echo "waiting for mysql" + sleep 2 + done + echo "timed out waiting for mysql" + exit 1 + - name: Run tests + run: mix test test-mssql: name: mssql integration test runs-on: ubuntu-24.04 + env: + MIX_ENV: test + ELIXIR_ASSERT_TIMEOUT: 10000 + ECTO_ADAPTER: tds + MSSQL_URL: sa:some!Password@127.0.0.1 strategy: fail-fast: false matrix: - elixirbase: - - "1.15.6-erlang-25.3.2.6-alpine-3.18.4" + elixir: ["1.15.6"] + otp: ["25.3.2.6"] mssql: - "2019" - "2022" + services: + mssql: + image: mcr.microsoft.com/mssql/server:${{ matrix.mssql }}-latest + env: + ACCEPT_EULA: Y + MSSQL_SA_PASSWORD: some!Password + ports: + - 1433:1433 steps: - - uses: earthly/actions-setup@v1 - - uses: actions/checkout@v7 - - name: test ecto_sql - run: earthly -P --ci --build-arg ELIXIR_BASE=${{matrix.elixirbase}} --build-arg MSSQL=${{matrix.mssql}} +integration-test-mssql + - name: Checkout + uses: actions/checkout@v7 + - name: Install Elixir and Erlang + uses: erlef/setup-beam@v1 + with: + elixir-version: ${{ matrix.elixir }} + otp-version: ${{ matrix.otp }} + - name: Restore deps and _build cache + uses: actions/cache@v6 + with: + path: | + deps + _build + key: ${{ runner.os }}-${{ matrix.elixir }}-${{ matrix.otp }}-${{ hashFiles('**/mix.lock') }} + restore-keys: | + ${{ runner.os }}-${{ matrix.elixir }}-${{ matrix.otp }}- + - name: Install dependencies + run: mix deps.get + - name: Wait for mssql + run: | + for i in $(seq 1 60); do + for sqlcmd in /opt/mssql-tools18/bin/sqlcmd /opt/mssql-tools/bin/sqlcmd; do + if docker exec ${{ job.services.mssql.id }} $sqlcmd -C -S localhost -U sa -P 'some!Password' -Q "SELECT 1" >/dev/null 2>&1; then + exit 0 + fi + done + echo "waiting for mssql" + sleep 2 + done + echo "timed out waiting for mssql" + exit 1 + - name: Run tests + run: mix test diff --git a/Earthfile b/Earthfile deleted file mode 100644 index 33edb41d..00000000 --- a/Earthfile +++ /dev/null @@ -1,127 +0,0 @@ -VERSION 0.6 - -all: - ARG ELIXIR_BASE=1.15.6-erlang-25.3.2.6-alpine-3.18.4 - BUILD \ - --build-arg POSTGRES=16.2-alpine \ - --build-arg POSTGRES=11.11-alpine \ - --build-arg POSTGRES=9.6-alpine \ - --build-arg POSTGRES=9.5-alpine \ - +integration-test-postgres - - BUILD \ - --build-arg MYSQL=5.7 \ - --build-arg MYSQL=8.0 \ - +integration-test-mysql - - BUILD \ - --build-arg MSSQL=2017 \ - --build-arg MSSQL=2019 \ - +integration-test-mssql - -setup-base: - ARG ELIXIR_BASE=1.15.6-erlang-25.3.2.6-alpine-3.18.4 - FROM hexpm/elixir:$ELIXIR_BASE - RUN apk add --no-progress --update git build-base - ENV ELIXIR_ASSERT_TIMEOUT=10000 - WORKDIR /src/ecto_sql - RUN apk add --no-progress --update docker docker-compose - RUN mix local.rebar --force - RUN mix local.hex --force - -COMMON_SETUP_AND_MIX: - COMMAND - COPY mix.exs mix.lock .formatter.exs . - COPY --dir bench integration_test lib test ./ - RUN mix deps.get - RUN mix deps.compile - RUN mix compile #--warnings-as-errors - -integration-test-postgres: - FROM +setup-base - ARG POSTGRES="11.11" - - IF [ "$POSTGRES" = "16.2-alpine" ] - # for 16 we need an upgraded version of pg_dump; - # alpine 3.16 does not come with the postgres 16 client by default; - # we must first update the public keys for the packages because they - # might have been rotated since our image was built - RUN apk add -X https://dl-cdn.alpinelinux.org/alpine/v3.19/main -u alpine-keys - RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.19/main' >> /etc/apk/repositories - RUN apk add postgresql16-client - ELSE - RUN apk add postgresql-client - END - - DO +COMMON_SETUP_AND_MIX - - # then run the tests - WITH DOCKER \ - --pull "postgres:$POSTGRES" --platform linux/amd64 - RUN set -e; \ - timeout=$(expr $(date +%s) + 30); \ - docker run --name pg --network=host -d -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres "postgres:$POSTGRES"; \ - # wait for postgres to start - while ! pg_isready --host=127.0.0.1 --port=5432 --quiet; do \ - test "$(date +%s)" -le "$timeout" || (echo "timed out waiting for postgres"; exit 1); \ - echo "waiting for postgres"; \ - sleep 1; \ - done; \ - # run tests - PG_URL=postgres:postgres@127.0.0.1 ECTO_ADAPTER=pg mix test; - END - -integration-test-mysql: - FROM +setup-base - RUN apk add mysql-client - - DO +COMMON_SETUP_AND_MIX - - ARG MYSQL="5.7" - WITH DOCKER \ - --pull "mysql:$MYSQL" --platform linux/amd64 - RUN set -e; \ - timeout=$(expr $(date +%s) + 30); \ - docker run --name mysql --network=host -d -e MYSQL_ROOT_PASSWORD=root "mysql:$MYSQL" \ - --sql_mode="ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION,ANSI_QUOTES" \ - # the default authentication plugin for MySQL 8 is sha 256 but it doesn't come with the docker image. falling back to the 5.7 way - --default-authentication-plugin=mysql_native_password; \ - # wait for mysql to start - while ! mysqladmin ping --host=127.0.0.1 --port=3306 --protocol=TCP --silent; do \ - test "$(date +%s)" -le "$timeout" || (echo "timed out waiting for mysql"; exit 1); \ - echo "waiting for mysql"; \ - sleep 1; \ - done; \ - # run tests - MYSQL_URL=root:root@127.0.0.1 ECTO_ADAPTER=myxql mix test; - END - - -integration-test-mssql: - ARG TARGETARCH - FROM +setup-base - - RUN apk add --no-cache curl gnupg --virtual .build-dependencies -- && \ - curl -O https://download.microsoft.com/download/3/5/5/355d7943-a338-41a7-858d-53b259ea33f5/msodbcsql18_18.3.2.1-1_${TARGETARCH}.apk && \ - curl -O https://download.microsoft.com/download/3/5/5/355d7943-a338-41a7-858d-53b259ea33f5/mssql-tools18_18.3.1.1-1_${TARGETARCH}.apk && \ - echo y | apk add --allow-untrusted msodbcsql18_18.3.2.1-1_${TARGETARCH}.apk mssql-tools18_18.3.1.1-1_${TARGETARCH}.apk && \ - apk del .build-dependencies && rm -f msodbcsql*.sig mssql-tools*.apk - ENV PATH="/opt/mssql-tools18/bin:${PATH}" - - DO +COMMON_SETUP_AND_MIX - - ARG MSSQL="2017" - WITH DOCKER \ - --pull "mcr.microsoft.com/mssql/server:$MSSQL-latest" --platform linux/amd64 - RUN set -e; \ - timeout=$(expr $(date +%s) + 30); \ - docker run -d -p 1433:1433 --name mssql -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=some!Password' "mcr.microsoft.com/mssql/server:$MSSQL-latest"; \ - # wait for mssql to start - while ! sqlcmd -C -S tcp:127.0.0.1,1433 -U sa -P 'some!Password' -Q "SELECT 1" >/dev/null 2>&1; do \ - test "$(date +%s)" -le "$timeout" || (echo "timed out waiting for mssql"; exit 1); \ - echo "waiting for mssql"; \ - sleep 1; \ - done; \ - # run tests - ECTO_ADAPTER=tds mix test; - END diff --git a/README.md b/README.md index 0c2d7552..a23ad375 100644 --- a/README.md +++ b/README.md @@ -39,23 +39,19 @@ You may also run `mix test.all` to run the unit tests and all integration tests. $ ECTO_PATH=../ecto mix test.all -### Running containerized tests +### Running databases with Docker -It is also possible to run the integration tests under a containerized environment using [earthly](https://earthly.dev/get-earthly). You will also need Docker installed on your system. Then you can run: +You can also run all databases with Docker: - $ earthly -P +all + $ docker run --name pg -d -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres postgres:16.2-alpine + $ docker run --name mysql -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root mysql:8.0 --sql_mode="ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION,ANSI_QUOTES" --default-authentication-plugin=mysql_native_password + $ docker run --name mssql -d -p 1433:1433 -e ACCEPT_EULA=Y -e 'MSSQL_SA_PASSWORD=some!Password' mcr.microsoft.com/mssql/server:2022-latest -You can also use this to interactively debug any failing integration tests using the corresponding commands: +And then point the tests to them: - $ earthly -P -i --build-arg ELIXIR_BASE=1.8.2-erlang-20.3.8.26-alpine-3.11.6 --build-arg MYSQL=5.7 +integration-test-mysql - $ earthly -P -i --build-arg ELIXIR_BASE=1.8.2-erlang-20.3.8.26-alpine-3.11.6 --build-arg MSSQL=2019 +integration-test-mssql - $ earthly -P -i --build-arg ELIXIR_BASE=1.8.2-erlang-20.3.8.26-alpine-3.11.6 --build-arg POSTGRES=11.11 +integration-test-postgres + $ PG_URL=postgres:postgres@127.0.0.1 MYSQL_URL=root:root@127.0.0.1 MSSQL_URL='sa:some!Password@127.0.0.1' mix test.all -Then once you enter the containerized shell, you can inspect the underlying databases with the respective commands: - - PGPASSWORD=postgres psql -h 127.0.0.1 -U postgres -d postgres ecto_test - MYSQL_PASSWORD=root mysql -h 127.0.0.1 -uroot -proot ecto_test - sqlcmd -U sa -P 'some!Password' +The `pg` and `myxql` integration tests also require the `pg_dump`/`psql` and `mysqldump`/`mysql` clients, respectively. ## License