mirror of https://github.com/rwf2/Rocket.git
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:
parent
7b050ebaae
commit
30cf75335b
|
@ -11,6 +11,8 @@ serde = "1.0"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
serde_derive = "1.0"
|
serde_derive = "1.0"
|
||||||
diesel = { version = "1.3", features = ["sqlite", "r2d2"] }
|
diesel = { version = "1.3", features = ["sqlite", "r2d2"] }
|
||||||
|
diesel_migrations = "1.3"
|
||||||
|
log = "0.4"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
parking_lot = { version = "0.6", features = ["nightly"] }
|
parking_lot = { version = "0.6", features = ["nightly"] }
|
||||||
|
|
|
@ -6,23 +6,3 @@ a result, you'll need to have `sqlite3` and its headers installed:
|
||||||
* **OS X:** `brew install sqlite`
|
* **OS X:** `brew install sqlite`
|
||||||
* **Debian/Ubuntu:** `apt-get install libsqlite3-dev`
|
* **Debian/Ubuntu:** `apt-get install libsqlite3-dev`
|
||||||
* **Arch:** `pacman -S sqlite`
|
* **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"
|
|
||||||
```
|
|
||||||
|
|
|
@ -3,15 +3,4 @@
|
||||||
SCRIPT_PATH=$(cd "$(dirname "$0")" ; pwd -P)
|
SCRIPT_PATH=$(cd "$(dirname "$0")" ; pwd -P)
|
||||||
DATABASE_URL="${SCRIPT_PATH}/db/db.sqlite"
|
DATABASE_URL="${SCRIPT_PATH}/db/db.sqlite"
|
||||||
|
|
||||||
pushd "${SCRIPT_PATH}" > /dev/null
|
rm -f "${DATABASE_URL}"
|
||||||
# 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
|
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
|
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use] extern crate rocket;
|
||||||
#[macro_use] extern crate diesel;
|
#[macro_use] extern crate diesel;
|
||||||
|
#[macro_use] extern crate diesel_migrations;
|
||||||
|
#[macro_use] extern crate log;
|
||||||
#[macro_use] extern crate serde_derive;
|
#[macro_use] extern crate serde_derive;
|
||||||
extern crate rocket_contrib;
|
extern crate rocket_contrib;
|
||||||
|
|
||||||
|
@ -10,6 +12,7 @@ mod task;
|
||||||
#[cfg(test)] mod tests;
|
#[cfg(test)] mod tests;
|
||||||
|
|
||||||
use rocket::Rocket;
|
use rocket::Rocket;
|
||||||
|
use rocket::fairing::AdHoc;
|
||||||
use rocket::request::{Form, FlashMessage};
|
use rocket::request::{Form, FlashMessage};
|
||||||
use rocket::response::{Flash, Redirect};
|
use rocket::response::{Flash, Redirect};
|
||||||
use rocket_contrib::{Template, databases::database, static_files::StaticFiles};
|
use rocket_contrib::{Template, databases::database, static_files::StaticFiles};
|
||||||
|
@ -17,6 +20,11 @@ use diesel::SqliteConnection;
|
||||||
|
|
||||||
use task::{Task, Todo};
|
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")]
|
#[database("sqlite_database")]
|
||||||
pub struct DbConn(SqliteConnection);
|
pub struct DbConn(SqliteConnection);
|
||||||
|
|
||||||
|
@ -74,6 +82,16 @@ fn index(msg: Option<FlashMessage>, conn: DbConn) -> Template {
|
||||||
fn rocket() -> (Rocket, Option<DbConn>) {
|
fn rocket() -> (Rocket, Option<DbConn>) {
|
||||||
let rocket = rocket::ignite()
|
let rocket = rocket::ignite()
|
||||||
.attach(DbConn::fairing())
|
.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("/", StaticFiles::from("static/"))
|
||||||
.mount("/", routes![index])
|
.mount("/", routes![index])
|
||||||
.mount("/todo", routes![new, toggle, delete])
|
.mount("/todo", routes![new, toggle, delete])
|
||||||
|
|
Loading…
Reference in New Issue