-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement most of the Diesel CLI for 0.4.0 #79
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/sh | ||
cd diesel_cli && cargo run -- $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#!/bin/sh | ||
(cd diesel && cargo test --features unstable) && | ||
(cd diesel_cli && cargo test) && | ||
(cd diesel_tests && cargo test --features unstable --no-default-features) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "diesel_cli" | ||
version = "0.4.0" | ||
authors = ["Sean Griffin <[email protected]>"] | ||
|
||
[[bin]] | ||
name = "diesel" | ||
|
||
[dependencies] | ||
diesel = "^0.3.0" | ||
clap = "^1.5.5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#[macro_use] | ||
extern crate clap; | ||
extern crate diesel; | ||
|
||
use clap::{App, AppSettings, Arg, ArgMatches, SubCommand}; | ||
use diesel::migrations; | ||
use std::env; | ||
|
||
fn main() { | ||
let database_arg = || Arg::with_name("DATABASE_URL") | ||
.long("database-url") | ||
.help("Specifies the database URL to connect to. Falls back to \ | ||
the DATABASE_URL environment variable if unspecified.") | ||
.takes_value(true); | ||
|
||
let migration_subcommand = SubCommand::with_name("migration") | ||
.setting(AppSettings::VersionlessSubcommands) | ||
.subcommand( | ||
SubCommand::with_name("run") | ||
.about("Runs all pending migrations") | ||
.arg(database_arg()) | ||
).subcommand( | ||
SubCommand::with_name("revert") | ||
.about("Reverts the latest run migration") | ||
.arg(database_arg()) | ||
).subcommand( | ||
SubCommand::with_name("redo") | ||
.about("Reverts and re-runs the latest migration. Useful \ | ||
for testing that a migration can in fact be reverted.") | ||
.arg(database_arg()) | ||
).subcommand( | ||
SubCommand::with_name("generate") | ||
.about("Generate a new migration with the given name, and \ | ||
the current timestamp as the version") | ||
.arg(Arg::with_name("MIGRATION_NAME") | ||
.help("The name of the migration to create") | ||
.required(true) | ||
) | ||
).setting(AppSettings::SubcommandRequiredElseHelp); | ||
|
||
let matches = App::new("diesel") | ||
.version(env!("CARGO_PKG_VERSION")) | ||
.setting(AppSettings::VersionlessSubcommands) | ||
.subcommand(migration_subcommand) | ||
.setting(AppSettings::SubcommandRequiredElseHelp) | ||
.get_matches(); | ||
|
||
match matches.subcommand() { | ||
("migration", Some(matches)) => run_migration_command(matches), | ||
_ => unreachable!(), | ||
} | ||
} | ||
|
||
// FIXME: We can improve the error handling instead of `unwrap` here. | ||
fn run_migration_command(matches: &ArgMatches) { | ||
match matches.subcommand() { | ||
("run", Some(args)) => { | ||
migrations::run_pending_migrations(&connection(&database_url(args))) | ||
.unwrap(); | ||
} | ||
("revert", Some(args)) => { | ||
migrations::revert_latest_migration(&connection(&database_url(args))) | ||
.unwrap(); | ||
} | ||
("redo", Some(args)) => { | ||
let connection = connection(&database_url(args)); | ||
connection.transaction(|| { | ||
let reverted_version = try!(migrations::revert_latest_migration(&connection)); | ||
migrations::run_migration_with_version(&connection, &reverted_version) | ||
}).unwrap(); | ||
} | ||
("generate", Some(args)) => { | ||
panic!("Migration generator is not implemented this pass") | ||
} | ||
_ => unreachable!("The cli parser should prevent reaching here"), | ||
} | ||
} | ||
|
||
fn database_url(matches: &ArgMatches) -> String { | ||
matches.value_of("DATABASE_URL") | ||
.map(|s| s.into()) | ||
.or(env::var("DATABASE_URL").ok()) | ||
.expect("The --database-url argument must be passed, \ | ||
or the DATABASE_URL environment variable must be set.") | ||
} | ||
|
||
fn connection(database_url: &str) -> diesel::Connection { | ||
diesel::Connection::establish(database_url) | ||
.expect(&format!("Error connecting to {}", database_url)) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this branch is at a different level of abstraction than the others. What do you think about extracting this into a function also?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If anything I'm actually thinking of moving the other branches down. I don't like having
#[doc(hidden)]
public functions in the main library, and really the only "public" use case I can see is running pending migrations as part of your build script.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
given that this is main.rs, are any of the things in here actually public?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, that's what I mean. I'd rather move more of the logic into here and out of diesel proper.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah ok. Either way, this is mostly fine, it just caught me as a little odd that the branches weren't balanced. Do with that information what your much better informed than me self wants to do :)