Skip to content
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

migration up or down to the specific version #759

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
10 changes: 7 additions & 3 deletions sea-orm-cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,19 +248,20 @@ pub fn run_migrate_command(matches: &ArgMatches<'_>) -> Result<(), Box<dyn Error
update_migrator(&migration_name, migration_dir)?;
return Ok(());
}
let (subcommand, migration_dir, steps, verbose) = match migrate_subcommand {
let (subcommand, migration_dir, steps, version, verbose) = match migrate_subcommand {
// Catch all command with pattern `migrate xxx`
(subcommand, Some(args)) => {
let migration_dir = args.value_of("MIGRATION_DIR").unwrap();
let steps = args.value_of("NUM_MIGRATION");
let version = args.value_of("VERSION_MIGRATION");
let verbose = args.is_present("VERBOSE");
(subcommand, migration_dir, steps, verbose)
(subcommand, migration_dir, steps, version, verbose)
}
// Catch command `migrate`, this will be treated as `migrate up`
_ => {
let migration_dir = matches.value_of("MIGRATION_DIR").unwrap();
let verbose = matches.is_present("VERBOSE");
("up", migration_dir, None, verbose)
("up", migration_dir, None, None, verbose)
}
};
// Construct the `--manifest-path`
Expand All @@ -280,6 +281,9 @@ pub fn run_migrate_command(matches: &ArgMatches<'_>) -> Result<(), Box<dyn Error
if let Some(steps) = steps {
args.extend(["-n", steps]);
}
if let Some(version) = version {
args.extend(["-V", version]);
}
if verbose {
args.push("-v");
}
Expand Down
26 changes: 20 additions & 6 deletions sea-orm-cli/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,30 @@ pub fn get_subcommands() -> Vec<App<'static, 'static>> {
.short("n")
.help("Number of pending migrations to be applied")
.takes_value(true),
)
.arg(
Arg::with_name("VERSION_MIGRATION")
.long("version")
.short("V")
.help("Version of pending migrations to be applied")
.takes_value(true),
),
SubCommand::with_name("down")
SubCommand::with_name("down")
.about("Rollback applied migrations")
.arg(
Arg::with_name("NUM_MIGRATION")
.long("num")
.short("n")
.help("Number of pending migrations to be rolled back")
.takes_value(true)
.default_value("1"),
.long("num")
.short("n")
.help("Number of pending migrations to be rolled back")
.takes_value(true)
.default_value("1"),
)
.arg(
Arg::with_name("VERSION_MIGRATION")
.long("version")
.short("V")
.help("Version of pending migrations to be rolled back")
.takes_value(true),
),
]
}
22 changes: 16 additions & 6 deletions sea-orm-migration/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,24 @@ where
("up", None) => M::up(db, None).await,
("down", None) => M::down(db, Some(1)).await,
("up", Some(args)) => {
let str = args.value_of("NUM_MIGRATION").unwrap_or_default();
let steps = str.parse().ok();
M::up(db, steps).await
let version = args.value_of("VERSION_MIGRATION");
if version.is_some() {
M::change_to_version(db, version.unwrap()).await
} else {
let str = args.value_of("NUM_MIGRATION").unwrap_or_default();
let steps = str.parse().ok();
M::up(db, steps).await
}
}
("down", Some(args)) => {
let str = args.value_of("NUM_MIGRATION").unwrap();
let steps = str.parse().ok().unwrap_or(1);
M::down(db, Some(steps)).await
let version = args.value_of("VERSION_MIGRATION");
if version.is_some() {
M::change_to_version(db, version.unwrap()).await
} else {
let str = args.value_of("NUM_MIGRATION").unwrap();
let steps = str.parse().ok().unwrap_or(1);
M::down(db, Some(steps)).await
}
}
_ => M::up(db, None).await,
}
Expand Down
34 changes: 34 additions & 0 deletions sea-orm-migration/src/migrator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,40 @@ pub trait MigratorTrait: Send {

Ok(())
}

/// Apply or Rollback migrations to version
async fn change_to_version(db: &DbConn, version: &str) -> Result<(), DbErr> {
let mut steps = Self::get_steps(Self::get_pending_migrations(db).await?, version);
if steps > 0 {
Self::up(db, Some(steps)).await
} else {
let mut migrations = Self::get_applied_migrations(db).await?;
migrations.reverse();
steps = Self::get_steps(migrations, version);
if steps > 1 {
Self::down(db, Some(steps - 1)).await
} else {
Ok(())
}
}
}

fn get_steps(migrations: Vec<Migration>, version: &str) -> u32 {
let mut index = 0;
let mut matched = false;
for Migration { migration, .. } in migrations {
index += 1;
if migration.name() == version {
matched = true;
break;
}
};
if matched {
index
} else {
0
}
}
}

pub(crate) fn query_tables(db: &DbConn) -> SelectStatement {
Expand Down