Trait Migration

Source
pub trait Migration<DB>: Send + Sync {
    // Required methods
    fn app(&self) -> &str;
    fn name(&self) -> &str;
    fn parents(&self) -> Vec<Box<dyn Migration<DB>>>;
    fn operations(&self) -> Vec<Box<dyn Operation<DB>>>;

    // Provided methods
    fn replaces(&self) -> Vec<Box<dyn Migration<DB>>> { ... }
    fn run_before(&self) -> Vec<Box<dyn Migration<DB>>> { ... }
    fn is_atomic(&self) -> bool { ... }
    fn is_virtual(&self) -> bool { ... }
}
Expand description

Trait for defining database migration

A migration represents a set of operations that can be applied to or reverted from a database. Each migration has an associated application name, migration name, and may depend on other migrations.

Migrations can also replace existing migrations, enforce ordering with run before and parents, and control atomicity and virtualization.

Migration trait is implemented for (A,N) where A: AsRef<str>, N: AsRef<str> where A is the app name and N is the name of the migration. You can use migration in this form in parents, replaces and run_before if you cannot reference migration or create migration easily

Required Methods§

Source

fn app(&self) -> &str

Returns the application name associated with the migration. This can be the name of the folder or library where the migration is located.

This value is used in combination with the migration name to uniquely identify a migration.

Source

fn name(&self) -> &str

Returns the migration name, typically the file name without the extension.

This value, together with the application name, is used to uniquely identify a migration and determine equality between migrations.

Source

fn parents(&self) -> Vec<Box<dyn Migration<DB>>>

Returns the list of parent migrations.

Parent migrations must be applied before this migration can be applied. If no parent migrations are required, return an empty vector.

Source

fn operations(&self) -> Vec<Box<dyn Operation<DB>>>

Returns the operations associated with this migration.

A migration can include multiple operations (e.g., create, drop) that are related.

Provided Methods§

Source

fn replaces(&self) -> Vec<Box<dyn Migration<DB>>>

Returns the list of migrations that this migration replaces.

If any of these migrations have been applied, this migration will not be applied. If not, it will either be applied or reverted in place of those migrations.

The default implementation returns an empty vector.

Source

fn run_before(&self) -> Vec<Box<dyn Migration<DB>>>

Returns the list of migrations that this migration must run before(when applying) or after (when reverting).

This can be useful when a migration from another library needs to be applied after this migration or reverted before this migration.

The default implementation returns an empty vector.

Source

fn is_atomic(&self) -> bool

Indicates whether the migration is atomic. By default, this function returns true, meaning the migration is atomic.

If the migration is non-atomic, all its operations will be non-atomic as well. For migrations requiring mixed atomicity, it’s recommended to split them into separate migrations, each handling atomic and non-atomic operations respectively.

Source

fn is_virtual(&self) -> bool

Indicates whether the migration is virtual. By default, this function returns false, meaning the migration is not virtual.

A virtual migration serves as a reference to another migration with the same app and name. If the migration is virtual, all other methods are ignored expect its application name and its own name to check with non virtual migration so such non virtual migration can be used in its place.

Trait Implementations§

Source§

impl<DB> Hash for dyn Migration<DB>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
Source§

impl<DB> PartialEq for dyn Migration<DB>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<DB> Eq for dyn Migration<DB>

Implementations on Foreign Types§

Source§

impl<DB, A, N> Migration<DB> for (A, N)
where A: AsRef<str> + Send + Sync, N: AsRef<str> + Send + Sync,

Source§

fn app(&self) -> &str

Source§

fn name(&self) -> &str

Source§

fn parents(&self) -> Vec<Box<dyn Migration<DB>>>

Source§

fn operations(&self) -> Vec<Box<dyn Operation<DB>>>

Source§

fn is_virtual(&self) -> bool

Implementors§