From 35f8ee4b280f9b677530fd975d465d641f449e77 Mon Sep 17 00:00:00 2001 From: APB9785 <74077809+APB9785@users.noreply.github.com> Date: Fri, 12 Apr 2024 15:08:56 -0500 Subject: [PATCH 1/8] Add MySQL and MS SQL services to Github Actions config --- .github/workflows/elixir.yml | 132 ++++++++++++++++++++++++++++++++++- 1 file changed, 130 insertions(+), 2 deletions(-) diff --git a/.github/workflows/elixir.yml b/.github/workflows/elixir.yml index e5024f1bb..d351c85bc 100644 --- a/.github/workflows/elixir.yml +++ b/.github/workflows/elixir.yml @@ -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: @@ -77,6 +77,134 @@ 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}} + + runs-on: ubuntu-20.04 + + services: + mysql: + image: mysql:5.7 + env: + MYSQL_ALLOW_EMPTY_PASSWORD: yes + MYSQL_DATABASE: your-db_dev + ports: + - 3306 + options: >- + --health-cmd="mysqladmin ping" + --health-interval=10s + --health-timeout=5s + --health-retries=3 + + 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}} + + 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}}" From 92936b187729ea1eadd7518d22bf16e76d997b95 Mon Sep 17 00:00:00 2001 From: APB9785 <74077809+APB9785@users.noreply.github.com> Date: Fri, 12 Apr 2024 16:25:27 -0500 Subject: [PATCH 2/8] Set adapter via config/env var --- .github/workflows/elixir.yml | 4 ++-- config/config.exs | 10 +++++++++- guides/introduction/installation.md | 8 ++++++++ lib/beacon/repo.ex | 2 +- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/.github/workflows/elixir.yml b/.github/workflows/elixir.yml index d351c85bc..709144f60 100644 --- a/.github/workflows/elixir.yml +++ b/.github/workflows/elixir.yml @@ -142,7 +142,7 @@ jobs: - run: mix tailwind.install - - run: mix test + - run: DB_ADAPTER=mysql 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}}" @@ -203,7 +203,7 @@ jobs: - run: mix tailwind.install - - run: mix test + - run: DB_ADAPTER=mssql mix test quality: name: "quality: OTP ${{matrix.otp}} | Elixir ${{matrix.elixir}} | Phoenix ${{matrix.phoenix-version}} | LiveView ${{matrix.phoenix-live-view-version}}" diff --git a/config/config.exs b/config/config.exs index b3d0473ff..c753bff8c 100644 --- a/config/config.exs +++ b/config/config.exs @@ -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 -> diff --git a/guides/introduction/installation.md b/guides/introduction/installation.md index 07182407d..e1c503a91 100644 --- a/guides/introduction/installation.md +++ b/guides/introduction/installation.md @@ -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`. diff --git a/lib/beacon/repo.ex b/lib/beacon/repo.ex index 7cd3780a1..4f88198c9 100644 --- a/lib/beacon/repo.ex +++ b/lib/beacon/repo.ex @@ -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 From 014efbc958e736f92006941528401df98b67cf11 Mon Sep 17 00:00:00 2001 From: APB9785 <74077809+APB9785@users.noreply.github.com> Date: Fri, 12 Apr 2024 16:43:52 -0500 Subject: [PATCH 3/8] Add myxql and tds deps --- mix.exs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mix.exs b/mix.exs index 6ee9562f6..1bbcf7d4e 100644 --- a/mix.exs +++ b/mix.exs @@ -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"}, @@ -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(), From 8257585298ff7f8641842d3d839ec7a136418bc6 Mon Sep 17 00:00:00 2001 From: APB9785 <74077809+APB9785@users.noreply.github.com> Date: Fri, 12 Apr 2024 17:02:58 -0500 Subject: [PATCH 4/8] Add port, user, pass to test repo config --- .github/workflows/elixir.yml | 2 +- config/test.exs | 25 ++++++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/.github/workflows/elixir.yml b/.github/workflows/elixir.yml index 709144f60..161922b74 100644 --- a/.github/workflows/elixir.yml +++ b/.github/workflows/elixir.yml @@ -108,7 +108,7 @@ jobs: image: mysql:5.7 env: MYSQL_ALLOW_EMPTY_PASSWORD: yes - MYSQL_DATABASE: your-db_dev + MYSQL_DATABASE: beacon_test ports: - 3306 options: >- diff --git a/config/test.exs b/config/test.exs index 5bd4a4995..3c0bd1474 100644 --- a/config/test.exs +++ b/config/test.exs @@ -4,12 +4,31 @@ config :phoenix, :json_library, Jason config :logger, level: :error +db_port = fn + "mysql" -> 3306 + "mssql" -> 1433 + _ -> 5432 +end + +username = fn + "mysql" -> nil + "mssql" -> nil + _ -> "postgres" +end + +password = fn + "mysql" -> nil + "mssql" -> "Beacon!CMS!!" + _ -> "postgres" +end + config :beacon, Beacon.Repo, database: "beacon_test", - password: "postgres", + password: password.(System.get_env("DB_ADAPTER")), pool: Ecto.Adapters.SQL.Sandbox, - username: "postgres", - ownership_timeout: 1_000_000_000 + username: username.(System.get_env("DB_ADAPTER")), + ownership_timeout: 1_000_000_000, + port: db_port.(System.get_env("DB_ADAPTER")) config :beacon, ecto_repos: [Beacon.Repo] From 02596a0be02de7c3520d604f5b25661337fcc47d Mon Sep 17 00:00:00 2001 From: APB9785 <74077809+APB9785@users.noreply.github.com> Date: Fri, 12 Apr 2024 17:17:22 -0500 Subject: [PATCH 5/8] setup DB_ADAPTER env variable at a higher level --- .github/workflows/elixir.yml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/elixir.yml b/.github/workflows/elixir.yml index 161922b74..697801091 100644 --- a/.github/workflows/elixir.yml +++ b/.github/workflows/elixir.yml @@ -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 @@ -100,6 +101,7 @@ jobs: MDEX_BUILD: 1 PHOENIX_VERSION: ${{matrix.phoenix-version}} PHOENIX_LIVE_VIEW_VERSION: ${{matrix.phoenix-live-view-version}} + DB_ADAPTER: mysql runs-on: ubuntu-20.04 @@ -115,7 +117,7 @@ jobs: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s - --health-retries=3 + --health-retries=5 steps: - name: Checkout code @@ -142,7 +144,7 @@ jobs: - run: mix tailwind.install - - run: DB_ADAPTER=mysql mix test + - 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}}" @@ -152,6 +154,7 @@ jobs: 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 @@ -203,7 +206,7 @@ jobs: - run: mix tailwind.install - - run: DB_ADAPTER=mssql mix test + - run: mix test quality: name: "quality: OTP ${{matrix.otp}} | Elixir ${{matrix.elixir}} | Phoenix ${{matrix.phoenix-version}} | LiveView ${{matrix.phoenix-live-view-version}}" From beb847ace5ed4c718bf6304a5ca596b28ce75b18 Mon Sep 17 00:00:00 2001 From: APB9785 <74077809+APB9785@users.noreply.github.com> Date: Fri, 12 Apr 2024 17:30:28 -0500 Subject: [PATCH 6/8] set MYSQL_UNIX_PORT --- .github/workflows/elixir.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/elixir.yml b/.github/workflows/elixir.yml index 697801091..bdecb4205 100644 --- a/.github/workflows/elixir.yml +++ b/.github/workflows/elixir.yml @@ -102,6 +102,7 @@ jobs: 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 From d496607b1920a5789afbf2feed8b1dca0adf6e08 Mon Sep 17 00:00:00 2001 From: APB9785 <74077809+APB9785@users.noreply.github.com> Date: Fri, 12 Apr 2024 18:06:37 -0500 Subject: [PATCH 7/8] Add protocol and hostname options for MySQL --- config/test.exs | 52 ++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/config/test.exs b/config/test.exs index 3c0bd1474..59d9564fe 100644 --- a/config/test.exs +++ b/config/test.exs @@ -4,32 +4,36 @@ config :phoenix, :json_library, Jason config :logger, level: :error -db_port = fn - "mysql" -> 3306 - "mssql" -> 1433 - _ -> 5432 +case System.get_env("DB_ADAPTER") do + "mysql" -> + config :beacon, Beacon.Repo, + database: "beacon_test", + password: "mysql", + pool: Ecto.Adapters.SQL.Sandbox, + username: "mysql", + 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 -username = fn - "mysql" -> nil - "mssql" -> nil - _ -> "postgres" -end - -password = fn - "mysql" -> nil - "mssql" -> "Beacon!CMS!!" - _ -> "postgres" -end - -config :beacon, Beacon.Repo, - database: "beacon_test", - password: password.(System.get_env("DB_ADAPTER")), - pool: Ecto.Adapters.SQL.Sandbox, - username: username.(System.get_env("DB_ADAPTER")), - ownership_timeout: 1_000_000_000, - port: db_port.(System.get_env("DB_ADAPTER")) - config :beacon, ecto_repos: [Beacon.Repo] # Fake Key Values. We're not putting real creds here. From f61bc72a0db8a550c3773d9ca5c4c411bf69aa99 Mon Sep 17 00:00:00 2001 From: APB9785 <74077809+APB9785@users.noreply.github.com> Date: Fri, 12 Apr 2024 18:31:45 -0500 Subject: [PATCH 8/8] switch MySQL user/pass --- config/test.exs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/config/test.exs b/config/test.exs index 59d9564fe..7fb9b12c1 100644 --- a/config/test.exs +++ b/config/test.exs @@ -8,9 +8,8 @@ case System.get_env("DB_ADAPTER") do "mysql" -> config :beacon, Beacon.Repo, database: "beacon_test", - password: "mysql", pool: Ecto.Adapters.SQL.Sandbox, - username: "mysql", + username: "root", ownership_timeout: 1_000_000_000, port: 3306, protocol: :tcp,