Conversation
+ Add MigrationTrait::use_transaction() (None/Some(true)/Some(false)) and SchemaManager::begin()/commit() for manual control + Change up()/down() from single batch transaction to per-migration + Add DatabaseExecutor::OwnedTransaction variant and is_transaction() method
sinder38
reviewed
Mar 4, 2026
| } | ||
|
|
||
| async fn insert_migration_record<C: ConnectionTrait>( | ||
| db: &C, |
Contributor
There was a problem hiding this comment.
this Is very minor but why not:
async fn insert_migration_record(db: &impl ConnectionTrait, ...)
Member
Author
There was a problem hiding this comment.
thanks for the suggestion, a long time ago the convention is to use where T. we can have another PR to migrate all of them in the codebase where it makes sense
🎉 Released In 2.0.0-rc.36 🎉Huge thanks for the contribution! |
Contributor
|
@tyt2y3 Yeah... but It does wrap on drop even when I've fn use_transaction(&self) -> Option<bool> {
Some(false)
}use sea_orm_migration::{prelude::*, sea_orm::Statement};
#[derive(DeriveMigrationName)]
pub struct DatabaseMigration;
#[async_trait::async_trait]
impl MigrationTrait for DatabaseMigration {
fn use_transaction(&self) -> Option<bool> {
Some(false)
}
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let db = manager.get_connection();
db.execute_unprepared("CREATE DATABASE app;").await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let db = manager.get_connection();
// 1. Terminate all active connections to the target database
let disconnect_query = format!(
"SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '{}' AND pid <> pg_backend_pid();",
"app"
);
db.execute_unprepared(&disconnect_query).await?;
// db.execute_raw(Statement::from_string(db.get_database_backend(), "DROP DATABASE app;")).await?;
db.execute_unprepared("DROP DATABASE IF EXISTS app;").await?;
Ok(())
}
} |
This was referenced Apr 23, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add
MigrationTrait::use_transaction()to control whether individual migrationsrun inside a transaction, replacing the previous all-or-nothing batch transaction
behavior on Postgres.
Changes
Core (
sea-orm)OwnedTransaction(DatabaseTransaction)variant toDatabaseExecutorenum,enabling
SchemaManagerto own a transactionDatabaseExecutor::is_transaction()methodIntoDatabaseExecutorimpl for ownedDatabaseTransactionMigration (
sea-orm-migration)MigrationTrait::use_transaction() -> Option<bool>:None(default): follow backend convention (Postgres = txn, MySQL/SQLite = no txn)Some(true): force transaction on any backendSome(false): disable automatic transactionSchemaManager::begin()returning an owned transactionexec_up_with/exec_down_withto wrap each migration individuallybased on
use_transaction()+ backend, instead of batching all migrationsTests
run_transaction_testcovering all threeuse_transactionmodes plusfailure-with-txn (DDL rolled back) and failure-without-txn (DDL persists)
async-stdtotokioExample Usage