Skip to content

Commit 46fdd2a

Browse files
committed
Extract multitenancy
1 parent b994d08 commit 46fdd2a

File tree

2 files changed

+72
-67
lines changed

2 files changed

+72
-67
lines changed

usage-rules.md

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -10,73 +10,6 @@ SPDX-License-Identifier: MIT
1010

1111
AshPostgres is the PostgreSQL data layer for Ash Framework. It's the most fully-featured Ash data layer and should be your default choice unless you have specific requirements for another data layer. Any PostgreSQL version higher than 13 is fully supported.
1212

13-
## Multitenancy
14-
15-
AshPostgres supports schema-based multitenancy:
16-
17-
```elixir
18-
defmodule MyApp.Tenant do
19-
use Ash.Resource,
20-
data_layer: AshPostgres.DataLayer
21-
22-
# Resource definition...
23-
24-
postgres do
25-
table "tenants"
26-
repo MyApp.Repo
27-
28-
# Automatically create/manage tenant schemas
29-
manage_tenant do
30-
template ["tenant_", :id]
31-
end
32-
end
33-
end
34-
```
35-
36-
### Setting Up Multitenancy
37-
38-
1. Configure your repo to support multitenancy:
39-
40-
```elixir
41-
defmodule MyApp.Repo do
42-
use AshPostgres.Repo, otp_app: :my_app
43-
44-
# Return all tenant schemas for migrations
45-
def all_tenants do
46-
import Ecto.Query, only: [from: 2]
47-
all(from(t in "tenants", select: fragment("? || ?", "tenant_", t.id)))
48-
end
49-
end
50-
```
51-
52-
2. Mark resources that should be multi-tenant:
53-
54-
```elixir
55-
defmodule MyApp.Post do
56-
use Ash.Resource,
57-
data_layer: AshPostgres.DataLayer
58-
59-
multitenancy do
60-
strategy :context
61-
attribute :tenant
62-
end
63-
64-
# Resource definition...
65-
end
66-
```
67-
68-
3. When tenant migrations are generated, they'll be in `priv/repo/tenant_migrations`
69-
70-
4. Run tenant migrations in addition to regular migrations:
71-
72-
```bash
73-
# Run regular migrations
74-
mix ash.migrate
75-
76-
# Run tenant migrations
77-
mix ash_postgres.migrate --tenants
78-
```
79-
8013
## Advanced Features
8114

8215
### Manual Relationships

usage-rules/multitenancy.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<!--
2+
SPDX-FileCopyrightText: 2020 Zach Daniel
3+
4+
SPDX-License-Identifier: MIT
5+
-->
6+
7+
# Multitenancy
8+
9+
AshPostgres supports schema-based multitenancy:
10+
11+
```elixir
12+
defmodule MyApp.Tenant do
13+
use Ash.Resource,
14+
data_layer: AshPostgres.DataLayer
15+
16+
# Resource definition...
17+
18+
postgres do
19+
table "tenants"
20+
repo MyApp.Repo
21+
22+
# Automatically create/manage tenant schemas
23+
manage_tenant do
24+
template ["tenant_", :id]
25+
end
26+
end
27+
end
28+
```
29+
30+
## Setting Up Multitenancy
31+
32+
1. Configure your repo to support multitenancy:
33+
34+
```elixir
35+
defmodule MyApp.Repo do
36+
use AshPostgres.Repo, otp_app: :my_app
37+
38+
# Return all tenant schemas for migrations
39+
def all_tenants do
40+
import Ecto.Query, only: [from: 2]
41+
all(from(t in "tenants", select: fragment("? || ?", "tenant_", t.id)))
42+
end
43+
end
44+
```
45+
46+
2. Mark resources that should be multi-tenant:
47+
48+
```elixir
49+
defmodule MyApp.Post do
50+
use Ash.Resource,
51+
data_layer: AshPostgres.DataLayer
52+
53+
multitenancy do
54+
strategy :context
55+
attribute :tenant
56+
end
57+
58+
# Resource definition...
59+
end
60+
```
61+
62+
3. When tenant migrations are generated, they'll be in `priv/repo/tenant_migrations`
63+
64+
4. Run tenant migrations in addition to regular migrations:
65+
66+
```bash
67+
# Run regular migrations
68+
mix ash.migrate
69+
70+
# Run tenant migrations
71+
mix ash_postgres.migrate --tenants
72+
```

0 commit comments

Comments
 (0)