From a701724eac45cde1aa1033a2d11cf6418b9176db Mon Sep 17 00:00:00 2001 From: Rob Lineweaver Date: Thu, 11 Aug 2022 13:40:52 -0400 Subject: [PATCH 1/3] add configurable boolean wrap_migrations_in_transaction to control whether to wrap task execution within a database transaction. --- lib/rake/task_migration.rb | 3 +++ lib/rake/task_migration/migrator.rb | 10 +++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/rake/task_migration.rb b/lib/rake/task_migration.rb index c0d2e3e..0c1eaf2 100644 --- a/lib/rake/task_migration.rb +++ b/lib/rake/task_migration.rb @@ -16,6 +16,9 @@ module TaskMigration mattr_accessor :migration_namespace self.migration_namespace = DEFAULT_NAMESPACE + mattr_accessor :wrap_migrations_in_transaction + self.wrap_migrations_in_transaction = true + class << self def config yield self diff --git a/lib/rake/task_migration/migrator.rb b/lib/rake/task_migration/migrator.rb index aa6731e..1fdaf60 100644 --- a/lib/rake/task_migration/migrator.rb +++ b/lib/rake/task_migration/migrator.rb @@ -28,7 +28,7 @@ def migrate_task(task) announce "#{task}: migrating" - ActiveRecord::Base.transaction do + with_optional_transaction_wrapper do time = Benchmark.measure do invoke(task) end @@ -44,6 +44,14 @@ def migrate_task(task) end end + def with_optional_transaction_wrapper + if Rake::TaskMigration.wrap_migrations_in_transaction + ActiveRecord::Base.transaction { yield } + else + yield + end + end + def invoke(task) Rake::Task[task].invoke end From 9cf3beb18b5e48768e9676ed34e7cb9411ac69c8 Mon Sep 17 00:00:00 2001 From: Rob Lineweaver Date: Thu, 11 Aug 2022 13:53:27 -0400 Subject: [PATCH 2/3] update documentation with new configuration option --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index acceb3c..e6717de 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ Use an initializer file for configuration. - `migration_table_name (default = 'rake_task_migrations')` - `migration_namespace (default = :migrations)` - +- `wrap_migrations_in_transaction (default = true)` #### Example: ```ruby @@ -69,6 +69,7 @@ Use an initializer file for configuration. Rake::TaskMigration.config do |config| config.migration_table_name = 'table_name' config.migration_namespace = 'namespace' + config.wrap_migrations_in_transaction = false end ``` From a491dfdba91f8c91763d5f22201f3febd5fccf22 Mon Sep 17 00:00:00 2001 From: Rob Lineweaver Date: Thu, 11 Aug 2022 13:56:37 -0400 Subject: [PATCH 3/3] restore empty line --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e6717de..6fdfcc6 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ Use an initializer file for configuration. - `migration_table_name (default = 'rake_task_migrations')` - `migration_namespace (default = :migrations)` - `wrap_migrations_in_transaction (default = true)` + #### Example: ```ruby