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

Allow to specify custom schema name #15

Closed
mgibowski opened this issue Nov 2, 2023 · 4 comments · Fixed by #16
Closed

Allow to specify custom schema name #15

mgibowski opened this issue Nov 2, 2023 · 4 comments · Fixed by #16

Comments

@mgibowski
Copy link

mgibowski commented Nov 2, 2023

Current behaviour

Currently Endo hard-codes the schema name to "public":

from(x in __MODULE__, as: :self, where: x.table_schema == "public")

However, sometimes the application may use a schema with a different name.

Connection prefixes in Ecto

Ecto calls those are called "connection prefixes" and there are two ways to set them.

  1. By setting a global connection_prefix

With the help of the after_connect callback as documented in Ecto:

https://hexdocs.pm/ecto_sql/Ecto.Adapters.Postgres.html#module-after-connect-callback

  1. By setting a per-query and per-struct prefix

https://hexdocs.pm/ecto/multi-tenancy-with-query-prefixes.html#per-query-and-per-struct-prefixes

Example:

MyApp.Repo.all(Sample, prefix: "a_custom_schema_prefix")

Suggestion

It would be great if Endo supported querying schemas other than "public".

One possibility would be allowing to configure a global table schema that would overwrite the default "public" with a config:

config :endo, table_schema: "my_custom_schema_other_than_public"

Or (and) to mimic the Ecto API, for example:

Endo.list_tables(MyApp.Repo, prefix: "my_custom_schema_other_than_public")
@vereis
Copy link
Collaborator

vereis commented Nov 7, 2023

I think both suggestions are reasonable! I'm open to a PR if you want to tackle this, otherwise, I'm sure we can accommodate this relatively soon!

@vereis
Copy link
Collaborator

vereis commented Jan 17, 2024

Hi @mgibowski I've made a PR which implements both suggestions!

I've written up some documentation and will push it out once tested and merged, but in short, you'll be able to do the following in future:

  1. On a query by query basis, mimicking Ecto, you can set prefix: "my_prefix" on any Endo.get_table/3 or Endo.list_tables/2 calls which works as you'd expect
  2. Or, for all queries, we now defer to the config for the default prefix instead of always defaulting to "public". For backwards compatibility we default to public if not specified. This can be done by setting config :endo, table_schema: "my_prefix"

Thanks for raising and sorry for the delays ❤️

@vereis
Copy link
Collaborator

vereis commented Jan 17, 2024

@mgibowski just pushed 0.1.20 which includes the above functionality!

@mgibowski
Copy link
Author

Amazing, thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants