Comments (3)
Definitely worth exploring :)
from ash_postgres.
@zachdaniel I can work on a PR but need some more advice from you
from ash_postgres.
So the guide on setting up a dynamic repository is here: https://hexdocs.pm/ecto/replicas-and-dynamic-repositories.html
The main issue with us doing it this way in ash_postgres
(i.e every query maybe getting a dynamic repo) in place of changing the schema is that we'd be constantly establishing new connections for each query for actions that may call many queries.
What we would need to do is something like this:
Add a hook for data layers to wrap the entire execution of an action for a given tenant, returning context to be set on all queries/changesets for that tenant, storing that context (keyed by the tenant) in the process dictionary context(Ash.set_context()
which deep-merges). Whenever we execute a query where the tenant is different than the one before, we run this hook and carry its contents down to all other queries. This has to be implemented at the action invocation level, not using something like Ash.Query.around_action
which just wraps the individual query call.
Whenever we run an action, we check for context for the tenant of that action, and set it on to the current query. We already have the ability to configure the repo dynamically like so:
`Ash.Query.set_context(query, %{data_layer: %{:repo, RepoName}})`.
So this new hook for ash_postgres
would just be setting a new repo. And then we'd configure it like this:
postgres do
multitenant_context fn config, tenant ->
Keyword.put(config, :username, "#{tenant}.#{config[:username]}")
end
end
The default implementation of that function would be Keyword.put(config, :prefix, tenant)
.
And if all that is changed in that keyword is the prefix
option then we won't set a dynamic repo. If anything else changes, then we will.
There are a fair amount of moving parts here, but it can be done. We will also likely find a few places where we aren't carrying down parent context from one query to the next. We'll just have to iterate on it.
from ash_postgres.
Related Issues (20)
- Regression installing ash_uuid extension HOT 1
- Polymorphic resource belongs_to relationship error HOT 1
- Using ref in a filter gives an error in ash 3 HOT 3
- Custom index unexpectedly dropped when upgrading to 2.0 HOT 8
- parent does not work from within an aggregate filter HOT 1
- protocol Jason.Encoder not implemented for dynamic ([], []) of type Ecto.Query.DynamicExpr HOT 1
- After upgrading to Ash 3 and latest postgres, complex nested aggregate fails HOT 1
- PSQL QUERY ERROR on updating json fields with nested objects containing arrays HOT 1
- Canβt rollback only 1 migration ago with `mix ash.rollback` HOT 2
- Proposal for `ash_postgres.squash_snapshots` mix task HOT 1
- Changing identities leads to generation of erroneous migrations HOT 1
- The maximum length of the index name is not considered HOT 2
- `Identity.where` is not working HOT 3
- Removing an attribute generates a migration with commented out changes, but snapshot with the changes HOT 5
- Handle the new Ecto parameterized type format
- Error thrown if relationship aggregate is used in update with atomic_update HOT 3
- Don't sort identity keys when creating unique indexes
- Updates from GraphQL broken
- Exceeding index limit gives confusing error message
- NaiveDateTime value breaks queries with Map type HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ash_postgres.