Skip to content

Commit 2e1658e

Browse files
hamptokrabonander
authored andcommitted
feat: adds the setup command to the sqlx-cli
The reset command is the equivalent of `sqlx db create`, and `sqlx migrate run`.
1 parent 916f1fc commit 2e1658e

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

sqlx-cli/src/database.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::migrate;
12
use console::style;
23
use dialoguer::Confirm;
34
use sqlx::any::Any;
@@ -30,3 +31,13 @@ pub async fn drop(uri: &str, confirm: bool) -> anyhow::Result<()> {
3031

3132
Ok(())
3233
}
34+
35+
pub async fn reset(uri: &str, confirm: bool) -> anyhow::Result<()> {
36+
drop(uri, confirm).await?;
37+
setup(uri).await
38+
}
39+
40+
pub async fn setup(uri: &str) -> anyhow::Result<()> {
41+
create(uri).await?;
42+
migrate::run(uri).await
43+
}

sqlx-cli/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,8 @@ hint: This command only works in the manifest directory of a Cargo package."#
3939
Command::Database(database) => match database.command {
4040
DatabaseCommand::Create => database::create(&database_url).await?,
4141
DatabaseCommand::Drop { yes } => database::drop(&database_url, !yes).await?,
42-
DatabaseCommand::Reset { yes } => {
43-
database::drop(&database_url, yes).await?;
44-
database::create(&database_url).await?;
45-
migrate::run(&database_url).await?;
46-
}
42+
DatabaseCommand::Reset { yes } => database::reset(&database_url, yes).await?,
43+
DatabaseCommand::Setup => database::setup(&database_url).await?,
4744
},
4845

4946
Command::Prepare { check: false, args } => prepare::run(&database_url, args)?,

sqlx-cli/src/opt.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ pub enum DatabaseCommand {
6464
#[clap(short)]
6565
yes: bool,
6666
},
67+
/// Creates the database specified in your DATABASE_URL and runs any pending migrations.
68+
Setup,
6769
}
6870

6971
/// Group of commands for creating and running migrations.

0 commit comments

Comments
 (0)