Skip to content

Commit

Permalink
extract gem
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Aug 13, 2024
1 parent eb10582 commit c186e20
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 27 deletions.
60 changes: 47 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -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:

<a href="https://keygen.sh?ref=verbose_migrations">
<div>
<img src="https://keygen.sh/images/logo-pill.png" width="200" alt="Keygen">
</div>
</a>

_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).
23 changes: 23 additions & 0 deletions lib/verbose_migrations.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions lib/verbose_migrations/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module VerboseMigrations
ActiveSupport.on_load :active_record do
ActiveRecord::Migration.prepend(MigrationExtension)
end
end

60 changes: 53 additions & 7 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 0 additions & 7 deletions spec/verbose_migrations.rb

This file was deleted.

71 changes: 71 additions & 0 deletions spec/verbose_migrations_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions verbose_migrations.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c186e20

Please sign in to comment.