沉默Rails架构加载

时间:2011-09-09 15:43:25

标签: ruby-on-rails activerecord

我为Rails编写了multi-tenancy gem

当我创建新租户时,我会加载schema.rb文件。这工作正常,除了每次我这样做,我得到了大量的日志消息:

-- 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等...加载schema.rb文件的所有典型代码。我的问题是在测试过程中看到这个很烦人。我真的不在乎看到它并且它使我的测试输出变得混乱,使我更难以调试和验证测试等......

有谁知道我可以让这个输出静音的方法?我在我的宝石中尝试了以下内容:

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

但这不会改变一件事。有没有人知道配置选项或其他方式我可以使模式加载的输出静音?

5 个答案:

答案 0 :(得分:4)

经过一番挖掘,这就是我所学到的。

您可以通过执行以下操作使日志静音:

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

silence_stream是一个内核方法,所以你应该可以调用它,它会杀死STDOUT,这是记录输出的地方。所以上面对我有用。

如果您使用的是Spork,则无法使用

因此对于Spork,您通常会在每次运行时将schema.rb加载到内存中。不幸的是,Spork甚至在silence_stream包装器之前获取输出,并推动输出。我在阅读a Ruby Inside article时发现了这一点,这解释了这一点。这篇文章令人困惑,因为他把代码留在那里,尽管它对Spork没有任何作用。

答案 1 :(得分:2)

实际上,如here所述,使用spork时,您可以使架构加载输出静音。

答案 2 :(得分:1)

我发现将这行min_messages添加到database.yml很平静的事情:

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

我还发现模板参数解决了Ubuntu上的SQL_ASCII编码问题     PG::Error: ERROR: new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII)

答案 3 :(得分:1)

实际上public void applyAll() { final OWLClass clsA = factory.getOWLClass(iri(Motifs.class, "A")); final OWLDataPropertyExpression prop = factory.getOWLDataProperty(Utils.iri(Motifs.class, "prop")); //final OWLDataPropertyExpression equal = factory.getOWLDataProperty(IRI.create("http://www.w3.org/2003/11/swrlb#equal")); final SWRLVariable varX = factory.getSWRLVariable(iri(Motifs.class, "x")); final SWRLVariable varY = factory.getSWRLVariable(iri(Motifs.class, "y")); final OWLLiteral literal = factory.getOWLLiteral(50); final OWLNamedIndividual ind = declareIndividual(iri(Motifs.class, "me")); addAxiom(factory.getOWLDataPropertyAssertionAxiom(prop, ind, literal)); final Set<SWRLAtom> antecedent = new HashSet<>(); antecedent.add(factory.getSWRLDataPropertyAtom(prop, varX, varY)); // antecedent.add(factory.getSWRLDataPropertyAtom(equal, varY, factory.getSWRLLiteralArgument(literal))); final List<SWRLDArgument> args = new ArrayList<>(2); args.add(varY); args.add(factory.getSWRLLiteralArgument(literal)); antecedent.add(factory.getSWRLBuiltInAtom(IRI.create("http://www.w3.org/2003/11/swrlb#equal"), args)); final Set<SWRLAtom> consequences = new HashSet<>(); consequences.add(factory.getSWRLClassAtom(clsA, varX)); addAxiom(factory.getSWRLRule(antecedent, consequences)); final PelletReasoner pellet = PelletReasonerFactory.getInstance().createReasoner(ontology); final NodeSet<OWLClass> x = pellet.getTypes(ind, false); x.getNodes().forEach(node -> System.out.println(ind + " -> " + node.getRepresentativeElement())); System.out.println("------------------------------------"); if (!pellet.isConsistent()) System.out.println("Ontology isn't consistent"); final OWLObjectRenderer renderer = new DLSyntaxObjectRenderer(); for (final OWLAxiom r : ontology.getAxioms()) System.out.println(renderer.render(r)); } 产生了日志输出,所以对于它的价值,最正确的解决方案是:

ActiveRecord::Migration

the docs。 (另请参阅“控制详细程度”部分。)

答案 4 :(得分:1)

以下适用于Rails 5(已弃用silence_stream)。在此示例中,我在指定时间内加载架构以便我可以预测输出,并将ActiveRecord::Schema.verbose设置为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