joeljuca / swiss_schema

A Swiss Army knife for your Ecto schemas
https://hex.pm/packages/swiss_schema
Apache License 2.0
26 stars 4 forks source link

Load repo from app default configs #19

Closed DanubioLima closed 5 months ago

DanubioLima commented 6 months ago

The repo option for SwissSchema is now loaded for the default application configs. If not found, it get the option from opts parameter, like before.

See #18

joeljuca commented 5 months ago

Hi @DanubioLima! Good to have you here. :)

It would be wise to avoid reusing Ecto-specific configuration for SwissSchema because it might confuse users about the purpose of :ecto_repos. Also, SwissSchema assumes a default Ecto repo to work with – and even though it's possible to use multiple repos through the :repo opt, it's not something we'll be worried about in the default configuration.

I imagined something simple, like this:

# config/config.exs

config :my_app, SwissSchema, repo: MyApp.Repo

So when I use SwissSchema without inline opts, it would use the default config, loaded from some config file.

defmodule MyApp.Accounts.User do
  use Ecto.Schema

  # :repo opt is already set in config/config.exs
  use SwissSchema
end
DanubioLima commented 5 months ago

Hi @DanubioLima! Good to have you here. :)

It would be wise to avoid reusing Ecto-specific configuration for SwissSchema because it might confuse users about the purpose of :ecto_repos. Also, SwissSchema assumes a default Ecto repo to work with – and even though it's possible to use multiple repos through the :repo opt, it's not something we'll be worried about in the default configuration.

I imagined something simple, like this:

# config/config.exs

config :my_app, SwissSchema, repo: MyApp.Repo

So when I use SwissSchema without inline opts, it would use the default config, loaded from some config file.

defmodule MyApp.Accounts.User do
  use Ecto.Schema

  # :repo opt is already set in config/config.exs
  use SwissSchema
end

@joeljuca You are right. In this moment, the code does not use multiple repos, we can handle it in future. When I was developing in my local machine, this error block me many times:

SwissSchemaTest: failure on setup_all callback, all tests have been invalidated
     ** (MatchError) no match of right hand side value: {:error, %Exqlite.Error{message: "database is locked", statement: nil}}
     stacktrace:
       (ecto_sqlite3 0.10.3) lib/ecto/adapters/sqlite3.ex:230: Ecto.Adapters.SQLite3.storage_up/1
       test/swiss_schema_test.exs:31: anonymous fn/1 in SwissSchemaTest.__ex_unit_setup_all_0/1
       (elixir 1.16.0) lib/enum.ex:987: Enum."-each/2-lists^foreach/1-0-"/2
       test/swiss_schema_test.exs:24: SwissSchemaTest.__ex_unit_setup_all_0/1
       test/swiss_schema_test.exs:1: SwissSchemaTest.__ex_unit__/2

It's a problem with SQLite connection but I can't resolve it. Do you have any idea how to avoid that problem?

joeljuca commented 5 months ago

Danúbio,

This is a known problem, and I solved by rewriting tests to use Postgres instead of SQLite.

There’s a race condition somewhere I couldn’t find, and in the end I just gave up searching.

You can rebase your branch with main to get my latest commits and everything should be fine:

$ git checkout [your branch]
$ git rebase main

Let me know if the problem persists.

DanubioLima commented 5 months ago

Danúbio,

This is a known problem, and I solved by rewriting tests to use Postgres instead of SQLite.

There’s a race condition somewhere I couldn’t find, and in the end I just gave up searching.

You can rebase your branch with main to get my latest commits and everything should be fine:

$ git checkout [your branch]
$ git rebase main

Let me know if the problem persists.

Yes, it's solved my problem. Thanks!

joeljuca commented 5 months ago

PR resubmitted as #22.