Search code examples
ruby-on-railsneo4j.rbdatabase-cleaner

Rsepc - Database cleaner with Neo4j.rb 8.0.13


With Old version of neo4j & eno4j.rb everything was working good

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

After Upgrade with a new version of Neo4j & Neo4j.rb I have changed this file

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

This is my database cleaner file , I am using postgres & neo4j in my project I have done this after looking at http://neo4jrb.readthedocs.io/en/8.0.x/Miscellany.html#cleaning-your-database-for-testing I am getting issue when I run test cases

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

Solution

  • I'd suggest just getting rid of DatabaseCleaner entirely. It offers three different forms of database cleaning, but only one really works with Neo4j so there isn't much point (see this page).

    You should be able to do MATCH (n) DETACH DELETE n if you are using 8.1.x of the neo4j gem. Previously if you deleted the whole database the gem would complain that migrations need to be run because the SchemaMigration nodes would have been deleted. In 8.1, that check is now ignored. Before 8.1 you'd want to do something like:

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

    Also, separately, in 8.1 you may want to not do rake neo4j:migrate and instead do rake neo4j:schema:load which loads all of the constraints / indexes instead of running through each migration, which can get slower and slower as you get more migrations. For neo4j:schema:load to work, though, you need to make sure that the db/neo4j/schema.yml file is checked into your repository (that file is re-generated fresh every time you run rake neo4j:migrate so that it's in sync with your migrations)

    I also prefer putting my DELETE statement in a before(:each) rather than an after(:each) so that I can be sure that my tests are running in a fresh state. Otherwise one broken test file which doesn't clean up after itself messes up another test file and it's generally not clear what's going on when that happens.