module CaptureMigrationSql
Extension methods for ActiveRecord::Migration class.
Subscriber that is attached to ActiveRecord and will handle writing migration SQL to the output stream.
Constants
- VERSION
Public Class Methods
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.
# File lib/capture_migration_sql.rb, line 18 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
Return true if capturing SQL is enabled for migrations.
# File lib/capture_migration_sql.rb, line 37 def capture_enabled? !!Thread.current[:capture_migration_sql_enabled] end
Return the strema migration SQL is being written to.
# File lib/capture_migration_sql.rb, line 42 def capture_stream Thread.current[:capture_migration_sql_stream] end
Return the directory set by `capture_sql` for storing migration SQL.
# File lib/capture_migration_sql.rb, line 27 def directory @sql_directory if defined?(@sql_directory) end
Return the migration version number to start capaturing SQL.
# File lib/capture_migration_sql.rb, line 32 def starting_with_version @starting_with_version if defined?(@starting_with_version) end