From c186e20b058aa25db4cf6bc7222bd05c77897514 Mon Sep 17 00:00:00 2001 From: Zeke Gabrielse Date: Tue, 13 Aug 2024 16:20:21 -0500 Subject: [PATCH] extract gem --- README.md | 60 ++++++++++++++++++++------ lib/verbose_migrations.rb | 23 ++++++++++ lib/verbose_migrations/railtie.rb | 8 ++++ spec/spec_helper.rb | 60 +++++++++++++++++++++++--- spec/verbose_migrations.rb | 7 --- spec/verbose_migrations_spec.rb | 71 +++++++++++++++++++++++++++++++ verbose_migrations.gemspec | 2 + 7 files changed, 204 insertions(+), 27 deletions(-) create mode 100644 lib/verbose_migrations/railtie.rb delete mode 100644 spec/verbose_migrations.rb create mode 100644 spec/verbose_migrations_spec.rb diff --git a/README.md b/README.md index 56e73c9..b70a243 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,65 @@ -# VerboseMigrations +# verbose_migrations -TODO: Delete this and the text below, and describe your gem +[![CI](https://github.com/keygen-sh/verbose_migrations/actions/workflows/test.yml/badge.svg)](https://github.com/keygen-sh/verbose_migrations/actions) +[![Gem Version](https://badge.fury.io/rb/verbose_migrations.svg)](https://badge.fury.io/rb/verbose_migrations) -Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/verbose_migrations`. To experiment with that code, run `bin/console` for an interactive prompt. +Override Active Record logger to `DEBUG` mode during Active Record migrations +to easily follow along and spot blocking queries in a migration, even when the +logger is set to e.g. `WARN`. + +This gem was extracted from [Keygen](https://keygen.sh). + +Sponsored by: + + +
+ Keygen +
+
+ +_A fair source software licensing and distribution API._ ## Installation -TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org. +Add this line to your application's `Gemfile`: + +```ruby +gem 'verbose_migrations' +``` -Install the gem and add to the application's Gemfile by executing: +And then execute: - $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG +```bash +$ bundle +``` -If bundler is not being used to manage dependencies, install the gem by executing: +Or install it yourself as: - $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG +```bash +$ gem install verbose_migrations +``` ## Usage -TODO: Write usage instructions here +```ruby -## Development +``` -After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. +## Supported Rubies -To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org). +**`verbose_migrations` supports Ruby 3.1 and above.** We encourage you to upgrade +if you're on an older version. Ruby 3 provides a lot of great features, like +better pattern matching and a new shorthand hash syntax. + +## Is it any good? + +Yes. ## Contributing -Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/verbose_migrations. +If you have an idea, or have discovered a bug, please open an issue or create a +pull request. + +## License + +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). diff --git a/lib/verbose_migrations.rb b/lib/verbose_migrations.rb index f1103ba..1e4cf6e 100644 --- a/lib/verbose_migrations.rb +++ b/lib/verbose_migrations.rb @@ -1,6 +1,29 @@ # frozen_string_literal: true +require 'active_support' +require 'active_record' +require 'logger' + require_relative 'verbose_migrations/version' +require_relative 'verbose_migrations/railtie' module VerboseMigrations + module MigrationExtension + cattr_accessor :verbose_logger, default: nil + cattr_accessor :verbosity, default: nil + + def verbose? = verbosity.present? && verbose_logger.present? + def verbose!(logger: ActiveRecord::Base.logger, level: Logger::DEBUG) + self.verbose_logger = logger + self.verbosity = level + end + + def migrate(...) + verbosity_was, verbose_logger.level = verbose_logger.level, verbosity if verbose? + + super + ensure + verbose_logger.level = verbosity_was if verbose? + end + end end diff --git a/lib/verbose_migrations/railtie.rb b/lib/verbose_migrations/railtie.rb new file mode 100644 index 0000000..ed599e1 --- /dev/null +++ b/lib/verbose_migrations/railtie.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +module VerboseMigrations + ActiveSupport.on_load :active_record do + ActiveRecord::Migration.prepend(MigrationExtension) + end +end + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8bc5dd9..3501a6b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,15 +1,61 @@ # frozen_string_literal: true -require "verbose_migrations" +require 'verbose_migrations' +require 'active_support' +require 'active_record' +require 'rails' +require 'sqlite3' +require 'logger' -RSpec.configure do |config| - # Enable flags like --only-failures and --next-failure - config.example_status_persistence_file_path = ".rspec_status" +ActiveRecord::Base.logger = ActiveSupport::TaggedLogging.new( + ActiveSupport::Logger.new(STDOUT), +) + +ActiveRecord::Base.establish_connection( + adapter: 'sqlite3', + database: ':memory:', +) + +RSpec::Matchers.define_negated_matcher :not_change, :change +RSpec::Matchers.define :transition do |receiver, method_name| + supports_block_expectations + + match do |expectation| + setter_method = receiver.method(:"#{method_name}=") + getter_method = receiver.method(method_name) + initial_state = getter_method.call + + @actual_states = [initial_state] + + allow(receiver).to receive(setter_method.name) do |value| + @actual_states << value + + setter_method.call(value) + end + + expectation.call + + @actual_states == @expected_states + end + + chain :through do |expected_states| + @expected_states = expected_states + end + + failure_message do + "expected block to transition through #{@expected_states} but it transitioned through #{@actual_states}" + end - # Disable RSpec exposing methods globally on `Module` and `main` + failure_message_when_negated do + "expected block not to transition through #{@expected_states} but it did" + end +end + +RSpec.configure do |config| + config.expect_with(:rspec) { _1.syntax = :expect } config.disable_monkey_patching! - config.expect_with :rspec do |c| - c.syntax = :expect + config.around :each, :suppress_migration_messages do |example| + ActiveRecord::Migration.suppress_messages { example.run } end end diff --git a/spec/verbose_migrations.rb b/spec/verbose_migrations.rb deleted file mode 100644 index 42d124d..0000000 --- a/spec/verbose_migrations.rb +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true - -RSpec.describe VerboseMigrations do - it 'has a version number' do - expect(VerboseMigrations::VERSION).not_to be nil - end -end diff --git a/spec/verbose_migrations_spec.rb b/spec/verbose_migrations_spec.rb new file mode 100644 index 0000000..5bc2da2 --- /dev/null +++ b/spec/verbose_migrations_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe VerboseMigrations do + let(:migration) { Class.new(ActiveRecord::Migration[Rails.version[..2]]) { def up = nil } } + let(:logger) { ActiveRecord::Base.logger } + + # FIXME(ezekg) verbosity effects all migrations + before { migration.verbose_logger, migration.verbosity = nil, nil } + before { logger.level = Logger::UNKNOWN } + + describe '.verbose!' do + it 'enables verbose logging' do + expect { migration.verbose! }.to change { migration.verbose? }.from(false).to(true) + end + + it 'enables verbose logging at debug level by default' do + expect { migration.verbose! }.to change { migration.verbosity }.from(nil).to(Logger::DEBUG) + end + + it 'enables verbose logging at custom :level' do + expect { migration.verbose!(level: Logger::INFO) }.to( + change { migration.verbosity }.from(nil).to(Logger::INFO), + ) + end + + it 'enables verbose logging for custom :logger' do + verbose_logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT)) + + expect { migration.verbose!(logger: verbose_logger) }.to( + change { migration.verbose_logger }.from(nil).to(verbose_logger), + ) + end + end + + describe '#migrate', :suppress_migration_messages do + let(:instance) { migration.new } + + it 'enables verbose logging at debug level by default' do + migration.verbose!(logger:) + + expect { instance.migrate(:up) }.to( + transition(logger, :level).through [Logger::UNKNOWN, Logger::DEBUG, Logger::UNKNOWN] + ) + end + + it 'enables verbose logging at custom :level' do + migration.verbose!(level: Logger::INFO) + + expect { instance.migrate(:up) }.to( + transition(logger, :level).through [Logger::UNKNOWN, Logger::INFO, Logger::UNKNOWN] + ) + end + + it 'enables verbose logging for custom :logger' do + verbose_logger = ActiveSupport::TaggedLogging.new(Logger.new(nil)) + verbose_logger.level = Logger::UNKNOWN + + migration.verbose!(logger: verbose_logger) + + expect { migration.new.migrate(:up) }.to( + transition(verbose_logger, :level).through([Logger::UNKNOWN, Logger::DEBUG, Logger::UNKNOWN]).and( + not_change { logger.level }, + ), + ) + + expect(logger.level).to eq Logger::UNKNOWN + end + end +end diff --git a/verbose_migrations.gemspec b/verbose_migrations.gemspec index 6569d16..5a95c79 100644 --- a/verbose_migrations.gemspec +++ b/verbose_migrations.gemspec @@ -19,4 +19,6 @@ Gem::Specification.new do |spec| spec.add_dependency 'rails', '>= 6.0' spec.add_development_dependency 'rspec-rails' + spec.add_development_dependency 'sqlite3', '~> 1.4' + spec.add_development_dependency 'prism' end