Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Test MySQL and MS SQL databases in CI #447

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 134 additions & 2 deletions .github/workflows/elixir.yml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can create another workflow file to run only on main instead of changing this one. So on every PR it would run tests for Postgres (fast and the most popular vendor) and on main it would run for others so we make sure Beacon runs on all official adapters but doesn't take too long to run tests on PRs. Wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good to me!

Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ permissions:
contents: read

jobs:
test:
name: "test: OTP ${{matrix.otp}} | Elixir ${{matrix.elixir}} | Phoenix ${{matrix.phoenix-version}} | LiveView ${{matrix.phoenix-live-view-version}}"
test-postgres:
name: "test: postgres 13.1 | OTP ${{matrix.otp}} | Elixir ${{matrix.elixir}} | Phoenix ${{matrix.phoenix-version}} | LiveView ${{matrix.phoenix-live-view-version}}"

strategy:
matrix:
Expand All @@ -33,6 +33,7 @@ jobs:
MDEX_BUILD: 1
PHOENIX_VERSION: ${{matrix.phoenix-version}}
PHOENIX_LIVE_VIEW_VERSION: ${{matrix.phoenix-live-view-version}}
DB_ADAPTER: postgres

runs-on: ubuntu-20.04

Expand Down Expand Up @@ -77,6 +78,137 @@ jobs:

- run: mix test

test-mysql:
name: "test: MySQL 5.7 | OTP ${{matrix.otp}} | Elixir ${{matrix.elixir}} | Phoenix ${{matrix.phoenix-version}} | LiveView ${{matrix.phoenix-live-view-version}}"

strategy:
matrix:
include:
# minimum required versions
- otp: "23"
elixir: "1.13.0"
phoenix-version: "1.7.0"
phoenix-live-view-version: "0.20.2"

# latest
- otp: "26"
elixir: "1.16"
phoenix-version: "~> 1.7"
phoenix-live-view-version: "~> 0.20"

env:
MIX_ENV: test
MDEX_BUILD: 1
PHOENIX_VERSION: ${{matrix.phoenix-version}}
PHOENIX_LIVE_VIEW_VERSION: ${{matrix.phoenix-live-view-version}}
DB_ADAPTER: mysql
MYSQL_UNIX_PORT: 3306

runs-on: ubuntu-20.04

services:
mysql:
image: mysql:5.7
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: beacon_test
ports:
- 3306
options: >-
--health-cmd="mysqladmin ping"
--health-interval=10s
--health-timeout=5s
--health-retries=5

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Elixir
uses: erlef/setup-beam@v1
with:
otp-version: ${{ matrix.otp }}
elixir-version: ${{ matrix.elixir }}

