Use static path to database file in todo example.

This commit is contained in:
Sergio Benitez 2018-05-29 15:24:38 +02:00
parent d17b392538
commit b12058110d
3 changed files with 14 additions and 34 deletions

View File

@ -9,33 +9,20 @@ a result, you'll need to have `sqlite3` and its headers installed:
## Running
**Before running, building, or testing this example, you'll need to ensure the
following:**
**Before running, building, or testing this example, you'll need to ensure that
a SQLite database file with the proper schema is present.**
1. 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.
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. The script will output a `DATABASE_URL` variable.
You can also install the Diesel CLI and run the migrations manually with the
following commands:
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 --no-default-features --features sqlite
```
# install Diesel CLI tools
cargo install diesel_cli --no-default-features --features sqlite
# create db/db.sql
DATABASE_URL=db/db.sql diesel migration run
```
2. A `DATABASE_URL` environment variable is set that points to the SQLite
database file.
Use the `DATABASE_URL` variable emitted from the `bootstrap.sh` script, or
enter it manually, as follows:
* `DATABASE_URL=db/db.sql cargo build`
* `DATABASE_URL=db/db.sql cargo test`
* `DATABASE_URL=db/db.sql cargo run`
# create db/db.sql
diesel migration run --database-url="db/db.sql"
```

View File

@ -1,7 +0,0 @@
CREATE TABLE tasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
description VARCHAR NOT NULL,
completed BOOLEAN NOT NULL DEFAULT 0
);
INSERT INTO tasks (description) VALUES ("demo task");

View File

@ -9,7 +9,7 @@ use rocket::{Request, State, Outcome};
pub type SqlitePool = Pool<ConnectionManager<SqliteConnection>>;
pub const DATABASE_URL: &'static str = env!("DATABASE_URL");
pub const DATABASE_URL: &'static str = concat!(env!("CARGO_MANIFEST_DIR"), "/db/db.sql");
pub fn init_pool() -> SqlitePool {
let manager = ConnectionManager::<SqliteConnection>::new(DATABASE_URL);