pub struct Migrator { /* private fields */ }Implementations§
Source§impl Migrator
impl Migrator
pub fn new(context: MigrationContext) -> Migrator
pub fn add_migration(self, migration: Migration) -> Migrator
pub fn add_migrations(self, migrations: Vec<Migration>) -> Migrator
pub fn get_migrations(&self) -> &Vec<Migration>
pub fn get_pending_migrations(&self) -> Vec<&Migration>
pub fn get_applied_migrations(&self) -> Vec<&Migration>
pub fn latest_version(&self) -> Option<&str>
pub fn find_migration(&self, version: &str) -> Option<&Migration>
Sourcepub fn check_version_conflicts(&self) -> Result<(), DbError>
pub fn check_version_conflicts(&self) -> Result<(), DbError>
检测迁移版本冲突(重复版本号)
返回第一个冲突的版本号(如有)。
在 migrate/up/down 等执行方法入口处调用,确保迁移列表无重复版本。
Sourcepub fn build_create_migrations_table_sql(&self) -> String
pub fn build_create_migrations_table_sql(&self) -> String
P1-4:生成 __migrations 表的 CREATE TABLE IF NOT EXISTS SQL
表结构(跨方言兼容):
CREATE TABLE IF NOT EXISTS __migrations (
version VARCHAR(255) NOT NULL PRIMARY KEY,
name VARCHAR(255),
batch INTEGER NOT NULL,
executed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);不同方言的差异:
- MySQL/PG/SQLite:
TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP - Oracle:
TIMESTAMP DEFAULT CURRENT_TIMESTAMP - SQL Server:
DATETIME DEFAULT GETDATE()
Sourcepub async fn migrate(&mut self) -> Result<Vec<String>, DbError>
pub async fn migrate(&mut self) -> Result<Vec<String>, DbError>
执行所有待迁移(batch=0)的 up SQL
若数据库方言支持 DDL 事务(PostgreSQL/SQLite),则用事务包裹所有待执行迁移, 任一迁移失败时回滚全部变更,避免部分迁移导致的状态不一致。 不支持 DDL 事务的方言(MySQL/Oracle/SQL Server)逐条执行,失败时保留已执行的变更。
P1-4:若连接可用,会自动创建 __migrations 持久化表,并在执行前从表中
同步已执行记录、执行后向表插入新记录。
Sourcepub async fn rollback(&mut self, version: &str) -> Result<(), DbError>
pub async fn rollback(&mut self, version: &str) -> Result<(), DbError>
回滚指定版本(执行 down SQL)
P1-4:若连接可用,会自动创建 __migrations 表,并在回滚后从表中删除记录。
Sourcepub async fn up(
&mut self,
target_version: Option<&str>,
) -> Result<Vec<String>, DbError>
pub async fn up( &mut self, target_version: Option<&str>, ) -> Result<Vec<String>, DbError>
执行到指定版本(包括该版本)
P1-4:若连接可用,会自动创建 __migrations 表,并在执行前从表中
同步已执行记录、执行后向表插入新记录。
Sourcepub async fn down(
&mut self,
target_version: Option<&str>,
) -> Result<Vec<String>, DbError>
pub async fn down( &mut self, target_version: Option<&str>, ) -> Result<Vec<String>, DbError>
回滚到指定版本(执行该版本之后所有迁移的 down SQL)
P1-4:若连接可用,会自动创建 __migrations 表,并在回滚后从表中删除记录。
Sourcepub fn progress(&self) -> MigrationProgress
pub fn progress(&self) -> MigrationProgress
获取迁移进度
Auto Trait Implementations§
impl !RefUnwindSafe for Migrator
impl !UnwindSafe for Migrator
impl Freeze for Migrator
impl Send for Migrator
impl Sync for Migrator
impl Unpin for Migrator
impl UnsafeUnpin for Migrator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more