Embed the diesel database migrations in the `todo` example.

`diesel_cli` is no longer needed for running the example or its tests.
This commit is contained in:
jeb 2018-09-11 12:35:33 -07:00
parent 7b050ebaae
commit 30cf75335b
4 changed files with 21 additions and 32 deletions

View File

@ -11,6 +11,8 @@ serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
diesel = { version = "1.3", features = ["sqlite", "r2d2"] }
diesel_migrations = "1.3"
log = "0.4"
[dev-dependencies]
parking_lot = { version = "0.6", features = ["nightly"] }

View File

@ -6,23 +6,3 @@ a result, you'll need to have `sqlite3` and its headers installed:
* **OS X:** `brew install sqlite`
* **Debian/Ubuntu:** `apt-get install libsqlite3-dev`
* **Arch:** `pacman -S sqlite`
## Running
**Before running, building, or testing this example, you'll need to ensure that
a SQLite database file with the proper schema is present.**
On a Unix machine or with bash installed, you can simply run the `boostrap.sh`
script to create the database. The script installs the `diesel_cli` tools if
they're not already installed and runs the migrations.
You can also install the Diesel CLI and run the migrations manually with the
following commands:
```sh
# install Diesel CLI tools
cargo install diesel_cli --version '<= 1.2' --no-default-features --features=sqlite
# create db/db.sqlite
diesel migration run --database-url="db/db.sqlite"
```

View File

@ -3,15 +3,4 @@
SCRIPT_PATH=$(cd "$(dirname "$0")" ; pwd -P)
DATABASE_URL="${SCRIPT_PATH}/db/db.sqlite"
pushd "${SCRIPT_PATH}" > /dev/null
# clear an existing database
rm -f "${DATABASE_URL}"
# install the diesel CLI tools if they're not installed
if ! command -v diesel >/dev/null 2>&1; then
cargo install diesel_cli --version '<= 1.2' --no-default-features --features=sqlite > /dev/null
fi
# create db/db.sqlite
diesel migration --database-url="${DATABASE_URL}" run > /dev/null
popd > /dev/null
rm -f "${DATABASE_URL}"

View File

@ -3,6 +3,8 @@
#[macro_use] extern crate rocket;
#[macro_use] extern crate diesel;
#[macro_use] extern crate diesel_migrations;
#[macro_use] extern crate log;
#[macro_use] extern crate serde_derive;
extern crate rocket_contrib;
@ -10,6 +12,7 @@ mod task;
#[cfg(test)] mod tests;
use rocket::Rocket;
use rocket::fairing::AdHoc;
use rocket::request::{Form, FlashMessage};
use rocket::response::{Flash, Redirect};
use rocket_contrib::{Template, databases::database, static_files::StaticFiles};
@ -17,6 +20,11 @@ use diesel::SqliteConnection;
use task::{Task, Todo};
// 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);
@ -74,6 +82,16 @@ fn index(msg: Option<FlashMessage>, conn: DbConn) -> Template {
fn rocket() -> (Rocket, Option<DbConn>) {
let rocket = rocket::ignite()
.attach(DbConn::fairing())
.attach(AdHoc::on_attach("Database Migrations", |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)
},
}
}))
.mount("/", StaticFiles::from("static/"))
.mount("/", routes![index])
.mount("/todo", routes![new, toggle, delete])