pub struct MigrationEngine { /* private fields */ }Expand description
The migration engine — compares old and new model snapshots to generate migration SQL.
Implementations§
Source§impl MigrationEngine
impl MigrationEngine
pub fn new(dialect: MigrationDialect) -> Self
Sourcepub fn generate(
&self,
name: &str,
current: &[EntityTypeMeta],
previous_snapshot: &Option<ModelSnapshot>,
) -> EFResult<Migration>
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.
Sourcepub fn create_snapshot(
&self,
migration_id: &str,
entity_types: &[EntityTypeMeta],
) -> ModelSnapshot
pub fn create_snapshot( &self, migration_id: &str, entity_types: &[EntityTypeMeta], ) -> ModelSnapshot
Creates a snapshot from current entity type metadata.
Source§impl MigrationEngine
impl MigrationEngine
Sourcepub async fn ensure_history_table(
&self,
provider: &dyn IDatabaseProvider,
) -> EFResult<()>
pub async fn ensure_history_table( &self, provider: &dyn IDatabaseProvider, ) -> EFResult<()>
Ensures the migration history table exists.
Sourcepub async fn apply(
&self,
provider: &dyn IDatabaseProvider,
migration: &Migration,
) -> EFResult<()>
pub async fn apply( &self, provider: &dyn IDatabaseProvider, migration: &Migration, ) -> EFResult<()>
Applies a migration’s Up SQL against a database provider.
Sourcepub async fn revert(
&self,
provider: &dyn IDatabaseProvider,
migration: &Migration,
) -> EFResult<()>
pub async fn revert( &self, provider: &dyn IDatabaseProvider, migration: &Migration, ) -> EFResult<()>
Reverts a migration using its Down SQL.
Sourcepub async fn get_applied_migrations(
&self,
provider: &dyn IDatabaseProvider,
) -> EFResult<Vec<MigrationHistoryEntry>>
pub async fn get_applied_migrations( &self, provider: &dyn IDatabaseProvider, ) -> EFResult<Vec<MigrationHistoryEntry>>
Returns migrations already recorded in the database history table.
Sourcepub async fn is_applied(
&self,
provider: &dyn IDatabaseProvider,
migration_id: &str,
) -> EFResult<bool>
pub async fn is_applied( &self, provider: &dyn IDatabaseProvider, migration_id: &str, ) -> EFResult<bool>
Returns whether a migration id is already recorded in history.
Sourcepub async fn apply_pending(
&self,
provider: &dyn IDatabaseProvider,
migrations: &[Migration],
) -> EFResult<usize>
pub async fn apply_pending( &self, provider: &dyn IDatabaseProvider, migrations: &[Migration], ) -> EFResult<usize>
Applies all pending migrations in order, skipping already-applied ids.
Sourcepub async fn revert_last(
&self,
provider: &dyn IDatabaseProvider,
migrations: &[Migration],
) -> EFResult<Option<String>>
pub async fn revert_last( &self, provider: &dyn IDatabaseProvider, migrations: &[Migration], ) -> EFResult<Option<String>>
Reverts the most recently applied migration from the given list.
Sourcepub async fn revert_to_target(
&self,
provider: &dyn IDatabaseProvider,
migrations: &[Migration],
target: Option<&str>,
) -> EFResult<Vec<String>>
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.
Sourcepub fn generate_script(
migrations: &[Migration],
from: Option<&str>,
to: Option<&str>,
) -> EFResult<String>
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):
fromis exclusive: the DB is assumed to already be atfrom.tois inclusive: the script brings the DB up to and includingto.from = None: start from an empty database (before any migration).to = None: end at the latest migration.- When
fromprecedesto: emits Up SQL for migrations in(from, to]. - When
fromfollowsto: emits Down SQL for migrations in(to, from]. - When
from == to: emits a no-op comment.
Sourcepub async fn ensure_created(
&self,
provider: &dyn IDatabaseProvider,
entity_types: &[EntityTypeMeta],
) -> EFResult<()>
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).
Sourcepub async fn ensure_deleted(
&self,
provider: &dyn IDatabaseProvider,
entity_types: &[EntityTypeMeta],
) -> EFResult<()>
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().
Sourcepub async fn apply_seed_data(
&self,
provider: &dyn IDatabaseProvider,
meta: &EntityTypeMeta,
rows: &[HashMap<String, DbValue>],
) -> EFResult<()>
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
impl MigrationEngine
Sourcepub fn foreign_key_name(
table: &str,
column: &str,
referenced_table: &str,
) -> String
pub fn foreign_key_name( table: &str, column: &str, referenced_table: &str, ) -> String
Standard foreign-key constraint name used by Add/DropForeignKey.
Sourcepub fn index_name(table: &str, column: &str) -> String
pub fn index_name(table: &str, column: &str) -> String
Standard index name used by CreateIndex/DropIndex.