Module: CaptureMigrationSql::MigrationExtension

Defined in:
lib/capture_migration_sql/migration_extension.rb

Instance Method Summary collapse

Instance Method Details

#disable_sql_logging(&block) ⇒ Object

Disable SQL logging. You can use this method to turn off logging SQL when the migration is munging data that may vary between environments.



16
17
18
# File 'lib/capture_migration_sql/migration_extension.rb', line 16

def disable_sql_logging(&block)
  sql_logging(enabled: false, &block)
end

#enable_sql_logging(&block) ⇒ Object

Enable SQL logging. You can call this method within a block where SQL logging was disabled to renable it.



22
23
24
# File 'lib/capture_migration_sql/migration_extension.rb', line 22

def enable_sql_logging(&block)
  sql_logging(enabled: true, &block)
end

#exec_migration(conn, direction) ⇒ Object

Monkey patch to public but internal ActiveRecord method to pass a connection that will log SQL statements.



8
9
10
11
12
# File 'lib/capture_migration_sql/migration_extension.rb', line 8

def exec_migration(conn, direction)
  log_migration_sql(direction) do
    super(conn, direction)
  end
end

#using_connection(connection_or_class, label: nil, &block) ⇒ Object

Use a different database connection for the block. You can use this if your application has multiple databases to swap connections for the migration. You can pass in either a database connection or an ActiveRecord::Base class to use the connection used by that class.

The label argument will be added to the logged SQL as a comment.



32
33
34
35
36
37
38
39
40
41
# File 'lib/capture_migration_sql/migration_extension.rb', line 32

def using_connection(connection_or_class, label: nil, &block)
  if connection_or_class.is_a?(Class) && connection_or_class < ActiveRecord::Base
    label ||= connection_or_class.name
    connection_or_class.connection_pool.with_connection do |connection|
      switch_connection_in_block(connection, label: label, &block)
    end
  else
    switch_connection_in_block(connection_or_class, label: label, &block)
  end
end