diff --git a/.github/workflows/elixir.yml b/.github/workflows/elixir.yml index e5024f1b..bdecb420 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: @@ -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 @@ -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}}" diff --git a/config/config.exs b/config/config.exs index b3d0473f..c753bff8 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/config/test.exs b/config/test.exs index 5bd4a499..7fb9b12c 100644 --- a/config/test.exs +++ b/config/test.exs @@ -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] diff --git a/guides/introduction/installation.md b/guides/introduction/installation.md index 07182407..e1c503a9 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 7cd3780a..4f88198c 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 diff --git a/mix.exs b/mix.exs index 6ee9562f..1bbcf7d4 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(),