Skip to main content

MigrationEngine

Struct MigrationEngine 

Source
pub struct MigrationEngine { /* private fields */ }
Expand description

The migration engine — compares old and new model snapshots to generate migration SQL.

Implementations§

Source§

impl MigrationEngine

Source

pub fn new(dialect: MigrationDialect) -> Self

Source

pub fn generate( &self, name: &str, current: &[EntityTypeMeta], previous_snapshot: &Option<ModelSnapshot>, ) -> EFResult<Migration>

Generates a migration by diffing the current model against a snapshot.

Source

pub fn create_snapshot( &self, migration_id: &str, entity_types: &[EntityTypeMeta], ) -> ModelSnapshot

Creates a snapshot from current entity type metadata.

Source§

impl MigrationEngine

Source

pub async fn ensure_history_table( &self, provider: &dyn IDatabaseProvider, ) -> EFResult<()>

Ensures the migration history table exists.

Source

pub async fn apply( &self, provider: &dyn IDatabaseProvider, migration: &Migration, ) -> EFResult<()>

Applies a migration’s Up SQL against a database provider.

Source

pub async fn revert( &self, provider: &dyn IDatabaseProvider, migration: &Migration, ) -> EFResult<()>

Reverts a migration using its Down SQL.

Source

pub async fn get_applied_migrations( &self, provider: &dyn IDatabaseProvider, ) -> EFResult<Vec<MigrationHistoryEntry>>

Returns migrations already recorded in the database history table.

Source

pub async fn is_applied( &self, provider: &dyn IDatabaseProvider, migration_id: &str, ) -> EFResult<bool>

Returns whether a migration id is already recorded in history.

Source

pub async fn apply_pending( &self, provider: &dyn IDatabaseProvider, migrations: &[Migration], ) -> EFResult<usize>

Applies all pending migrations in order, skipping already-applied ids.

Source

pub async fn revert_last( &self, provider: &dyn IDatabaseProvider, migrations: &[Migration], ) -> EFResult<Option<String>>

Reverts the most recently applied migration from the given list.

Source

pub async fn revert_to_target( &self, provider: &dyn IDatabaseProvider, migrations: &[Migration], target: Option<&str>, ) -> EFResult<Vec<String>>

Reverts all migrations applied strictly after target (exclusive), leaving the database at target’s state. Returns the reverted ids in the order they were reverted (most-recent first).

If target is None, reverts ALL applied migrations. Returns an error if target is not currently applied.

Source

pub fn generate_script( migrations: &[Migration], from: Option<&str>, to: Option<&str>, ) -> EFResult<String>

Generates a combined SQL script transitioning the database from the from migration state to the to migration state.

Semantics (mirroring dotnet ef migrations script):

  • from is exclusive: the DB is assumed to already be at from.
  • to is inclusive: the script brings the DB up to and including to.
  • from = None: start from an empty database (before any migration).
  • to = None: end at the latest migration.
  • When from precedes to: emits Up SQL for migrations in (from, to].
  • When from follows to: emits Down SQL for migrations in (to, from].
  • When from == to: emits a no-op comment.
Source

pub async fn ensure_created( &self, provider: &dyn IDatabaseProvider, entity_types: &[EntityTypeMeta], ) -> EFResult<()>

Generates and applies the initial schema for the given entity types. Corresponds to EF Core Database.EnsureCreated(). Does not use the migrations history table (idempotent DDL only).

Source

pub async fn ensure_deleted( &self, provider: &dyn IDatabaseProvider, entity_types: &[EntityTypeMeta], ) -> EFResult<()>

Drops all tables for the given entity types. Corresponds to EF Core Database.EnsureDeleted().

Source

pub async fn apply_seed_data( &self, provider: &dyn IDatabaseProvider, meta: &EntityTypeMeta, rows: &[HashMap<String, DbValue>], ) -> EFResult<()>

Inserts seed data configured via ModelBuilder::has_data.

Source§

impl MigrationEngine

Source

pub fn foreign_key_name( table: &str, column: &str, referenced_table: &str, ) -> String

Standard foreign-key constraint name used by Add/DropForeignKey.

Source

pub fn index_name(table: &str, column: &str) -> String

Standard index name used by CreateIndex/DropIndex.

Source

pub fn generate_alter_column_sql( &self, table: &str, column_name: &str, new: &SnapshotColumn, ) -> String

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, 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, 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.