|
| 1 | +# Axum App with multi-database Database |
| 2 | + |
| 3 | +This example project involves three crates, each owning a different schema in one database, |
| 4 | +with their own set of migrations. |
| 5 | + |
| 6 | +* The main crate, a simple binary simulating the action of a REST API. |
| 7 | + * 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. |
| 10 | +* `accounts`: a subcrate simulating a reusable account-management crate. |
| 11 | + * Owns schema `accounts`. |
| 12 | +* `payments`: a subcrate simulating a wrapper for a payments API. |
| 13 | + * Owns schema `payments`. |
| 14 | + |
| 15 | +## Note: Schema-Qualified Names |
| 16 | + |
| 17 | +This example uses schema-qualified names everywhere for clarity. |
| 18 | + |
| 19 | +It can be tempting to change the `search_path` of the connection (MySQL, Postgres) to eliminate the need for schema |
| 20 | +prefixes, but this can cause some really confusing issues when names conflict. |
| 21 | + |
| 22 | +This example will generate a `_sqlx_migrations` table in three different schemas; if `search_path` is set |
| 23 | +to `public,accounts,payments` and the migrator for the main application attempts to reference the table unqualified, |
| 24 | +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 and `sqlx.toml` support: |
| 31 | + |
| 32 | +``` |
| 33 | +cargo install sqlx-cli --features postgres,sqlx-toml |
| 34 | +``` |
| 35 | + |
| 36 | +Start a Postgres server (shown here using Docker, `run` command also works with `podman`): |
| 37 | + |
| 38 | +``` |
| 39 | +docker run -d -e POSTGRES_PASSWORD=password -p 5432:5432 --name postgres postgres:latest |
| 40 | +``` |
| 41 | + |
| 42 | +Create `.env` with the various database URLs or set them in your shell environment; |
| 43 | + |
| 44 | +``` |
| 45 | +DATABASE_URL=postgres://postgres:password@localhost/example-multi-database |
| 46 | +ACCOUNTS_DATABASE_URL=postgres://postgres:password@localhost/example-multi-database-accounts |
| 47 | +PAYMENTS_DATABASE_URL=postgres://postgres:password@localhost/example-multi-database-payments |
| 48 | +``` |
| 49 | + |
| 50 | +Run the following commands: |
| 51 | + |
| 52 | +``` |
| 53 | +(cd accounts && sqlx db setup) |
| 54 | +(cd payments && sqlx db setup) |
| 55 | +sqlx db setup |
| 56 | +``` |
| 57 | + |
| 58 | +It is an open question how to make this more convenient; `sqlx-cli` could gain a `--recursive` flag that checks |
| 59 | +subdirectories for `sqlx.toml` files, but that would only work for crates within the same workspace. If the `accounts` |
| 60 | +and `payments` crates were instead crates.io dependencies, we would need Cargo's help to resolve that information. |
| 61 | + |
| 62 | +An issue has been opened for discussion: <https://github.com/launchbadge/sqlx/issues/3761> |
0 commit comments