Skip to content

Commit 324b2f1

Browse files
shujamukhtar-opsShuja Mukhtar
andauthored
refactor: rewrite the cartesian products (#249)
Co-authored-by: Shuja Mukhtar <[email protected]>
1 parent c60b125 commit 324b2f1

File tree

10 files changed

+36
-17
lines changed

10 files changed

+36
-17
lines changed

internal/repository/neo4j/assignment.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ func (r *AssignmentRepository) Create(ctx context.Context, assignment *model.Ass
6666
assignment.CreatedAt = convert.ToPointer(createdAt)
6767

6868
cypher := `
69-
MATCH (u:` + assignment.User.Label() + ` {id: $user_id}), (r:` + assignment.Resource.Label() + ` {id: $resource_id})
69+
MATCH (u:` + assignment.User.Label() + ` {id: $user_id})
70+
MATCH (r:` + assignment.Resource.Label() + ` {id: $resource_id})
7071
MERGE (u)-[a:` + EdgeKindAssignedTo.String() + ` {kind: $kind}]->(r)
7172
ON CREATE SET a.id = $id, a.created_at = datetime($created_at)`
7273

internal/repository/neo4j/attachment.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ func (r *AttachmentRepository) Create(ctx context.Context, belongsTo model.ID, a
6565
attachment.UpdatedAt = nil
6666

6767
cypher := `
68-
MATCH (b:` + belongsTo.Label() + ` {id: $belong_to_id}), (o:` + attachment.CreatedBy.Label() + ` {id: $created_by_id})
68+
MATCH (b:` + belongsTo.Label() + ` {id: $belong_to_id})
69+
MATCH (o:` + attachment.CreatedBy.Label() + ` {id: $created_by_id})
6970
CREATE
7071
(a:` + attachment.ID.Label() + ` {
7172
id: $id, name: $name, file_id: $file_id, created_by: $created_by_id, created_at: datetime($created_at)

internal/repository/neo4j/comment.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ func (r *CommentRepository) Create(ctx context.Context, belongsTo model.ID, comm
6565
comment.UpdatedAt = nil
6666

6767
cypher := `
68-
MATCH (b:` + belongsTo.Label() + ` {id: $belong_to_id}), (o:` + comment.CreatedBy.Label() + ` {id: $created_by_id})
68+
MATCH (b:` + belongsTo.Label() + ` {id: $belong_to_id})
69+
MATCH (o:` + comment.CreatedBy.Label() + ` {id: $created_by_id})
6970
CREATE
7071
(c:` + comment.ID.Label() + ` {id: $id, content: $content, created_by: $created_by_id, created_at: datetime($created_at)}),
7172
(b)-[:` + EdgeKindHasComment.String() + ` {id: $has_comment_rel_id, created_at: datetime($created_at)}]->(c),

internal/repository/neo4j/document.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ func (r *DocumentRepository) Create(ctx context.Context, belongsTo model.ID, doc
7777
document.UpdatedAt = nil
7878

7979
cypher := `
80-
MATCH (b:` + belongsTo.Label() + ` {id: $belong_to_id}), (o:` + document.CreatedBy.Label() + ` {id: $created_by_id})
80+
MATCH (b:` + belongsTo.Label() + ` {id: $belong_to_id})
81+
MATCH (o:` + document.CreatedBy.Label() + ` {id: $created_by_id})
8182
CREATE
8283
(d:` + document.ID.Label() + ` {
8384
id: $id, name: $name, excerpt: $excerpt, file_id: $file_id, created_by: $created_by_id,

internal/repository/neo4j/issue.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ func (r *IssueRepository) Create(ctx context.Context, project model.ID, issue *m
144144
issue.UpdatedAt = nil
145145

146146
cypher := `
147-
MATCH (p:` + project.Label() + ` {id: $project_id}), (u:` + issue.ReportedBy.Label() + ` {id: $reported_by_id})
147+
MATCH (p:` + project.Label() + ` {id: $project_id})
148+
MATCH (u:` + issue.ReportedBy.Label() + ` {id: $reported_by_id})
148149
CREATE
149150
(i:` + issue.ID.Label() + ` {
150151
id: $id, numeric_id: $numeric_id, kind: $kind, title: $title, description: $description, status: $status,
@@ -358,7 +359,8 @@ func (r *IssueRepository) AddWatcher(ctx context.Context, issue model.ID, user m
358359
}
359360

360361
cypher := `
361-
MATCH (i:` + issue.Label() + ` {id: $issue_id}), (u:` + user.Label() + ` {id: $user_id})
362+
MATCH (i:` + issue.Label() + ` {id: $issue_id})
363+
MATCH (u:` + user.Label() + ` {id: $user_id})
362364
CREATE (u)-[:` + EdgeKindWatches.String() + ` {id: $rel_id, created_at: datetime($created_at)}]->(i)`
363365

364366
params := map[string]any{
@@ -444,7 +446,8 @@ func (r *IssueRepository) AddRelation(ctx context.Context, relation *model.Issue
444446
relation.UpdatedAt = nil
445447

446448
cypher := `
447-
MATCH (s:` + relation.Source.Label() + ` {id: $source_id}), (t:` + relation.Target.Label() + ` {id: $target_id})
449+
MATCH (s:` + relation.Source.Label() + ` {id: $source_id})
450+
MATCH (t:` + relation.Target.Label() + ` {id: $target_id})
448451
MERGE (s)-[r:` + EdgeKindRelatedTo.String() + ` {kind: $kind}]->(t)
449452
ON CREATE SET r.id = $id, r.created_at = datetime($created_at)
450453
`

internal/repository/neo4j/label.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ func (r *LabelRepository) AttachTo(ctx context.Context, labelID, attachTo model.
144144
}
145145

146146
cypher := `
147-
MATCH (l:` + labelID.Label() + ` {id: $label_id}), (n:` + attachTo.Label() + ` {id: $node_id})
147+
MATCH (l:` + labelID.Label() + ` {id: $label_id})
148+
MATCH (n:` + attachTo.Label() + ` {id: $node_id})
148149
CREATE (n)-[:` + EdgeKindHasLabel.String() + `]->(l)`
149150

150151
params := map[string]any{

internal/repository/neo4j/organization.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ func (r *OrganizationRepository) AddMember(ctx context.Context, orgID, memberID
186186
}
187187

188188
cypher := `
189-
MATCH (o:` + orgID.Label() + ` {id: $org_id}), (u:` + memberID.Label() + ` {id: $member_id})
189+
MATCH (o:` + orgID.Label() + ` {id: $org_id})
190+
MATCH (u:` + memberID.Label() + ` {id: $member_id})
190191
MERGE (u)-[m:` + EdgeKindMemberOf.String() + `]->(o)
191192
ON CREATE SET m.created_at = datetime($now), m.id = $membership_id
192193
ON MATCH SET m.updated_at = datetime($now)`

internal/repository/neo4j/permission.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ func (r *PermissionRepository) Create(ctx context.Context, perm *model.Permissio
6868
perm.UpdatedAt = nil
6969

7070
cypher := `
71-
MATCH (subject:` + perm.Subject.Label() + ` {id: $subject}), (target:` + perm.Target.Label() + ` {id: $target})
71+
MATCH (subject:` + perm.Subject.Label() + ` {id: $subject})
72+
MATCH (target:` + perm.Target.Label() + ` {id: $target})
7273
MERGE (subject)-[p:` + EdgeKindHasPermission.String() + ` {id: $id, kind: $kind}]->(target) ON CREATE SET p.created_at = datetime($created_at)
7374
`
7475

internal/repository/neo4j/role.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ func (r *RoleRepository) Create(ctx context.Context, createdBy, belongsTo model.
6666
role.UpdatedAt = nil
6767

6868
cypher := `
69-
MATCH (u:` + createdBy.Label() + ` {id: $owner_id}), (b:` + belongsTo.Label() + ` {id: $belongs_to_id})
69+
MATCH (u:` + createdBy.Label() + ` {id: $owner_id})
70+
MATCH (b:` + belongsTo.Label() + ` {id: $belongs_to_id})
7071
MERGE (r:` + role.ID.Label() + ` {id: $role_id})
7172
ON CREATE SET r += { name: $name, description: $description, created_at: datetime($created_at) }
7273
CREATE (r)<-[:` + EdgeKindHasTeam.String() + ` { id: $has_team_id, created_at: datetime($created_at) }]-(b)
@@ -99,7 +100,8 @@ func (r *RoleRepository) Get(ctx context.Context, id, belongsTo model.ID) (*mode
99100
defer span.End()
100101

101102
cypher := `
102-
MATCH (r:` + id.Label() + ` {id: $id}), (b:` + belongsTo.Label() + ` {id: $belongs_to_id})
103+
MATCH (r:` + id.Label() + ` {id: $id})
104+
MATCH (b:` + belongsTo.Label() + ` {id: $belongs_to_id})
103105
OPTIONAL MATCH (r)<-[:` + EdgeKindMemberOf.String() + `]-(u:` + model.ResourceTypeUser.String() + `)
104106
OPTIONAL MATCH (r)-[p:` + EdgeKindHasPermission.String() + `]->()
105107
RETURN r, collect(DISTINCT u.id) AS m, collect(DISTINCT p.id) AS p
@@ -153,7 +155,8 @@ func (r *RoleRepository) Update(ctx context.Context, id, belongsTo model.ID, pat
153155
defer span.End()
154156

155157
cypher := `
156-
MATCH (r:` + id.Label() + ` {id: $id}), (b:` + belongsTo.Label() + ` {id: $belongs_to_id})
158+
MATCH (r:` + id.Label() + ` {id: $id})
159+
MATCH (b:` + belongsTo.Label() + ` {id: $belongs_to_id})
157160
SET r += $patch, r.updated_at = datetime()
158161
WITH r
159162
OPTIONAL MATCH (r)<-[:` + EdgeKindMemberOf.String() + `]-(u:` + model.ResourceTypeUser.String() + `)
@@ -179,7 +182,9 @@ func (r *RoleRepository) AddMember(ctx context.Context, roleID, memberID, belong
179182
defer span.End()
180183

181184
cypher := `
182-
MATCH (r:` + roleID.Label() + ` {id: $role_id}), (u:` + memberID.Label() + ` {id: $member_id}), (b:` + belongsToID.Label() + ` {id: $belongs_to_id})
185+
MATCH (r:` + roleID.Label() + ` {id: $role_id})
186+
MATCH (u:` + memberID.Label() + ` {id: $member_id})
187+
MATCH (b:` + belongsToID.Label() + ` {id: $belongs_to_id})
183188
MERGE (u)-[m:` + EdgeKindMemberOf.String() + `]->(r)
184189
ON CREATE SET m.created_at = datetime($now), m.id = $membership_id
185190
ON MATCH SET m.updated_at = datetime($now)`
@@ -204,7 +209,8 @@ func (r *RoleRepository) RemoveMember(ctx context.Context, roleID, memberID, bel
204209
defer span.End()
205210

206211
cypher := `
207-
MATCH (:` + roleID.Label() + ` {id: $role_id})<-[r:` + EdgeKindMemberOf.String() + `]-(:` + memberID.Label() + ` {id: $member_id}), (b:` + belongsToID.Label() + ` {id: $belongs_to_id})
212+
MATCH (:` + roleID.Label() + ` {id: $role_id})<-[r:` + EdgeKindMemberOf.String() + `]-(:` + memberID.Label() + ` {id: $member_id})
213+
MATCH (b:` + belongsToID.Label() + ` {id: $belongs_to_id})
208214
DELETE r`
209215

210216
params := map[string]any{
@@ -224,7 +230,9 @@ func (r *RoleRepository) Delete(ctx context.Context, id, belongsTo model.ID) err
224230
ctx, span := r.tracer.Start(ctx, "repository.neo4j.RoleRepository/Delete")
225231
defer span.End()
226232

227-
cypher := `MATCH (r:` + id.Label() + ` {id: $id}), (b:` + belongsTo.Label() + ` {id: $belongs_to_id}) DETACH DELETE r`
233+
cypher := `MATCH (r:` + id.Label() + ` {id: $id})
234+
MATCH (b:` + belongsTo.Label() + ` {id: $belongs_to_id})
235+
DETACH DELETE r`
228236
params := map[string]any{
229237
"id": id.String(),
230238
"belongs_to_id": belongsTo.String(),

internal/repository/neo4j/todo.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ func (r *TodoRepository) Create(ctx context.Context, todo *model.Todo) error {
6767
todo.UpdatedAt = nil
6868

6969
cypher := `
70-
MATCH (o:` + todo.OwnedBy.Label() + ` {id: $owner_id}), (c:` + todo.CreatedBy.Label() + ` {id: $creator_id})
70+
MATCH (o:` + todo.OwnedBy.Label() + ` {id: $owner_id})
71+
MATCH (c:` + todo.CreatedBy.Label() + ` {id: $creator_id})
7172
CREATE
7273
(t:` + todo.ID.Label() + ` {
7374
id: $id, title: $title, description: $description, priority: $priority, completed: $completed,

0 commit comments

Comments
 (0)