I need to run Diesel database migrations in production for a Rocket-based app. Generally there are to ways to perform migrations for the database:
- At application startup.
- Separately from application startup.
I prefer the second option that would be invoked by using the --migrate
flag for the application binary, but as the target application is fairly simple the first way will do just fine.
There is a thread in the Diesel issue tracker about running migrations in production and advise on how to do it:
- Add
diesel_migrations
to your dependencies- Include an
extern crate diesel_migrations
in your crate, and make sure to decorate it with#[macro_use]
- At the beginning of your code, add
embed_migrations!()
- To run the migrations, Use
embedded_migrations::run(&db_conn)
In main.rs
I made:
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_migrations;
#[macro_use]
extern crate rocket;
#[macro_use]
extern crate rocket_contrib;
#[database("my_db_name")]
pub struct DbConn(diesel::PgConnection);
fn main() {
// Update database
embed_migrations!();
embedded_migrations::run(&DbConn);
// Launch the app
...
}
This leads to the error:
error[E0277]: the trait bound `fn(diesel::r2d2::PooledConnection<<diesel::PgConnection as rocket_contrib::databases::Poolable>::Manager>) -> DbConn {DbConn}: diesel::Connection` is not satisfied
--> src/main.rs:30:30
|
29 | embed_migrations!();
| --------------------
| |
| required by this bound in `main::embedded_migrations::run`
30 | embedded_migrations::run(&DbConn);
| ^^^^^^^ the trait `diesel::Connection` is not implemented for `fn(diesel::r2d2::PooledConnection<<diesel::PgConnection as rocket_contrib::databases::Poolable>::Manager>) -> DbConn {DbConn}`
|
= note: required because of the requirements on the impl of `diesel_migrations::MigrationConnection` for `fn(diesel::r2d2::PooledConnection<<diesel::PgConnection as rocket_contrib::databases::Poolable>::Manager>) -> DbConn {DbConn}`
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
How to fix it?