Skip to content

Commit

Permalink
Updated MigrateDatabase Trait + related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrajs16 committed Nov 10, 2023
1 parent 03a78c0 commit 73ab65c
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 15 deletions.
7 changes: 4 additions & 3 deletions sqlx-cli/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub async fn create(connect_opts: &ConnectOpts) -> anyhow::Result<()> {
Ok(())
}

pub async fn drop(connect_opts: &ConnectOpts, confirm: bool) -> anyhow::Result<()> {
pub async fn drop(connect_opts: &ConnectOpts, confirm: bool, force: bool) -> anyhow::Result<()> {
if confirm && !ask_to_continue(connect_opts) {
return Ok(());
}
Expand All @@ -33,7 +33,7 @@ pub async fn drop(connect_opts: &ConnectOpts, confirm: bool) -> anyhow::Result<(
let exists = crate::retry_connect_errors(connect_opts, Any::database_exists).await?;

if exists {
Any::drop_database(&connect_opts.database_url).await?;
Any::drop_database(&connect_opts.database_url, force).await?;
}

Ok(())
Expand All @@ -43,8 +43,9 @@ pub async fn reset(
migration_source: &str,
connect_opts: &ConnectOpts,
confirm: bool,
force: bool,
) -> anyhow::Result<()> {
drop(connect_opts, confirm).await?;
drop(connect_opts, confirm, force).await?;
setup(migration_source, connect_opts).await
}

Expand Down
6 changes: 4 additions & 2 deletions sqlx-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@ pub async fn run(opt: Opt) -> Result<()> {
DatabaseCommand::Drop {
confirmation,
connect_opts,
} => database::drop(&connect_opts, !confirmation.yes).await?,
force,
} => database::drop(&connect_opts, !confirmation.yes, force).await?,
DatabaseCommand::Reset {
confirmation,
source,
connect_opts,
} => database::reset(&source, &connect_opts, !confirmation.yes).await?,
force,
} => database::reset(&source, &connect_opts, !confirmation.yes, force).await?,
DatabaseCommand::Setup {
source,
connect_opts,
Expand Down
8 changes: 8 additions & 0 deletions sqlx-cli/src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ pub enum DatabaseCommand {

#[clap(flatten)]
connect_opts: ConnectOpts,

/// PostgreSQL only: force drops the database.
#[clap(long, short, default_value = "false")]
force: bool,
},

/// Drops the database specified in your DATABASE_URL, re-creates it, and runs any pending migrations.
Expand All @@ -88,6 +92,10 @@ pub enum DatabaseCommand {

#[clap(flatten)]
connect_opts: ConnectOpts,

/// PostgreSQL only: force drops the database.
#[clap(long, short, default_value = "false")]
force: bool,
},

/// Creates the database specified in your DATABASE_URL and runs any pending migrations.
Expand Down
6 changes: 3 additions & 3 deletions sqlx-core/src/any/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl Debug for AnyDriver {
pub struct AnyMigrateDatabase {
create_database: DebugFn<fn(&str) -> BoxFuture<'_, crate::Result<()>>>,
database_exists: DebugFn<fn(&str) -> BoxFuture<'_, crate::Result<bool>>>,
drop_database: DebugFn<fn(&str) -> BoxFuture<'_, crate::Result<()>>>,
drop_database: DebugFn<fn(&str, bool) -> BoxFuture<'_, crate::Result<()>>>,
}

impl AnyMigrateDatabase {
Expand All @@ -105,8 +105,8 @@ impl AnyMigrateDatabase {
(self.database_exists)(url)
}

pub fn drop_database<'a>(&self, url: &'a str) -> BoxFuture<'a, crate::Result<()>> {
(self.drop_database)(url)
pub fn drop_database<'a>(&self, url: &'a str, force: bool) -> BoxFuture<'a, crate::Result<()>> {
(self.drop_database)(url, force)
}
}

Expand Down
6 changes: 3 additions & 3 deletions sqlx-core/src/any/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ impl MigrateDatabase for Any {
})
}

fn drop_database(url: &str) -> BoxFuture<'_, Result<(), Error>> {
Box::pin(async {
fn drop_database(url: &str, force: bool) -> BoxFuture<'_, Result<(), Error>> {
Box::pin(async move {
driver::from_url_str(url)?
.get_migrate_database()?
.drop_database(url)
.drop_database(url, force)
.await
})
}
Expand Down
2 changes: 1 addition & 1 deletion sqlx-core/src/migrate/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub trait MigrateDatabase {

// drop database in url
// uses a maintenance database depending on driver
fn drop_database(url: &str) -> BoxFuture<'_, Result<(), Error>>;
fn drop_database(url: &str, _force: bool) -> BoxFuture<'_, Result<(), Error>>;
}

// 'e = Executor
Expand Down
2 changes: 1 addition & 1 deletion sqlx-mysql/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl MigrateDatabase for MySql {
})
}

fn drop_database(url: &str) -> BoxFuture<'_, Result<(), Error>> {
fn drop_database(url: &str, _force: bool) -> BoxFuture<'_, Result<(), Error>> {
Box::pin(async move {
let (options, database) = parse_for_maintenance(url)?;
let mut conn = options.connect().await?;
Expand Down
2 changes: 1 addition & 1 deletion sqlx-postgres/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl MigrateDatabase for Postgres {
})
}

fn drop_database(url: &str) -> BoxFuture<'_, Result<(), Error>> {
fn drop_database(url: &str, force: bool) -> BoxFuture<'_, Result<(), Error>> {
Box::pin(async move {
let (options, database) = parse_for_maintenance(url)?;
let mut conn = options.connect().await?;
Expand Down
2 changes: 1 addition & 1 deletion sqlx-sqlite/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl MigrateDatabase for Sqlite {
})
}

fn drop_database(url: &str) -> BoxFuture<'_, Result<(), Error>> {
fn drop_database(url: &str, _force: bool) -> BoxFuture<'_, Result<(), Error>> {
Box::pin(async move {
let options = SqliteConnectOptions::from_str(url)?;

Expand Down

0 comments on commit 73ab65c

Please sign in to comment.