From 73a8e5f2b9f40835affff69621ade2d1e9a03229 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Mon, 16 Mar 2020 12:16:31 +0100 Subject: [PATCH] change how links work --- src/wanijo/infrastructure/repl.clj | 45 +++++++++++++++++++++--------- src/wanijo/instance/db.clj | 18 ++++-------- src/wanijo/link/db.clj | 28 +++++++++---------- src/wanijo/main.clj | 3 +- src/wanijo/visualisation/db.clj | 2 +- 5 files changed, 53 insertions(+), 43 deletions(-) diff --git a/src/wanijo/infrastructure/repl.clj b/src/wanijo/infrastructure/repl.clj index ac4634c..6e7a3cb 100644 --- a/src/wanijo/infrastructure/repl.clj +++ b/src/wanijo/infrastructure/repl.clj @@ -1,9 +1,9 @@ (ns wanijo.infrastructure.repl (:require [buddy.hashers :as hashers] - [neo4j-clj.core :as db] + [wanijo.main :as main] [wanijo.infrastructure.neo4j :as neo4j])) -(db/defquery create-user +(neo4j/defquery create-user "CREATE (n:user) SET n.ident = {ident} SET n.pw = {pw} @@ -16,7 +16,7 @@ :pw (hashers/derive pw) :uuid (neo4j/uuid)})) -(db/defquery merge-config +(neo4j/defquery merge-config "MERGE (c:dbconfig) ON CREATE SET c.installed_at = {now}, @@ -26,38 +26,38 @@ c.last_migration = {now}, c.db_version = {version}") -(db/defquery config +(neo4j/defquery config "MATCH (c:dbconfig) RETURN c") -(db/defquery ver-0-schema-uuid +(neo4j/defquery ver-0-schema-uuid "CREATE CONSTRAINT ON (n:schema) ASSERT n.uuid IS UNIQUE") -(db/defquery ver-0-attribute-uuid +(neo4j/defquery ver-0-attribute-uuid "CREATE CONSTRAINT ON (n:attribute) ASSERT n.uuid IS UNIQUE") -(db/defquery ver-0-instance-uuid +(neo4j/defquery ver-0-instance-uuid "CREATE CONSTRAINT ON (n:instance) ASSERT n.uuid IS UNIQUE") -(db/defquery ver-0-property-uuid +(neo4j/defquery ver-0-property-uuid "CREATE CONSTRAINT ON (n:property) ASSERT n.uuid IS UNIQUE") -(db/defquery ver-0-user-uuid +(neo4j/defquery ver-0-user-uuid "CREATE CONSTRAINT ON (n:user) ASSERT n.uuid IS UNIQUE") -(db/defquery ver-0-link-uuid +(neo4j/defquery ver-0-link-uuid "CREATE CONSTRAINT ON (n:link) ASSERT n.uuid IS UNIQUE") -(db/defquery ver-1-tag-name +(neo4j/defquery ver-1-tag-name "CREATE CONSTRAINT ON (n:tag) ASSERT n.name IS UNIQUE") -(db/defquery ver-1-tag-uuid +(neo4j/defquery ver-1-tag-uuid "CREATE CONSTRAINT ON (n:tag) ASSERT n.uuid IS UNIQUE") @@ -73,9 +73,23 @@ (neo4j/exec-query! ver-1-tag-name {}) (neo4j/exec-query! ver-1-tag-uuid {})) +(neo4j/defquery migrate-links + "MATCH (i1:instance)-[:link_from]-(l:link)-[:link_to]-(i2:instance), + (l)-[cb:created_by]->(:user) + MERGE (i1)-[newl:link]->(i2) + ON CREATE SET + newl.name = l.name, + newl.created_at = l.created_at, + newl.uuid = l.uuid + DELETE cb, lf, lt, l") + +(defn init-version-2 [] + (neo4j/exec-query! migrate-links {})) + (def migrations [init-version-0 - init-version-1]) + init-version-1 + init-version-2]) (defn run-migrations! [] (neo4j/exec-query! merge-config {:now (neo4j/now-str) @@ -93,3 +107,8 @@ (neo4j/exec-query! merge-config {:now (neo4j/now-str) :version (count migrations)}) nil) + +(defn dev-server! [] + (main/stop-server!) + (main/start-server-dev!) + (println "Startet server at http://localhost:8080")) diff --git a/src/wanijo/instance/db.clj b/src/wanijo/instance/db.clj index 6dba364..f9ba9bf 100644 --- a/src/wanijo/instance/db.clj +++ b/src/wanijo/instance/db.clj @@ -141,18 +141,12 @@ OPTIONAL MATCH (p:property)-[pc:of]->(i), (p)-[pac:of]->(a:attribute) - OPTIONAL MATCH - (i)<-[lt1:link_to]-(ltn:link)-[lt2:link_from]->(), - (ltn)-[ltcb:created_by]->() - OPTIONAL MATCH - (i)<-[lf1:link_from]-(lfn:link)-[lf2:link_to]->(), - (lfn)-[lfcb:created_by]->() OPTIONAL MATCH (i)-[tw:tagged_with]->() + OPTIONAL MATCH + (i)-[l:link]-() DELETE pac, pc, cb, ic, p, - lt1, lt2, ltn, ltcb, - lf1, lf2, lfn, lfcb, - tw, i") + l, tw, i") (defn delete! [uuid] (neo4j/exec-query! delete {:uuid uuid})) @@ -171,8 +165,7 @@ (neo4j/defquery outgoing-links "MATCH (inst:instance {uuid:{uuid}}), - (inst)<-[:link_from]-(link:link), - (link)-[:link_to]->(target:instance), + (inst)-[link:link]->(target:instance), (target)-[:of]->(schema:schema) OPTIONAL MATCH (target)-[:tagged_with]->(tag:tag) @@ -183,8 +176,7 @@ (neo4j/defquery incoming-links "MATCH (inst:instance {uuid:{uuid}}), - (inst)<-[:link_to]-(link:link), - (link)-[:link_from]->(source:instance), + (inst)<-[link:link]-(source:instance), (source)-[:of]->(schema:schema) OPTIONAL MATCH (source)-[:tagged_with]->(tag:tag) diff --git a/src/wanijo/link/db.clj b/src/wanijo/link/db.clj index a9ae1de..9b5ca2a 100644 --- a/src/wanijo/link/db.clj +++ b/src/wanijo/link/db.clj @@ -10,30 +10,28 @@ (neo4j/defquery create "MATCH (i:instance {uuid:{from}}), - (u:user {uuid:{by}}), (t:instance {uuid:{target}}) - CREATE - (l:link {uuid:{uuid}})-[:created_by]->(u) - SET l.created_at = {created_at}, - l.name = {name} - CREATE - (i)<-[:link_from]-(l)-[:link_to]->(t)") -(defn create! [link] + CREATE (i)-[l:link]->(t) + SET l.created_at = {created_at}, + l.name = {name}, + l.created_by = {by}, + l.uuid = {uuid}") +(defn create! [{:keys [from name to by]}] (let [tuples (map (fn [target-uuid] [create - {:from (:from link) - :by (:by link) + {:from from + :by by :target target-uuid :uuid (neo4j/uuid) :created_at (neo4j/now-str) - :name (:name link)}]) - (:to link))] + :name name}]) + to)] (apply neo4j/exec-queries! tuples))) (neo4j/defquery delete - "MATCH (l:link {uuid:{uuid}}), - (l)-[r]-() - DELETE r, l") + "MATCH (:instance)-[l:link]-(:instance) + WHERE l.uuid = {uuid} + DELETE l") (defn delete! [uuid] (neo4j/exec-query! delete {:uuid uuid})) diff --git a/src/wanijo/main.clj b/src/wanijo/main.clj index 0ae1a40..494190c 100644 --- a/src/wanijo/main.clj +++ b/src/wanijo/main.clj @@ -20,7 +20,8 @@ :handler wanijo-handler/dev-app)) (defn stop-server! [] - (.stop @server)) + (when @server + (.stop @server))) (defn -main [& args] (start-server! :port (Integer/valueOf (or (System/getenv "port") diff --git a/src/wanijo/visualisation/db.clj b/src/wanijo/visualisation/db.clj index 514812c..ccf0cfd 100644 --- a/src/wanijo/visualisation/db.clj +++ b/src/wanijo/visualisation/db.clj @@ -19,7 +19,7 @@ "MATCH (source:instance)-[:of]->(schema:schema) OPTIONAL MATCH - (source)<-[:link_from]-(link:link)-[:link_to]->(target:instance), + (source)-[link:link]->(target:instance), (target)-[:of]->(target_schema:schema) RETURN source, schema, link, target, target_schema") (defn all-instance-connections! []