neo4j how to drop all constraints

2019-04-03 18:46发布

Is there a cypher command to drop all constraints?

I know I can drop specific constraints.

DROP CONSTRAINT ON (book:Book) ASSERT book.isbn IS UNIQUE

However I want to clear all constraints as part of teardown after testing. Can't find anything in the docs, but something like:

DROP CONSTRAINT *

Update: My testing setup.

Writing a tiny promise-based nodejs cypher client. I want to test defining unique indexes in application code.

标签: neo4j cypher
5条回答
欢心
2楼-- · 2019-04-03 19:19

The only way to drop constraints is doing this on a per-constraint level. You can use e.g. :schema in the Neo4j browser to get a list of all constraints. I'd just write a short script for this.

查看更多
等我变得足够好
3楼-- · 2019-04-03 19:20

Here is how I do this in Python:

    s = connection.get_session()

    # Drop constraints / indices
    for constraint in s.run("CALL db.constraints"):
        s.run("DROP " + constraint[0])

Feels a bit icky, I feel like constraints should be a better supported thing.

查看更多
Rolldiameter
4楼-- · 2019-04-03 19:22

Here is a helper for those using neo4jrb gem:

class MigrationHeper
  include Neo4j::Migrations::Helpers
  def drop_all
    execute("match (n) detach delete n;")
    execute("call db.constraints").each do |constraint|
      execute "drop " + constraint[:description]
    end
  end
end
查看更多
看我几分像从前
5楼-- · 2019-04-03 19:35

You can get a list of all indexes and constraints through GET requests to http://localhost:7474/db/data/schema/constraint/ and http://localhost:7474/db/data/schema/index. Here's how I do it in Ruby, maybe it'll give you an idea of how to do the same in Node.

c.after(:all) do
  conn = Faraday.new(url: "http://localhost:7474")
  response = conn.get('/db/data/schema/constraint/')
  constraints = JSON.parse(response.body)
  constraints.each do |constraint|
    Neo4j::Session.query("DROP CONSTRAINT ON (label:`#{constraint['label']}`) ASSERT label.#{constraint['property_keys'].first} IS UNIQUE")
  end 

  response = conn.get('/db/data/schema/index/')
  indexes = JSON.parse(response.body)
  indexes.each do |index|
    Neo4j::Session.query("DROP INDEX ON :`#{index['label']}`(#{index['property_keys'].first})")
  end
end
查看更多
仙女界的扛把子
6楼-- · 2019-04-03 19:40

Note using APOC you can drop all indexes and constraints via CALL apoc.schema.assert({}, {}).

查看更多
登录 后发表回答