rlaures
rlaures

Reputation: 335

How to disable ActiveRecord::Migration verbosity?

In short I want to remove the display of the ActiveRecord::Migration.create_table and drop_table functions that I use in an spec file, how can I do that ?

(I answered myself, but I still thinks its a good idea to post it, because there is a lack of documentation on that subject in Rails)

Longuer version:

I'm using Migrations in my Rspec on a Rails application and I wanted to remove the Migration messages.

I do that because I create temporary classes inheriting ApplicationRecord and unsure a library part of my Rails app is working on that kind of elements (in the tests I need to check it works on Hash and ApplicationRecord).

Just for the example, my library computes average on groups of values stored in Records.

In the support of the spec, I add a model_average.rb file containing:

# frozen_string_literal: true

# migration for the model
def model_average_up
  ActiveRecord::Migration.create_table :model_average_data do |t|
    t.integer :v1
    t.float :v2
  end
  ActiveRecord::Migration.create_table :model_average_acc_data do |t|
    t.float :avg_v1
    t.integer :count_v1
    t.float :avg_v2
    t.integer :count_v2
  end
end

def model_average_down
   ActiveRecord::Migration.drop_table :model_average_data
   ActiveRecord::Migration.drop_table :model_average_acc_data
 end

# Test model to compute average on it
class ModelAverageDatum < ApplicationRecord; end

# Test model containing average
class ModelAverageAccData < ApplicationRecord
  def eq?(obj)
    avg_v1 == obj.avg_v1 && count_v1 == obj.count_v1 && avg_v2 == obj.avg_v2 && count_v2 == obj.count_v2
  end
 end

In the spec file of the library, in the before(:context) I call the model_average_up and in the after(:context) I call the model_average_down.

During the rspec execution, it then displays the classic Migration outputs in the middle of the dots or of the documentation format of the test :

-- create_table(:model_average_data)
   -> 0.0059s
-- create_table(:model_average_acc_data)
   -> 0.0044s

I want to remove that output. Is it possible ?

Upvotes: 0

Views: 491

Answers (2)

stephenmurdoch
stephenmurdoch

Reputation: 34603

You can also do ActiveRecord::Migration.verbose = false before running the migrations. That way you avoid needing the block. It's nice to have options.

Upvotes: 1

rlaures
rlaures

Reputation: 335

Yes, it's possible and I found my answer:

Just put you migrations code in a block given in argument of ActiveRecord::Migration.suppress_messages.

In my case :

# migration for the model
def model_average_up
  ActiveRecord::Migration.suppress_messages do
    ActiveRecord::Migration.create_table :model_average_data do |t|
      t.integer :v1
      t.float :v2
    end
    ActiveRecord::Migration.create_table :model_average_acc_data do |t|
      t.float :avg_v1
      t.integer :count_v1
      t.float :avg_v2
      t.integer :count_v2
    end
  end
end

def model_average_down
  ActiveRecord::Migration.suppress_messages do
    ActiveRecord::Migration.drop_table :model_average_data
    ActiveRecord::Migration.drop_table :model_average_acc_data
  end
end

And then it's gone!

Upvotes: 1

Related Questions