是否有一个密码命令可以删除所有约束条件?
我知道我可以删除特定的约束条件。
DROP CONSTRAINT ON (book:Book) ASSERT book.isbn IS UNIQUE
然而在测试结束后,我想要清除所有约束作为拆卸的一部分。文档中找不到任何信息,但可以尝试这样:
DROP CONSTRAINT *
更新:我的测试环境。
正在编写一个基于 Promise 的 Node.js Cypher 客户端。我想测试在应用程序代码中定义唯一索引。
注意使用APOC,您可以通过CALL apoc.schema.assert({}, {})
删除所有索引和约束。
s = connection.get_session()
# Drop constraints / indices
for constraint in s.run("CALL db.constraints"):
s.run("DROP " + constraint[0])
感觉有些不舒服,我认为约束应该得到更好的支持。
http://localhost:7474/db/data/schema/constraint/
和http://localhost:7474/db/data/schema/index
发送GET请求来获取所有索引和约束的列表。以下是我在Ruby中的实现方式,也许可以给您在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
我知道楼主在询问如何通过编程方式进行测试,而这个答案并不能完全满足这种情况。但如果你只是想快速删除所有的约束条件而不用编写程序,你可以使用以下查询生成一个DROP CONSTRAINT
命令列表:
CALL db.constraints() YIELD name
RETURN "DROP CONSTRAINT " + name + ";";
cypher-shell
中以全部删除它们。如果您经常需要执行此操作,则可以轻松地使用shell脚本编写脚本。const { records } = await cypher(`CALL db.constraints`)
await Promise.all(records.map(record => {
cypher(`DROP CONSTRAINT ${record.get('name')}`);
}));
DROP CONSTRAINT ${record.get('name')};
,因为如果属性名称需要使用 "`",则按描述删除将无法正常工作。将此约束添加到您的数据库中,并检查您的方法是否有效:CREATE CONSTRAINT ON (p:Person) ASSERT exists(p.\
full name`);`。 - Telmo Trooper:schema
来获取所有约束的列表。我只会为此编写一个简短的脚本。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
首先,您必须创建Neo4j类以进行连接:
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
然后创建一个连接:
uri = 'uri'
pwd = 'pwd'
user= 'user'
conn = Neo4jConnection(uri=uri, user=user , pwd=pwd)
而且,您可以运行以下命令来删除所有约束:
## drop all constraints
const = conn.query("CALL db.constraints")
for c in const:
conn.query(f"DROP CONSTRAINT {c['name']}")
DROP GRAPH
;可编写脚本的轻量级服务器,可以在任何目录下托管图形neo4jlite --serve ./test-graph.db
;还是其他什么?您能描述一下您特定的测试设置吗? - akolleggerDROP GRAPH
但是出现了语法错误。这个命令在哪个 Neo4J / CQL 版本中被支持? - AJcodezDROP GRAPH
对我来说就可以了!现在我在测试运行之间清除数据库。https://github.com/aj0strow/neo4j/blob/master/lib/neo4j.js#L57 - AJcodez