Skip to content

Commit 4092929

Browse files
committed
Merge pull request diesel-rs#163 from mcasper/dotenv_for_cli
Allow the user to set the DATABASE_URL through dotenv
2 parents 3a27342 + 628e272 commit 4092929

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

@@ -251,6 +252,8 @@ fn handle_error<E: Error>(error: E) {
251252
}
252253

253254
fn database_url(matches: &ArgMatches) -> String {
255+
dotenv::dotenv().ok();
256+
254257
matches.value_of("DATABASE_URL")
255258
.map(|s| s.into())
256259
.or(env::var("DATABASE_URL").ok())
@@ -312,9 +315,10 @@ where I: Iterator<Item = A> + Clone,
312315
#[cfg(test)]
313316
mod tests {
314317
extern crate diesel;
315-
extern crate dotenv;
316318
extern crate tempdir;
317319

320+
use dotenv::dotenv;
321+
318322
use self::tempdir::TempDir;
319323
use self::diesel::Connection;
320324
use self::diesel::connection::PgConnection;
@@ -329,7 +333,7 @@ mod tests {
329333
use std::path::PathBuf;
330334

331335
fn database_url() -> String {
332-
dotenv::dotenv().ok();
336+
dotenv().ok();
333337
env::var("DATABASE_URL")
334338
.expect("DATABASE_URL must be set in order to run diesel_cli tests")
335339
}

0 commit comments

Comments
 (0)