2

I've written a multi-tenancy gem for Rails.

When I create a new tenant, I load in the schema.rb file. This works fine, except that each time I do it, I get a deluge of log messages:

-- create_table("users", {:force=>true})
NOTICE:  CREATE TABLE will create implicit sequence "users_id_seq" for serial column "users.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "users_pkey" for table "users"
   -> 0.0102s
-- add_index("users", ["email"], {:name=>"index_users_on_email", :unique=>true})
   -> 0.0035s
-- add_index("users", ["reset_password_token"], {:name=>"index_users_on_reset_password_token", :unique=>true})
   -> 0.0040s

Etc etc... all typical of the loading the schema.rb file. My problem is that it's rather annoying to see this during tests. I really don't care to see this and it muddies up my test output, making it much harder for me to debug and verify tests etc...

Does anyone know of a way I can silence this output? I've tried the following in my gem:

Rails.logger.silence{ load("#{Rails.root}/db/seeds.rb") }

but that doesn't change a thing. Does anyone know of a config option or some other way I can silence the output from schema loading?

brad
  • 31,987
  • 28
  • 102
  • 155
  • Here I posted a solution that works with spork: http://stackoverflow.com/questions/7425490/silence-rails-schema-load-for-spork/7633608#7633608 – NilsHaldenwang Oct 03 '11 at 10:39

5 Answers5

4

After some digging, here's what I've learned.

You can silence the log by doing the following:

silence_stream(STDOUT) do
  load "#{Rails.root}/db/schema.rb"
end

silence_stream is a Kernel method, so you should be able to call it and it will kill STDOUT which is where output is being logged to. So the above worked for me.

If you are using Spork, this will not work

So for Spork, you typically load the schema.rb in memory for each run. Unfortunately, Spork gets the output, even before the silence_stream wrapper, and pushes the output through. I discovered this whilst reading a Ruby Inside article which explains this. The article is confusing because he left the code in there, despite it doing nothing for Spork.

BlueFish
  • 5,045
  • 3
  • 26
  • 35
  • worked like a charm, thanks so much! Now if I could just figure out how to silence all of those extra noticies from postgresql I'd be happy. silent-postgres doesn't seem to get them all. – brad Sep 15 '11 at 20:27
  • Hey, sorry BlueFish, in the end, Nils' answer was *exactly* what I was looking for and works with Spork as well so I've got to reward him! Thanks for your help though. – brad Oct 04 '11 at 21:07
  • Hey Brad. No worries... it worked for me too so I have no hard feelings. I was starting to hate the schema logging when using spork. – BlueFish Oct 10 '11 at 22:28
2

The following works for me in Rails 5 (which has deprecated silence_stream). In this example, I'm loading the schema in a specified time so that I can predict the output, and I set ActiveRecord::Schema.verbose to false:

  setup do
    # Normalize time in order to match fixture file
    travel_to Time.zone.parse('2015-03-01T12:00:00') do
      ActiveRecord::Schema.verbose = false
      Rake::Task['db:schema:load'].reenable
      Rake::Task['db:schema:load'].invoke
      Rake::Task['db:fixtures:load'].reenable
      Rake::Task['db:fixtures:load'].invoke
    end
  end
Dan Kohn
  • 33,811
  • 9
  • 84
  • 100
2

Actually you can silence the schema load output when using spork as described here.

Community
  • 1
  • 1
NilsHaldenwang
  • 3,567
  • 3
  • 20
  • 30
1

I found that adding this line min_messages to the database.yml quieted things down nicely:

test:
  adapter: postgresql
  encoding: unicode
  database: myapp_test
  template: template0
  pool: 5
  username: <%= ENV['USERNAME'] %>
  password: <%= ENV['PASSWORD'] %>
  min_messages: WARNING

I also found the template parameter solves the SQL_ASCII encoding problem on Ubuntu PG::Error: ERROR: new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII)

kgf
  • 11
  • 1
1

It's actually ActiveRecord::Migration producing the log output, so for what it's worth the most correct solution would be:

ActiveRecord::Migration.suppress_messages do
  load("schema.rb")
end

See the docs. (See also the "Controlling verbosity" section.)

Mike Campbell
  • 7,921
  • 2
  • 38
  • 51