database - 如何在生产环境中使用 Rocket 运行 Diesel 迁移?

标签 database rust database-migration rust-diesel rust-rocket

我需要在生产环境中为基于 Rocket 的应用运行 Diesel 数据库迁移。通常有以下几种方法可以对数据库执行迁移:

  1. 在应用程序启动时。
  2. 与应用程序启动分开。

我更喜欢通过使用应用程序二进制文件的 --migrate 标志调用的第二个选项,但由于目标应用程序相当简单,第一种方法就可以了。

有一个thread in the Diesel issue tracker关于在生产中运行迁移并就如何进行迁移提出建议:

  1. Add diesel_migrations to your dependencies
  2. Include an extern crate diesel_migrations in your crate, and make sure to decorate it with #[macro_use]
  3. At the beginning of your code, add embed_migrations!()
  4. To run the migrations, Use embedded_migrations::run(&db_conn)

main.rs 我做了:

#![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
    ...
}

这会导致错误:

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)

如何解决?

最佳答案

在谷歌上搜索了更多并找到了工作示例 here .

关键代码

use rocket::Rocket;
use rocket::fairing::AdHoc;

// This macro from `diesel_migrations` defines an `embedded_migrations` module
// containing a function named `run`. This allows the example to be run and
// tested without any outside setup of the database.
embed_migrations!();

#[database("sqlite_database")]
pub struct DbConn(SqliteConnection);

fn run_db_migrations(rocket: Rocket) -> Result<Rocket, Rocket> {
    let conn = DbConn::get_one(&rocket).expect("database connection");
    match embedded_migrations::run(&*conn) {
        Ok(()) => Ok(rocket),
        Err(e) => {
            error!("Failed to run database migrations: {:?}", e);
            Err(rocket)
        }
    }
}

fn rocket() -> Rocket {
    rocket::ignite()
        .attach(DbConn::fairing())
        .attach(AdHoc::on_attach("Database Migrations", run_db_migrations))
        .mount("/", StaticFiles::from("static/"))
        .mount("/", routes![index])
        .mount("/todo", routes![new, toggle, delete])
        .attach(Template::fairing())
}

关于database - 如何在生产环境中使用 Rocket 运行 Diesel 迁移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61047355/

相关文章:

rust - 添加到基于 RefCell 构建的二叉树时,借用的值不会存在足够长的时间

mysql - Grails 数据库迁移插件问题

rust - 为什么 Vec<&str> 在这里缺少生命周期说明符?

rust - clap - 返回 ArgMatches<'static> 时如何传递 default_value

azure - 从 Azure 表存储到 Cosmos DB 表存储的数据迁移,

postgresql - Dbup(.net 库)和 Powershell for Postgresql

php - PHP 中准备好的 SQL 语句不更新 MySQL 数据库行

arrays - 不按顺序搜索 MongoDB 数组

database - 有没有办法在 XQuery 中获取 MarkLogic 服务器中的所有数据库名称?

database - Zend 框架。管理您的网站/应用程序的最佳方式是什么?