【问题标题】:Rsepc - Database cleaner with Neo4j.rb 8.0.13Rspec - 使用 Neo4j.rb 8.0.13 的数据库清理器
【发布时间】:2017-05-20 12:02:55
【问题描述】:

使用旧版本的 neo4j 和 eno4j.rb 一切正常

RSpec.configure do |config|
  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
    DatabaseCleaner[:neo4j, connection: { type: :server_db, path: ENV['TEST_GRAPHENEDB_URL'] }].strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner[:neo4j, connection: { type: :server_db, path: ENV['TEST_GRAPHENEDB_URL'] }].strategy = :transaction
  end

  config.before(:each, js: true) do
    DatabaseCleaner.strategy = :truncation
    DatabaseCleaner[:neo4j, connection: { type: :server_db, path: ENV['TEST_GRAPHENEDB_URL'] }].strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
    DatabaseCleaner[:neo4j, connection: { type: :server_db, path: ENV['TEST_GRAPHENEDB_URL'] }].start
  end

  config.after(:each) do
    DatabaseCleaner.clean
    DatabaseCleaner[:neo4j, connection: { type: :server_db, path: ENV['TEST_GRAPHENEDB_URL'] }].clean
  end
end

使用新版本的 Neo4j 和 Neo4j.rb 升级后,我更改了此文件

RSpec.configure do |config|
  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
    Neo4j::ActiveBase.current_session.query('MATCH (n) DETACH DELETE n')
  end
end

这是我的数据库清理文件,我在我的项目中使用 postgres 和 neo4j 我在查看后完成了这个 http://neo4jrb.readthedocs.io/en/8.0.x/Miscellany.html#cleaning-your-database-for-testing 运行测试用例时遇到问题

 Neo4j::PendingMigrationError:
   Migrations are pending:
   20170324201940
   20170324202013
   20170324202025
   20170324202040
   20170324202053
   20170324202110
   20170324202522
   20170324202604
   20170324202801
   20170328203203

【问题讨论】:

    标签: ruby-on-rails neo4j.rb database-cleaner


    【解决方案1】:

    我建议完全摆脱DatabaseCleaner。它提供了三种不同形式的数据库清理,但只有一种真正适用于 Neo4j,因此没有多大意义(参见 this page)。

    如果您使用的是 neo4j gem 的 8.1.x,您应该可以使用 MATCH (n) DETACH DELETE n。以前,如果您删除了整个数据库,gem 会抱怨需要运行迁移,因为 SchemaMigration 节点将被删除。在 8.1 中,该检查现在被忽略。在 8.1 之前,您需要执行以下操作:

    MATCH (n) WHERE NOT n:`Neo4j::Migrations::SchemaMigration` DETACH DELETE n
    

    另外,另外,在 8.1 中,您可能不想执行 rake neo4j:migrate 而是执行 rake neo4j:schema:load 加载所有约束/索引而不是运行每个迁移,随着迁移次数的增加,迁移速度会越来越慢.但是,要使neo4j:schema:load 工作,您需要确保将db/neo4j/schema.yml 文件签入到您的存储库中(每次运行rake neo4j:migrate 时都会重新生成该文件,以便它与您的迁移同步)

    我也更喜欢将我的DELETE 语句放在before(:each) 而不是after(:each) 中,这样我就可以确保我的测试以全新的状态运行。否则,一个自行清理的损坏的测试文件会弄乱另一个测试文件,并且通常不清楚发生这种情况时发生了什么。

    【讨论】:

      猜你喜欢
      • 2014-08-23
      • 2014-02-14
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      • 2015-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多