Skip to content

Commit 9d04576

Browse files
committed
Implement diesel database reset, and move diesel setup to `diesel
database setup` This has a lot of changes because it does a few things at the same time: * Changed most functions to take a generic Connection. This gets us a little closer to making the CLI generic over the backend, and allows for easier testing, as it gives us control over the connection it uses. * Document more functions and add some messages to the user when running setup/reset. * Add the `diesel database reset` command, and move `diesel setup` to be a subcommand of `diesel database`.
1 parent c784e47 commit 9d04576

4 files changed

Lines changed: 189 additions & 51 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
3434

3535
* Added `sum` and `avg` functions.
3636

37+
* Added the `diesel setup`, `diesel database setup`, and `diesel database
38+
reset` commands to the CLI.
39+
3740
### Changed
3841

3942
* Rename both the `#[derive(Queriable)]` attribute and the `Queriable` trait to

diesel_cli/README.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ diesel setup --database-url='postgres://localhost/my_db'
1313
diesel migration generate create_users_table
1414
```
1515

16-
You'll see that two files were generated for you,
16+
You'll see that a `migrations/` directory was generated for you (by the setup
17+
command), and two sql files were generated,
1718
`migrations/{current_timestamp}_create_users_table/up.sql` and
1819
`migrations/{current_timestamp}_create_users_table/down.sql`. You should edit
1920
these files to show how to update your schema, and how to undo that change.
@@ -32,25 +33,37 @@ CREATE TABLE users (
3233
DROP TABLE USERS;
3334
```
3435

35-
You can then run your new migration by running `diesel migration run`. Make sure
36-
that you set the `DATABASE_URL` environment variable first, or pass it directly
37-
by doing `diesel migration run --database-url="postgres://localhost/your_database"`
38-
Alternatively, you can call
39-
[`diesel::migrations::run_pending_migrations`][pending-migrations] from
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
4041
`build.rs`.
4142

4243
Diesel will automatically keep track of which migrations have already been run,
4344
ensuring that they're never run twice.
4445

45-
### Commands
46-
#### `setup`
46+
## Commands
47+
## `diesel setup`
4748
Searches for a `migrations/` directory, and if it can't find one, creates one
4849
in the same directory as the first `Cargo.toml` it finds. It then tries to
4950
connect to the provided DATABASE_URL, and will create the given database if it
5051
cannot connect to it. Finally it will create diesel's internal table for
5152
tracking which migrations have been run, and run any existing migrations if the
5253
internal table did not previously exist.
5354

55+
## `diesel database`
56+
#### `database setup`
57+
Tries to connect to the provided DATABASE_URL, and will create the given
58+
database if it cannot connect to it. It then creates diesel's internal
59+
migrations tracking table if it needs to be created, and runs any pending
60+
migrations if it created the internal table.
61+
62+
#### `database reset`
63+
Drops the database specified in your DATABASE_URL if it can, and then runs
64+
`database database setup`.
65+
66+
## `diesel migration`
5467
#### `migration generate`
5568
Takes the name of your migration as an argument, and will create a migration
5669
directory with `migrations/` in the format of
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,36 @@ use std::convert::From;
44
use std::{fmt, io};
55
use std::error::Error;
66

7-
use self::SetupError::*;
7+
use self::DatabaseError::*;
88

99
#[derive(Debug)]
10-
pub enum SetupError {
10+
pub enum DatabaseError {
1111
#[allow(dead_code)]
1212
CargoTomlNotFound,
1313
IoError(io::Error),
1414
QueryError(result::Error),
1515
ConnectionError(result::ConnectionError),
1616
}
1717

18-
impl From<io::Error> for SetupError {
18+
impl From<io::Error> for DatabaseError {
1919
fn from(e: io::Error) -> Self {
2020
IoError(e)
2121
}
2222
}
2323

24-
impl From<result::Error> for SetupError {
24+
impl From<result::Error> for DatabaseError {
2525
fn from(e: result::Error) -> Self {
2626
QueryError(e)
2727
}
2828
}
2929

30-
impl From<result::ConnectionError> for SetupError {
30+
impl From<result::ConnectionError> for DatabaseError {
3131
fn from(e: result::ConnectionError) -> Self {
3232
ConnectionError(e)
3333
}
3434
}
3535

36-
impl Error for SetupError {
36+
impl Error for DatabaseError {
3737
fn description(&self) -> &str {
3838
match *self {
3939
CargoTomlNotFound => "Unable to find Cargo.toml in this directory or any parent directories.",
@@ -44,13 +44,13 @@ impl Error for SetupError {
4444
}
4545
}
4646

47-
impl fmt::Display for SetupError {
47+
impl fmt::Display for DatabaseError {
4848
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
4949
self.description().fmt(f)
5050
}
5151
}
5252

53-
impl PartialEq for SetupError {
53+
impl PartialEq for DatabaseError {
5454
fn eq(&self, other: &Self) -> bool {
5555
match (self, other) {
5656
(

0 commit comments

Comments
 (0)