Class: ActiveRecord::Migration::ExecutionStrategy
- Defined in:
- activerecord/lib/active_record/migration/execution_strategy.rb
Overview
Execution Strategy
ExecutionStrategy is the base class for migration execution strategies. A migration execution strategy handles method calls made by migrations that the migration class does not implement directly.
When a migration calls methods like create_table, add_column, or add_index, these calls are delegated to the execution strategy, which determines how they should be executed.
Customizing Migration Execution
You can create custom execution strategies by subclassing ExecutionStrategy (or more commonly, ActiveRecord::Migration::DefaultStrategy) and overriding methods to customize migration behavior. For example:
class MyCustomStrategy < ActiveRecord::Migration::DefaultStrategy
def create_table(table_name, **)
# Custom logic before creating a table
puts "Creating table: #{table_name}"
super
end
def drop_table(table_name, **)
# Custom logic for dropping tables
super
end
end
# Apply globally
config.active_record.migration_strategy = MyCustomStrategy
# Or apply to a specific adapter
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.migration_strategy = MyCustomStrategy
The strategy receives the current migration instance when initialized, accessible via the migration attribute.
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(migration) ⇒ ExecutionStrategy
constructor
A new instance of ExecutionStrategy.
Constructor Details
#initialize(migration) ⇒ ExecutionStrategy
Returns a new instance of ExecutionStrategy.
43 44 45 |
# File 'activerecord/lib/active_record/migration/execution_strategy.rb', line 43 def initialize(migration) @migration = migration end |