-
-
Notifications
You must be signed in to change notification settings - Fork 520
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
base: master
Are you sure you want to change the base?
Changes from 9 commits
fec23d2
84a8947
c3ac5b2
b5d25a4
6703729
a88cc50
57e5c25
10cbe55
4db7bc8
a894499
962c6b8
2279bd2
7e67974
5d9aa1c
cfc1278
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -289,6 +289,70 @@ pub trait MigratorTrait: Send { | |
|
||
Ok(()) | ||
} | ||
|
||
/// Apply or Rollback migrations to version | ||
async fn change_to_version(db: &DbConn, version: &str) -> Result<(), DbErr> { | ||
Self::up_to_version(db, version).await?; | ||
Self::down_to_version(db, version).await | ||
} | ||
|
||
/// Apply migrations to version | ||
async fn up_to_version(db: &DbConn, version: &str) -> Result<(), DbErr> { | ||
let steps = Self::get_steps(db, MigrationStatus::Pending, version).await?; | ||
if steps > 0 { | ||
Self::up(db, Some(steps)).await | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// Rollback migrations to version | ||
async fn down_to_version(db: &DbConn, version: &str) -> Result<(), DbErr> { | ||
let steps = Self::get_steps(db, MigrationStatus::Applied, version).await?; | ||
if steps > 1 { | ||
Self::down(db, Some(steps - 1)).await | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
async fn get_steps(db: &DbConn, status: MigrationStatus, version: &str) -> Result<u32, DbErr> { | ||
let mut index = 0; | ||
let mut matched = false; | ||
let migrations = Self::get_migration_with_status(db).await?; | ||
match status { | ||
MigrationStatus::Pending => { | ||
for migration in migrations { | ||
if migration.status == MigrationStatus::Pending { | ||
index += 1; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is the case ( #755 ), Migration V1: Applied,Migration V2: Pending,Migration V3: Applied, Migration V4: Applied, get_pending_migrations method has not V3 |
||
if migration.migration.name() == version { | ||
matched = true; | ||
break; | ||
} | ||
} | ||
} | ||
MigrationStatus::Applied => { | ||
for migration in migrations.into_iter().rev() { | ||
if migration.status == MigrationStatus::Applied { | ||
index += 1; | ||
} | ||
if migration.migration.name() == version { | ||
if migration.status == MigrationStatus::Pending { | ||
index += 1; | ||
} | ||
matched = true; | ||
break; | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. Using Consider the case where we have three migrations:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Down to Migration 1, after that Migration 1 is Applied or Pending, In practical application, I think Applied is more reasonable,if Pending, I don't know what the current status is,What about your suggestion? |
||
} | ||
if matched { | ||
Ok(index) | ||
} else { | ||
Ok(0) | ||
} | ||
} | ||
} | ||
|
||
pub(crate) fn query_tables(db: &DbConn) -> SelectStatement { | ||
|
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.
I think this shouldn't perform
up
thendown
. Instead, it should go in one direction only