Module: CaptureMigrationSql

Defined in:
lib/capture_migration_sql/sql_subscriber.rb,
lib/capture_migration_sql.rb,
lib/capture_migration_sql/version.rb,
lib/capture_migration_sql/migration_extension.rb

Overview

Extension methods for ActiveRecord::Migration class.

Defined Under Namespace

Modules: MigrationExtension Classes: SqlSubscriber

Constant Summary collapse

VERSION =
File.read(File.join(__dir__, "..", "..", "VERSION"))

Class Method Summary collapse

Class Method Details

.capture(directory: nil, starting_with: 0) ⇒ Object

Call this method in an initializer to invoke dumping the SQL executed during migrations in to a file.

The directory argument indicates the directory where the files should be stored. If the directory is not specified, the files will be stored in db/migration_sql/.

The starting_with argument can be used to specify which migration you wish to start capturing SQL with. This can be useful if you are adding this gem to an existing project with a history of migrations that you don't want to go back and edit.



17
18
19
20
21
22
23
# File 'lib/capture_migration_sql.rb', line 17

def capture(directory: nil, starting_with: 0)
  unless ::ActiveRecord::Migration.include?(MigrationExtension)
    ::ActiveRecord::Migration.prepend(MigrationExtension)
  end
  @sql_directory = directory || Rails.root + "db" + "migration_sql"
  @starting_with_version = starting_with.to_i
end

.capture_enabled?Boolean

Return true if capturing SQL is enabled for migrations.

Returns:

  • (Boolean)


36
37
38
# File 'lib/capture_migration_sql.rb', line 36

def capture_enabled?
  !!Thread.current[:capture_migration_sql_enabled]
end

.capture_streamObject

Return the stream migration SQL is being written to.



41
42
43
# File 'lib/capture_migration_sql.rb', line 41

def capture_stream
  Thread.current[:capture_migration_sql_stream]
end

.directoryObject

Return the directory set by capture_sql for storing migration SQL.



26
27
28
# File 'lib/capture_migration_sql.rb', line 26

def directory
  @sql_directory if defined?(@sql_directory)
end

.schema_migrations_table_nameObject

Resolve the schema migrations table name so that both the SQL that is written to the file and the statement filter honor custom table names and any table name prefix or suffix. Falls back to "schema_migrations" if the name cannot be determined.



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/capture_migration_sql.rb', line 49

def schema_migrations_table_name
  if defined?(ActiveRecord::SchemaMigration) && ActiveRecord::SchemaMigration.respond_to?(:table_name)
    ActiveRecord::SchemaMigration.table_name
  else
    connection = ActiveRecord::Base.connection
    schema_migration = if connection.respond_to?(:schema_migration)
      connection.schema_migration
    elsif connection.pool.respond_to?(:schema_migration)
      connection.pool.schema_migration
    end
    schema_migration ? schema_migration.table_name : "schema_migrations"
  end
end

.starting_with_versionObject

Return the migration version number to start capturing SQL.



31
32
33
# File 'lib/capture_migration_sql.rb', line 31

def starting_with_version
  @starting_with_version if defined?(@starting_with_version)
end