Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add env var option to prevent wrapping migrations in a transaction #2046

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions sea-orm-migration/src/migrator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use futures::Future;
use std::collections::HashSet;
use std::env;
use std::fmt::Display;
use std::pin::Pin;
use std::time::SystemTime;
Expand Down Expand Up @@ -258,10 +259,16 @@ where

match db.get_database_backend() {
DbBackend::Postgres => {
let transaction = db.begin().await?;
let manager = SchemaManager::new(&transaction);
f(&manager).await?;
transaction.commit().await
let non_atomic = env::var("SEA_ORM_NON_ATOMIC_PG_MIGRATIONS").unwrap_or(String::from("false"));
match non_atomic.as_str() {
"true" => f(&SchemaManager::new(db)).await,
_ =>{
let transaction = db.begin().await?;
let manager = SchemaManager::new(&transaction);
f(&manager).await?;
transaction.commit().await
}
}
}
DbBackend::MySql | DbBackend::Sqlite => {
let manager = SchemaManager::new(db);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let db = manager.get_connection();

db.execute_unprepared(
"CREATE INDEX CONCURRENTLY fruit_name_index ON fruit(name)"
)
.await?;

Ok(())
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let db = manager.get_connection();

db.execute_unprepared(
"DROP INDEX fruit_name_index"
)
.await?;

Ok(())
}
}
1 change: 1 addition & 0 deletions sea-orm-migration/tests/common/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub mod m20220118_000003_seed_cake_table;
pub mod m20220118_000004_create_tea_enum;
pub mod m20220923_000001_seed_cake_table;
pub mod m20230109_000001_seed_cake_table;
pub mod m20230109_000002_create_index_concurrently;
1 change: 1 addition & 0 deletions sea-orm-migration/tests/common/migrator/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ impl MigratorTrait for Migrator {
Box::new(m20220118_000004_create_tea_enum::Migration),
Box::new(m20220923_000001_seed_cake_table::Migration),
Box::new(m20230109_000001_seed_cake_table::Migration),
Box::new(m20230109_000002_create_index_concurrently::Migration),
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ impl MigratorTrait for Migrator {
Box::new(m20220118_000004_create_tea_enum::Migration),
Box::new(m20220923_000001_seed_cake_table::Migration),
Box::new(m20230109_000001_seed_cake_table::Migration),
Box::new(m20230109_000002_create_index_concurrently::Migration),
]
}

Expand Down
10 changes: 7 additions & 3 deletions sea-orm-migration/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ where

println!("\nMigrator::get_pending_migrations");
let migrations = Migrator::get_pending_migrations(db).await?;
assert_eq!(migrations.len(), 5);
assert_eq!(migrations.len(), 6);

let migration = migrations.get(0).unwrap();
assert_eq!(migration.name(), "m20220118_000002_create_fruit_table");
Expand Down Expand Up @@ -158,7 +158,7 @@ where
// Should throw an error
println!("\nMigrator::up");
assert_eq!(
Migrator::up(db, None).await,
Migrator::up(db, Some(6)).await,
Err(DbErr::Migration(
"Abort migration and rollback changes".into()
))
Expand All @@ -176,11 +176,13 @@ where
}

println!("\nMigrator::up");
std::env::set_var("SEA_ORM_NON_ATOMIC_PG_MIGRATIONS", "true");
Migrator::up(db, None).await?;
std::env::remove_var("SEA_ORM_NON_ATOMIC_PG_MIGRATIONS");

println!("\nMigrator::get_applied_migrations");
let migrations = Migrator::get_applied_migrations(db).await?;
assert_eq!(migrations.len(), 6);
assert_eq!(migrations.len(), 7);

assert!(!manager.has_index("cake", "non_existent_index").await?);
assert!(manager.has_index("cake", "cake_name_index").await?);
Expand Down Expand Up @@ -237,13 +239,15 @@ where
assert!(!manager.has_table("fruit").await?);

println!("\nMigrator::fresh");
std::env::set_var("SEA_ORM_NON_ATOMIC_PG_MIGRATIONS", "true");
Migrator::fresh(db).await?;

assert!(manager.has_table("cake").await?);
assert!(manager.has_table("fruit").await?);

println!("\nMigrator::refresh");
Migrator::refresh(db).await?;
std::env::remove_var("SEA_ORM_NON_ATOMIC_PG_MIGRATIONS");

assert!(manager.has_table("cake").await?);
assert!(manager.has_table("fruit").await?);
Expand Down