Skip to content

Commit 46878e8

Browse files
committed
feat: multi-tenant example
No longer Axum-based because filling out the request routes would have distracted from the purpose of the example.
1 parent 0b79b51 commit 46878e8

File tree

22 files changed

+412
-148
lines changed

22 files changed

+412
-148
lines changed

Cargo.lock

Lines changed: 20 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ members = [
1111
"sqlx-postgres",
1212
"sqlx-sqlite",
1313
"examples/mysql/todos",
14-
"examples/postgres/axum-multi-tenant",
14+
"examples/postgres/multi-tenant",
1515
"examples/postgres/axum-social-with-tests",
1616
"examples/postgres/chat",
1717
"examples/postgres/files",

examples/postgres/axum-multi-tenant/payments/src/lib.rs

Lines changed: 0 additions & 34 deletions
This file was deleted.

examples/postgres/axum-multi-tenant/src/http/mod.rs

Lines changed: 0 additions & 7 deletions
This file was deleted.

examples/postgres/axum-multi-tenant/src/main.rs

Lines changed: 0 additions & 64 deletions
This file was deleted.

examples/postgres/axum-multi-tenant/Cargo.toml renamed to examples/postgres/multi-tenant/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
1616

1717
sqlx = { path = "../../..", version = "0.8.3", features = ["runtime-tokio", "postgres"] }
1818

19-
axum = "0.8.1"
19+
axum = { version = "0.8.1", features = ["macros"] }
2020

21-
clap = { version = "4.5.30", features = ["derive", "env"] }
2221
color-eyre = "0.6.3"
2322
dotenvy = "0.15.7"
2423
tracing-subscriber = "0.3.19"
2524

25+
rust_decimal = "1.36.0"
26+
27+
rand = "0.8.5"
2628

2729
[lints]
2830
workspace = true

examples/postgres/axum-multi-tenant/README.md renamed to examples/postgres/multi-tenant/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ with their own set of migrations.
55

66
* The main crate, an Axum app.
77
* Owns the `public` schema (tables are referenced unqualified).
8+
* Migrations are moved to `src/migrations` using config key `migrate.migrations-dir`
9+
to visually separate them from the subcrate folders.
810
* `accounts`: a subcrate simulating a reusable account-management crate.
911
* Owns schema `accounts`.
1012
* `payments`: a subcrate simulating a wrapper for a payments API.
@@ -20,3 +22,27 @@ prefixes, but this can cause some really confusing issues when names conflict.
2022
This example will generate a `_sqlx_migrations` table in three different schemas, and if `search_path` is set
2123
to `public,accounts,payments` and the migrator for the main application attempts to reference the table unqualified,
2224
it would throw an error.
25+
26+
# Setup
27+
28+
This example requires running three different sets of migrations.
29+
30+
Ensure `sqlx-cli` is installed with Postgres support.
31+
32+
Start a Postgres server.
33+
34+
Create `.env` with `DATABASE_URL` or set it in your shell environment.
35+
36+
Run the following commands:
37+
38+
```
39+
(cd accounts && sqlx db setup)
40+
(cd payments && sqlx migrate run)
41+
sqlx migrate run
42+
```
43+
44+
It is an open question how to make this more convenient; `sqlx-cli` could gain a `--recursive` flag that checks
45+
subdirectories for `sqlx.toml` files, but that would only work for crates within the same workspace. If the `accounts`
46+
and `payments` crates were instead crates.io dependencies, we would need Cargo's help to resolve that information.
47+
48+
An issue has been opened for discussion: <https://github.com/launchbadge/sqlx/issues/3761>

examples/postgres/axum-multi-tenant/accounts/Cargo.toml renamed to examples/postgres/multi-tenant/accounts/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ tokio = { version = "1", features = ["rt", "sync"] }
1010
argon2 = { version = "0.5.3", features = ["password-hash"] }
1111
password-hash = { version = "0.5", features = ["std"] }
1212

13-
uuid = "1"
13+
uuid = { version = "1", features = ["serde"] }
1414
thiserror = "1"
1515
rand = "0.8"
1616

17-
time = "0.3.37"
17+
time = { version = "0.3.37", features = ["serde"] }
18+
19+
serde = { version = "1.0.218", features = ["derive"] }
1820

1921
[dev-dependencies]
2022
sqlx = { workspace = true, features = ["runtime-tokio"] }

examples/postgres/axum-multi-tenant/accounts/migrations/01_setup.sql renamed to examples/postgres/multi-tenant/accounts/migrations/01_setup.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ create or replace function accounts.set_updated_at()
1212
$$
1313
begin
1414
NEW.updated_at = now();
15-
return NEW;
15+
return NEW;
1616
end;
1717
$$ language plpgsql;
1818

1919
create or replace function accounts.trigger_updated_at(tablename regclass)
2020
returns void as
2121
$$
2222
begin
23-
execute format('CREATE TRIGGER set_updated_at
23+
execute format('CREATE TRIGGER set_updated_at
2424
BEFORE UPDATE
2525
ON %s
2626
FOR EACH ROW

0 commit comments

Comments
 (0)