Database
Roadster provides built-in support for both SeaORM and Diesel.
SeaORM
When the db-sea-orm
feature is enabled, Roadster provides support for various SQL databases
via SeaORM, an ORM built on top of sqlx.
See the SeaORM docs for more details.
Migrator
To run your SeaORM migrations with Roadster, provide
your MigratorTrait
type
to Roadster. This is done by providing the migrator via the
RoadsterAppBuilder#sea_orm_migrator
method
fn build_app() -> App {
RoadsterApp::builder()
.state_provider(|context| Ok(context))
.sea_orm_migrator(Migrator)
.build()
}
or the
App#migrators
impl
pub struct MyApp;
#[async_trait]
impl App<AppContext> for MyApp {
type Cli = crate::cli::Cli;
async fn provide_state(&self, context: AppContext) -> RoadsterResult<AppContext> {
Ok(context)
}
fn migrators(
&self,
_state: &AppContext,
) -> RoadsterResult<Vec<Box<dyn roadster::db::migration::Migrator<AppContext>>>> {
Ok(vec![Box::new(SeaOrmMigrator::new(Migrator))])
}
}
Migration utilities
Roadster provides some utilities for defining common column types with SeaORM. See
the migration
module docs for the list of
utilities.
Diesel
When one of the db-diesel-*
features is enabled, Roadster provides support for various SQL databases
via Diesel. See the Diesel docs for more details.
Diesel connection pool
Because of Diesel's strong typing, Roadster has different features for each DB backend, each of which enables the respective DB connection pool. In addition to the non-async connections provided by diesel, async versions of the Postgres and Mysql connections are supported via diesel-async.
Feature | Connection type |
---|---|
db-diesel-postgres-pool | Non-async Postgres |
db-diesel-mysql-pool | Non-async Mysql |
db-diesel-sqlite-pool | Non-async Sqlite |
db-diesel-postgres-pool-async | Async Postgres |
db-diesel-mysql-pool-async | Async Mysql |
Migrator
To run your Diesel migrations with Roadster, provide
your migrations to Roadster. This is done by setting providing the migrator in the
RoadsterAppBuilder#diesel_migrator
method
const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations");
fn build_app() -> App {
RoadsterApp::builder()
.state_provider(|context| Ok(context))
.diesel_migrator::<roadster::db::DieselPgConn>(MIGRATIONS)
.build()
}
or the
App#migrators
impl
pub struct MyApp;
const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations");
#[async_trait]
impl App<AppContext> for MyApp {
type Cli = crate::cli::Cli;
async fn provide_state(&self, context: AppContext) -> RoadsterResult<AppContext> {
Ok(context)
}
fn migrators(
&self,
_state: &AppContext,
) -> RoadsterResult<Vec<Box<dyn roadster::db::migration::Migrator<AppContext>>>> {
Ok(vec![Box::new(
DieselMigrator::<roadster::db::DieselPgConn>::new(MIGRATIONS),
)])
}
}
Running migrations
Automatically
Roadster can automatically run your migrations when your app is starting. This behavior is configured by the
database.auto-migrate
config field.
[database]
auto-migrate = true # change to `false` to disable
Via the Roadster CLI
You can also manually run migrations via the CLI (when the cli
feature is enabled).
cargo run -- roadster migrate up
Note that this does not generate schemas/entities that may be generated by the SeaORM/Diesel CLI utilities. This only runs the migrations on the connected DB.
Via ORM CLI
SeaORM
sea migrate up
See: https://crates.io/crates/sea-orm-cli
Diesel
diesel migration run