Shuttle Shared Databases
This plugin manages databases on Shuttle and connects them to your app. A shared database is in the same cluster as other userβs databases, but it is not accessible by other users.
Usage
Start by adding shuttle-shared-db
to the dependencies for your service.
Each type of shareable database is behind its own feature flag and macro attribute path.
Engine | Feature flag | Attribute path |
---|---|---|
Postgres | postgres | shuttle_shared_db::Postgres |
Output type
By default, you can get the connection string to the database and connect to it with your preferred library. You can also specify other return types to get rid of common boilerplate.
Depending on which type declaration is used as the output type in the macro, you might need to activate additional feature flags:
Engine | Feature flag | Type declaration | Description |
---|---|---|---|
Postgres | String | The connection string including username and password | |
Postgres | sqlx (with rustls) or sqlx-native-tls | sqlx::PgPool | An sqlx connection pool with reasonable defaults |
Postgres | diesel-async | diesel_async::AsyncPgConnection | An async diesel connection with reasonable defaults |
Postgres | diesel-async-bb8 | diesel_bb8::Pool<diesel_async::AsyncPgConnection> | A bb8 connection pool with reasonable defaults |
Postgres | diesel-async-deadpool | diesel_deadpool::Pool<diesel_async::AsyncPgConnection> | A deadpool connection pool with reasonable defaults |
Lastly, add a macro annotation to the Shuttle main function. Here are examples for Postgres:
sqlx
compile-time checked macros.Parameters
Parameter | Type | Description |
---|---|---|
local_uri | &str | If specified, on local runs, use this database instead of starting a Docker container for it |
When passing in strings, you can also insert secrets from Secrets.toml
using string interpolation.
To insert the PASSWORD
secret, pass it in like this:
Secrets.dev.toml
, you need to set the same secret in Secrets.toml
to a empty string so that this step does not crash in deployment.The URI should be formatted according to the Postgres documentation.
If you do not specify a local_uri
, then cargo-shuttle will attempt to spin up a Docker container and launch the database inside of it.
For this to succeed, you must have Docker installed and you must also have started the Docker engine. If you have not used Docker
before, the easiest way is to install the desktop app and then launch it in order to start
the Docker engine.
Connection string
After deploying a project with a database macro, you can view the connection string with credentials with:
Using the connection string, you can connect to it for manual querying, inspection, and migration.
Example
The Shuttle main function below uses the #[shuttle_shared_db::Postgres]
attribute macro to provision a shared Postgres database,
which can be accessed with an sqlx Pool.
The full example can be found on GitHub.