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§
Sourcefn app(&self) -> &str
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.
Sourcefn name(&self) -> &str
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.
Sourcefn parents(&self) -> Vec<Box<dyn Migration<DB>>>
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.
Sourcefn operations(&self) -> Vec<Box<dyn Operation<DB>>>
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§
Sourcefn replaces(&self) -> Vec<Box<dyn Migration<DB>>>
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.
Sourcefn run_before(&self) -> Vec<Box<dyn Migration<DB>>>
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.
Sourcefn is_atomic(&self) -> bool
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.
Sourcefn is_virtual(&self) -> bool
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.