Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion sqlx-cli/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,16 @@ pub async fn setup(
connect_opts: &ConnectOpts,
) -> anyhow::Result<()> {
create(connect_opts).await?;
migrate::run(config, migration_source, connect_opts, false, false, None).await
migrate::run(
config,
migration_source,
connect_opts,
false,
false,
None,
false,
)
.await
}

async fn ask_to_continue_drop(db_url: String) -> bool {
Expand Down
27 changes: 26 additions & 1 deletion sqlx-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use futures_util::TryFutureExt;
use sqlx::AnyConnection;
use tokio::{select, signal};

use crate::opt::{Command, ConnectOpts, DatabaseCommand, MigrateCommand};
use crate::opt::{Command, ConnectOpts, DatabaseCommand, MigrateCommand, OverrideCommand};

pub mod database;
pub mod metadata;
Expand Down Expand Up @@ -99,6 +99,7 @@ async fn do_run(opt: Opt) -> anyhow::Result<()> {
dry_run,
*ignore_missing,
target_version,
false,
)
.await?
}
Expand All @@ -124,6 +125,30 @@ async fn do_run(opt: Opt) -> anyhow::Result<()> {
)
.await?
}
MigrateCommand::Override { command } => match command {
OverrideCommand::Skip {
source,
config,
mut connect_opts,
dry_run,
ignore_missing,
target_version,
} => {
let config = config.load_config().await?;
connect_opts.populate_db_url(&config)?;

migrate::run(
&config,
&source,
&connect_opts,
dry_run,
*ignore_missing,
target_version,
true,
)
.await?
}
},
MigrateCommand::Info {
source,
config,
Expand Down
12 changes: 9 additions & 3 deletions sqlx-cli/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ pub async fn run(
dry_run: bool,
ignore_missing: bool,
target_version: Option<i64>,
skip: bool,
) -> anyhow::Result<()> {
let migrator = migration_source.resolve(config).await?;

Expand Down Expand Up @@ -277,18 +278,23 @@ pub async fn run(
}
}
None => {
let skip =
let exceeds_target =
target_version.is_some_and(|target_version| migration.version > target_version);

let elapsed = if dry_run || skip {
let elapsed = if dry_run || exceeds_target {
Duration::new(0, 0)
} else if skip {
conn.skip(config.migrate.table_name(), migration).await?;
Duration::new(0, 0)
} else {
conn.apply(config.migrate.table_name(), migration).await?
};
let text = if skip {
let text = if exceeds_target {
"Skipped"
} else if dry_run {
"Can apply"
} else if skip {
"Skipped on request"
} else {
"Applied"
};
Expand Down
33 changes: 33 additions & 0 deletions sqlx-cli/src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@ pub enum MigrateCommand {
target_version: Option<i64>,
},

/// Override migration state, potentially dangerous operations.
Override {
#[clap(subcommand)]
command: OverrideCommand,
},

/// Revert the latest migration with a down file.
Revert {
#[clap(flatten)]
Expand Down Expand Up @@ -300,6 +306,33 @@ pub enum MigrateCommand {
},
}

#[derive(clap::Subcommand, Debug)]
pub enum OverrideCommand {
/// Skip all pending migrations without running them.
Skip {
#[clap(flatten)]
source: MigrationSourceOpt,

#[clap(flatten)]
config: ConfigOpt,

#[clap(flatten)]
connect_opts: ConnectOpts,

/// List all the migrations to be skipped without marking them as applied.
#[clap(long)]
dry_run: bool,

#[clap(flatten)]
ignore_missing: IgnoreMissing,

/// Apply migrations up to the specified version. If unspecified, apply all
/// pending migrations. If already at the target version, then no-op.
#[clap(long)]
target_version: Option<i64>,
},
}

#[derive(Args, Debug)]
pub struct AddMigrationOpts {
pub description: String,
Expand Down
36 changes: 25 additions & 11 deletions sqlx-cli/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ pub struct TestDatabase {
pub config_path: Option<PathBuf>,
}

pub enum MigrateCommand {
Run,
Revert,
Skip,
}

impl AsRef<str> for MigrateCommand {
fn as_ref(&self) -> &str {
match self {
MigrateCommand::Run => "run",
MigrateCommand::Revert => "revert",
MigrateCommand::Skip => "override skip",
}
}
}

impl TestDatabase {
pub fn new(name: &str, migrations: &str) -> Self {
// Note: only set when _building_
Expand Down Expand Up @@ -58,19 +74,17 @@ impl TestDatabase {
format!("sqlite://{}", self.file_path.display())
}

pub fn run_migration(&self, revert: bool, version: Option<i64>, dry_run: bool) -> Assert {
pub fn run_migration(
&self,
migrate_command: MigrateCommand,
version: Option<i64>,
dry_run: bool,
) -> Assert {
let mut command = Command::cargo_bin("sqlx").unwrap();
command
.args([
"migrate",
match revert {
true => "revert",
false => "run",
},
"--database-url",
&self.connection_string(),
"--source",
])
.arg("migrate")
.args(migrate_command.as_ref().split_whitespace())
.args(["--database-url", &self.connection_string(), "--source"])
.arg(&self.migrations_path);

if let Some(config_path) = &self.config_path {
Expand Down
Loading