Reputation: 34206
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.
Upvotes: 29
Views: 20322
Reputation: 6333
I know the OP is asking about doing this programatically in testing, and this answer doesn't really satisfy that scenario. But if you're just looking to quickly delete all your constraints quickly without writing a program, you can generate a list of DROP CONSTRAINT
commands using this query:
CALL db.constraints() YIELD name
RETURN "DROP CONSTRAINT " + name + ";";
Then you can quickly remove the pipes from the output and paste it back into cypher-shell
to drop them all. It would probably be pretty easy to script that up with a shell script if it was something you wanted to do often.
Upvotes: 2
Reputation: 21
First of all, you have to create Neo4j class for connection:
class Neo4jConnection:
def __init__(self, uri, user, pwd):
self.__uri = uri
self.__user = user
self.__pwd = pwd
self.__driver = None
try:
self.__driver = GraphDatabase.driver(self.__uri, auth=(self.__user, self.__pwd))
except Exception as e:
print("Failed to create the driver:", e)
def close(self):
if self.__driver is not None:
self.__driver.close()
def query(self, query, parameters=None, db=None):
assert self.__driver is not None, "Driver not initialized!"
session = None
response = None
try:
session = self.__driver.session(database=db) if db is not None else self.__driver.session()
response = list(session.run(query, parameters))
except Exception as e:
print("Query failed:", e)
finally:
if session is not None:
session.close()
return response
After that create a connection:
uri = 'uri'
pwd = 'pwd'
user= 'user'
conn = Neo4jConnection(uri=uri, user=user , pwd=pwd)
And, you can run the below to delete all constraints:
## drop all constraints
const = conn.query("CALL db.constraints")
for c in const:
conn.query(f"DROP CONSTRAINT {c['name']}")
Upvotes: 1
Reputation: 599
If you use node/javascript you can do something like:
const { records } = await cypher(`CALL db.constraints`)
await Promise.all(records.map(record => {
cypher(`DROP CONSTRAINT ${record.get('name')}`);
}));
Upvotes: 2
Reputation: 4090
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
Upvotes: 0
Reputation: 2802
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.
Upvotes: 7
Reputation: 913
Note using APOC you can drop all indexes and constraints via CALL apoc.schema.assert({}, {})
.
Upvotes: 46
Reputation: 5482
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
Upvotes: 7
Reputation: 39925
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.
Upvotes: 1