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.
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
Note using APOC you can drop all indexes and constraints via CALL apoc.schema.assert({}, {})
.
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.
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.
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