Skip to main content

Migrator

Struct Migrator 

Source
pub struct Migrator { /* private fields */ }

Implementations§

Source§

impl Migrator

Source

pub fn new(context: MigrationContext) -> Migrator

Source

pub fn add_migration(self, migration: Migration) -> Migrator

Source

pub fn add_migrations(self, migrations: Vec<Migration>) -> Migrator

Source

pub fn get_migrations(&self) -> &Vec<Migration>

Source

pub fn get_pending_migrations(&self) -> Vec<&Migration>

Source

pub fn get_applied_migrations(&self) -> Vec<&Migration>

Source

pub fn latest_version(&self) -> Option<&str>

Source

pub fn find_migration(&self, version: &str) -> Option<&Migration>

Source

pub fn check_version_conflicts(&self) -> Result<(), DbError>

检测迁移版本冲突(重复版本号)

返回第一个冲突的版本号(如有)。 在 migrate/up/down 等执行方法入口处调用,确保迁移列表无重复版本。

Source

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()
Source

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 持久化表,并在执行前从表中 同步已执行记录、执行后向表插入新记录。

Source

pub async fn rollback(&mut self, version: &str) -> Result<(), DbError>

回滚指定版本(执行 down SQL)

P1-4:若连接可用,会自动创建 __migrations 表,并在回滚后从表中删除记录。

Source

pub async fn up( &mut self, target_version: Option<&str>, ) -> Result<Vec<String>, DbError>

执行到指定版本(包括该版本)

P1-4:若连接可用,会自动创建 __migrations 表,并在执行前从表中 同步已执行记录、执行后向表插入新记录。

Source

pub async fn down( &mut self, target_version: Option<&str>, ) -> Result<Vec<String>, DbError>

回滚到指定版本(执行该版本之后所有迁移的 down SQL)

P1-4:若连接可用,会自动创建 __migrations 表,并在回滚后从表中删除记录。

Source

pub async fn reset(&mut self) -> Result<Vec<String>, DbError>

重置:回滚所有已执行的迁移,然后重新执行

Source

pub async fn refresh(&mut self) -> Result<Vec<String>, DbError>

刷新:回滚所有已执行的迁移,然后重新执行

Source

pub fn progress(&self) -> MigrationProgress

获取迁移进度

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more