Skip to content

Commit 628e272

Browse files
committed
Allow the user to set the DATABASE_URL through dotenv
This allows the user to set their DATABASE_URL for the diesel CLI in their `.env` file. I also took the chance to bump the dotenv version.
1 parent a82dc63 commit 628e272

3 files changed

Lines changed: 23 additions & 11 deletions

File tree

diesel_cli/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ keywords = ["orm", "database", "postgres", "postgresql", "sql"]
1313
name = "diesel"
1414

1515
[dependencies]
16-
diesel = "^0.4.0"
17-
clap = "^1.5.5"
1816
chrono = "^0.2.17"
17+
clap = "^1.5.5"
18+
diesel = "^0.4.0"
19+
dotenv = "^0.8.0"
1920

2021
[dev-dependencies]
21-
dotenv = "^0.6.0"
2222
tempdir = "^0.3.4"

diesel_cli/README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,24 @@ CREATE TABLE users (
3333
DROP TABLE USERS;
3434
```
3535

36-
You can then run your new migration by running `diesel migration run`. Make
37-
sure that you set the `DATABASE_URL` environment variable first, or pass it
38-
directly by doing `diesel migration run
39-
--database-url="postgres://localhost/your_database"` Alternatively, you can
40-
call [`diesel::migrations::run_pending_migrations`][pending-migrations] from
36+
You can then run your new migration by running `diesel migration run`. Your
37+
DATABASE_URL must be set in order to run this command, and there are serveral
38+
ways that you can set it:
39+
40+
* Set it as an environment variable manually
41+
* Set it as an environment variable using [rust-dotenv][rust-dotenv]
42+
* Pass it directly by adding the `--database-url` flag
43+
44+
As an alternative to running migrations with the CLI, you can call
45+
[`diesel::migrations::run_pending_migrations`][pending-migrations] from
4146
`build.rs`.
4247

4348
Diesel will automatically keep track of which migrations have already been run,
4449
ensuring that they're never run twice.
4550

46-
## Commands
51+
Commands
52+
--------
53+
4754
## `diesel setup`
4855
Searches for a `migrations/` directory, and if it can't find one, creates one
4956
in the same directory as the first `Cargo.toml` it finds. It then tries to
@@ -81,3 +88,4 @@ Runs the `down.sql` for the most recent migration.
8188
Runs the `down.sql` and then the `up.sql` for the most recent migration.
8289

8390
[pending-migrations]: http://sgrif.github.io/diesel/diesel/migrations/fn.run_pending_migrations.html
91+
[rust-dotenv]: https://github.com/slapresta/rust-dotenv#examples

diesel_cli/src/main.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ extern crate chrono;
22
#[macro_use]
33
extern crate clap;
44
extern crate diesel;
5+
extern crate dotenv;
56

67
mod database_error;
78

@@ -247,6 +248,8 @@ fn handle_error<E: Error>(error: E) {
247248
}
248249

249250
fn database_url(matches: &ArgMatches) -> String {
251+
dotenv::dotenv().ok();
252+
250253
matches.value_of("DATABASE_URL")
251254
.map(|s| s.into())
252255
.or(env::var("DATABASE_URL").ok())
@@ -262,9 +265,10 @@ fn connection(database_url: &str) -> PgConnection {
262265
#[cfg(test)]
263266
mod tests {
264267
extern crate diesel;
265-
extern crate dotenv;
266268
extern crate tempdir;
267269

270+
use dotenv::dotenv;
271+
268272
use self::tempdir::TempDir;
269273
use self::diesel::Connection;
270274
use self::diesel::connection::PgConnection;
@@ -278,7 +282,7 @@ mod tests {
278282
use std::{env, fs};
279283

280284
fn database_url() -> String {
281-
dotenv::dotenv().ok();
285+
dotenv().ok();
282286
env::var("DATABASE_URL")
283287
.expect("DATABASE_URL must be set in order to run diesel_cli tests")
284288
}

0 commit comments

Comments
 (0)