- name: Cache mix deps
uses: actions/cache@v3
id: cache-deps
with:
path: |
deps
_build
key: mix-${{ env.MIX_ENV }}-${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-${{ matrix.phoenix-version }}-${{ matrix.phoenix-live-view-version }}-${{ hashFiles('**/mix.lock') }}
restore-keys: |
mix-${{ env.MIX_ENV }}-${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-${{ matrix.phoenix-version }}-${{ matrix.phoenix-live-view-version }}-${{ hashFiles('**/mix.lock') }}

- run: mix deps.get --only-test

- run: mix tailwind.install

- run: mix test

test-mssql:
name: "test: MS SQL 2019 | OTP ${{matrix.otp}} | Elixir ${{matrix.elixir}} | Phoenix ${{matrix.phoenix-version}} | LiveView ${{matrix.phoenix-live-view-version}}"

env:
MIX_ENV: test
MDEX_BUILD: 1
PHOENIX_VERSION: ${{matrix.phoenix-version}}
PHOENIX_LIVE_VIEW_VERSION: ${{matrix.phoenix-live-view-version}}
DB_ADAPTER: mssql

runs-on: ubuntu-20.04

strategy:
matrix:
include:
# minimum required versions
- otp: "23"
elixir: "1.13.0"
phoenix-version: "1.7.0"
phoenix-live-view-version: "0.20.2"
# latest
- otp: "26"
elixir: "1.16"
phoenix-version: "~> 1.7"
phoenix-live-view-version: "~> 0.20"

services:
sqlserver:
image: mcr.microsoft.com/mssql/server:2019-latest
ports:
- 1433:1433
env:
ACCEPT_EULA: Y
SA_PASSWORD: Beacon!CMS!!

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Elixir
uses: erlef/setup-beam@v1
with:
otp-version: ${{ matrix.otp }}
elixir-version: ${{ matrix.elixir }}

- name: Cache mix deps
uses: actions/cache@v3
id: cache-deps
with:
path: |
deps
_build
key: mix-${{ env.MIX_ENV }}-${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-${{ matrix.phoenix-version }}-${{ matrix.phoenix-live-view-version }}-${{ hashFiles('**/mix.lock') }}
restore-keys: |
mix-${{ env.MIX_ENV }}-${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-${{ matrix.phoenix-version }}-${{ matrix.phoenix-live-view-version }}-${{ hashFiles('**/mix.lock') }}

- run: mix deps.get --only-test

- run: mix tailwind.install

- run: mix test

quality:
name: "quality: OTP ${{matrix.otp}} | Elixir ${{matrix.elixir}} | Phoenix ${{matrix.phoenix-version}} | LiveView ${{matrix.phoenix-live-view-version}}"

Expand Down
10 changes: 9 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ config :beacon, :generators, binary_id: true

config :phoenix, :json_library, Jason

config :beacon, Beacon.Repo, migration_timestamps: [type: :utc_datetime_usec]
db_adapter = fn
"mysql" -> Ecto.Adapters.MyXQL
"mssql" -> Ecto.Adapters.Tds
_ -> Ecto.Adapters.Postgres
end

config :beacon, Beacon.Repo,
migration_timestamps: [type: :utc_datetime_usec],
adapter: db_adapter.(System.get_env("DB_ADAPTER"))

if Mix.env() == :dev do
esbuild = fn args ->
Expand Down
34 changes: 28 additions & 6 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,34 @@ config :phoenix, :json_library, Jason

config :logger, level: :error

config :beacon, Beacon.Repo,
database: "beacon_test",
password: "postgres",
pool: Ecto.Adapters.SQL.Sandbox,
username: "postgres",
ownership_timeout: 1_000_000_000
case System.get_env("DB_ADAPTER") do
"mysql" ->
config :beacon, Beacon.Repo,
database: "beacon_test",
pool: Ecto.Adapters.SQL.Sandbox,
username: "root",
ownership_timeout: 1_000_000_000,
port: 3306,
protocol: :tcp,
hostname: "localhost"

"mssql" ->
config :beacon, Beacon.Repo,
database: "beacon_test",
password: "Beacon!CMS!!",
pool: Ecto.Adapters.SQL.Sandbox,
username: "mssql",
ownership_timeout: 1_000_000_000,
port: 1433

_ ->
config :beacon, Beacon.Repo,
database: "beacon_test",
password: "postgres",
pool: Ecto.Adapters.SQL.Sandbox,
username: "postgres",
ownership_timeout: 1_000_000_000
end

config :beacon, ecto_repos: [Beacon.Repo]

Expand Down
8 changes: 8 additions & 0 deletions guides/introduction/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ For more details please check out the docs: `mix help beacon.install`
show_sensitive_data_on_connection_error: true
```

If you are using MySQL or MS SQL Server, add either:
```elixir
# for MySQL
adapter: Ecto.Adapters.MyXQL
# for MS SQL Server
adapter: Ecto.Adapters.Tds
```

3. Edit your endpoint configuration `:render_errors` key, like so:

Replace the `[formats: [html: _]]` option with `BeaconWeb.ErrorHTML`.
Expand Down
2 changes: 1 addition & 1 deletion lib/beacon/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Beacon.Repo do

use Ecto.Repo,
otp_app: :beacon,
adapter: Ecto.Adapters.Postgres
adapter: Application.compile_env(:beacon, [Beacon.Repo, :adapter], Ecto.Adapters.Postgres)

# https://medium.com/very-big-things/towards-maintainable-elixir-the-core-and-the-interface-c267f0da43
def transact(fun) when is_function(fun) do
Expand Down
2 changes: 2 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ defmodule Beacon.MixProject do
{:image, "~> 0.40"},
{:jason, "~> 1.0"},
{:solid, "~> 0.14"},
{:myxql, ">= 0.0.0"},
phoenix_dep(),
{:phoenix_ecto, "~> 4.4"},
{:phoenix_html, "~> 4.0"},
Expand All @@ -56,6 +57,7 @@ defmodule Beacon.MixProject do
{:postgrex, "~> 0.16"},
{:safe_code, github: "TheFirstAvenger/safe_code"},
{:tailwind, "~> 0.2"},
{:tds, ">= 0.0.0"},
{:rustler, ">= 0.0.0", optional: true},
{:faker, "~> 0.17", only: :test},
live_monaco_editor_dep(),
Expand Down
Loading