Module: Vitess::Activerecord::Migration
- Defined in:
- lib/vitess/activerecord/migration.rb,
lib/vitess/activerecord/migration/version.rb
Defined Under Namespace
Classes: Cancelled, Error, Failed, TimedOut
Constant Summary collapse
- VERSION =
"0.3.0"
Instance Method Summary collapse
-
#create_table(table_name, **options) ⇒ Object
Override the create_table method.
-
#default_ddl_strategy ⇒ Object
Returns the default DDL strategy.
-
#exec_migration(connection, direction) ⇒ Object
Override exec_migration to set the default DDL strategy to vitess.
-
#migration_log_columns ⇒ Object
Returns the columns of SHOW VITESS_MIGRATIONS to log during the run.
-
#raise_on_timeout ⇒ Object
Returns whether an error will be raised when the DDL statement exceeds wait_timeout_seconds.
-
#wait_timeout_seconds ⇒ Object
Returns the timeout seconds for waiting for the completion of the DDL statement.
-
#with_ddl_strategy(strategy) ⇒ Object
Temporarily change the DDL strategy within the block.
Instance Method Details
#create_table(table_name, **options) ⇒ Object
Override the create_table method. If using the Vitess strategy, wait for the completion of the CREATE TABLE statement. This prevents an evaluation error from occurring if you try to execute a DDL on that table before the creation is complete.
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/vitess/activerecord/migration.rb', line 73 def create_table(table_name, **) super(table_name, **) # If create_table is called during revert, no additional processing is done. # We expect the DROP statement to be issued automatically during revert, but if execute is run here, # it will raise an IrreversibleMigration error, so this is to prevent that. return if down_migration_in_change_method? # If not using the Vitess strategy, do not wait for the completion of the CREATE TABLE statement. return unless vitess_strategy? wait_for_ddl end |
#default_ddl_strategy ⇒ Object
Returns the default DDL strategy. This method is called and set before executing the change, up, or down methods.
If you want to use a different strategy like direct, override this method.
18 19 20 |
# File 'lib/vitess/activerecord/migration.rb', line 18 def default_ddl_strategy "vitess" end |
#exec_migration(connection, direction) ⇒ Object
Override exec_migration to set the default DDL strategy to vitess. This method is called every time a migration is executed. If you want to use a different DDL strategy, call with_ddl_strategy inside the change method or elsewhere.
61 62 63 64 65 66 67 68 |
# File 'lib/vitess/activerecord/migration.rb', line 61 def exec_migration(connection, direction) @migration_direction = direction @using_change_method = self.respond_to?(:change) @migration_context = "#{self.version}_#{self.class.name.underscore}" with_ddl_strategy default_ddl_strategy do super(connection, direction) end end |
#migration_log_columns ⇒ Object
Returns the columns of SHOW VITESS_MIGRATIONS to log during the run.
Override this method if you want to change the default columns.
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/vitess/activerecord/migration.rb', line 44 def migration_log_columns %w[ migration_uuid migration_statement added_timestamp started_timestamp last_throttled_timestamp last_cutover_attempt_timestamp is_immediate_operation progress eta_seconds ] end |
#raise_on_timeout ⇒ Object
Returns whether an error will be raised when the DDL statement exceeds wait_timeout_seconds.
Override this method if you want to change how timeouts are handled.
37 38 39 |
# File 'lib/vitess/activerecord/migration.rb', line 37 def raise_on_timeout true end |
#wait_timeout_seconds ⇒ Object
Returns the timeout seconds for waiting for the completion of the DDL statement. If the DDL exceeds the timeout:
- A warning will be logged.
- An error will be raised and halt the Rails migration if
raise_on_timeoutistrue. - The Vitess migration will continue.
Override this method if you want to change the default timeout.
30 31 32 |
# File 'lib/vitess/activerecord/migration.rb', line 30 def wait_timeout_seconds 7200 # 120 minutes end |
#with_ddl_strategy(strategy) ⇒ Object
Temporarily change the DDL strategy within the block.
You can use this method inside the change method to change the strategy only during the execution of specific DDL statements. However, note that this makes the migration irreversible, so if it’s possible to handle this by overriding the default_ddl_strategy, use that instead.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/vitess/activerecord/migration.rb', line 91 def with_ddl_strategy(strategy) if enable_vitess? original_ddl_strategy = execute("SELECT @@ddl_strategy").first.first execute("SET @@ddl_strategy='#{strategy}'") execute("SET @@migration_context='#{@migration_context}'") unless strategy == "direct" begin yield wait_for_ddl unless strategy == "direct" ensure execute("SET @@ddl_strategy='#{original_ddl_strategy}'") end else yield end end |