-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathQueries.hs
More file actions
606 lines (584 loc) · 26.2 KB
/
Copy pathQueries.hs
File metadata and controls
606 lines (584 loc) · 26.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeOperators #-}
module Share.Postgres.Contributions.Queries
( createContribution,
contributionByProjectIdAndNumber,
shareContributionByProjectIdAndNumber,
listContributionsByProjectId,
contributionById,
updateContribution,
insertContributionStatusChangeEvent,
contributionStatusChangeEventsByContributionId,
listContributionsByUserId,
shareContributionsByBranchOf,
getPagedShareContributionTimelineByProjectIdAndNumber,
performMergesAndBCAUpdatesFromBranchPush,
rebaseContributionsFromMergedBranches,
contributionStateTokenById,
existsPrecomputedNamespaceDiff,
getPrecomputedNamespaceDiff,
savePrecomputedNamespaceDiff,
contributionsRelatedToBranches,
)
where
import Control.Lens
import Data.List qualified as List
import Data.Map qualified as Map
import Data.Set qualified as Set
import Data.Time (UTCTime)
import Safe (lastMay)
import Share.Codebase.Types (CodebaseEnv (..))
import Share.Contribution (Contribution (..), ContributionStatus (..))
import Share.IDs
import Share.Notifications.Queries qualified as NotifQ
import Share.Notifications.Types (NotificationEvent (..), NotificationEventData (..), ProjectContributionData (..))
import Share.Postgres qualified as PG
import Share.Postgres.Comments.Queries (commentsByTicketOrContribution)
import Share.Postgres.IDs
import Share.Prelude
import Share.Utils.API
import Share.Web.Errors
import Share.Web.Share.Contributions.API (ContributionTimelineCursor, ListContributionsCursor)
import Share.Web.Share.Contributions.Types
createContribution ::
-- | Author
UserId ->
ProjectId ->
-- | Title
Text ->
-- | Description
Maybe Text ->
ContributionStatus ->
-- | Source Branch
BranchId ->
-- | Target Branch
BranchId ->
PG.Transaction e (ContributionId, ContributionNumber)
createContribution authorId projectId title description status sourceBranchId targetBranchId = do
(contributionId, number) <-
PG.queryExpect1Row
[PG.sql|
WITH contrib_number AS (
SELECT (COALESCE(MAX(contribution_number), 0) + 1) AS new
FROM contributions contribution
WHERE contribution.project_id = #{projectId}
)
INSERT INTO contributions(
author_id,
project_id,
title,
description,
status,
source_branch,
target_branch,
contribution_number,
best_common_ancestor_causal_id
)
SELECT #{authorId}, #{projectId}, #{title}, #{description}, #{status}, #{sourceBranchId}, #{targetBranchId}, contrib_number.new, best_common_causal_ancestor(source_branch.causal_id, target_branch.causal_id)
FROM contrib_number
JOIN project_branches AS source_branch ON source_branch.id = #{sourceBranchId}
JOIN project_branches AS target_branch ON target_branch.id = #{targetBranchId}
RETURNING contributions.id, contributions.contribution_number
|]
insertContributionStatusChangeEvent contributionId authorId Nothing status
let contributionEventData =
ProjectContributionData
{ projectId,
contributionId,
fromBranchId = sourceBranchId,
toBranchId = targetBranchId,
contributorUserId = authorId
}
(projectResourceId, projectOwnerUserId) <-
PG.queryExpect1Row
[PG.sql|
SELECT p.resource_id, p.owner_user_id FROM projects p
WHERE p.id = #{projectId}
|]
let notifEvent =
NotificationEvent
{ eventId = (),
eventOccurredAt = (),
eventResourceId = projectResourceId,
eventData = ProjectContributionCreatedData contributionEventData,
eventScope = projectOwnerUserId
}
NotifQ.recordEvent notifEvent
pure (contributionId, number)
contributionByProjectIdAndNumber ::
ProjectId ->
ContributionNumber ->
PG.Transaction e (Maybe Contribution)
contributionByProjectIdAndNumber projectId contributionNumber = do
PG.query1Row @Contribution
[PG.sql|
SELECT
contribution.id,
contribution.project_id,
contribution.contribution_number,
contribution.title,
contribution.description,
contribution.status,
contribution.source_branch,
contribution.target_branch,
contribution.best_common_ancestor_causal_id,
contribution.created_at,
contribution.updated_at,
contribution.author_id
FROM contributions contribution
WHERE contribution.project_id = #{projectId}
AND contribution.contribution_number = #{contributionNumber}
|]
shareContributionByProjectIdAndNumber ::
ProjectId ->
ContributionNumber ->
PG.Transaction e (Maybe (ShareContribution UserId))
shareContributionByProjectIdAndNumber projectId contributionNumber = do
PG.query1Row
[PG.sql|
SELECT
contribution.id,
contribution.contribution_number,
project_owner.handle,
project.slug,
contribution.title,
contribution.description,
contribution.status,
source_branch.name,
source_branch_contributor.handle,
target_branch.name,
target_branch_contributor.handle,
contribution.created_at,
contribution.updated_at,
contribution.author_id,
(SELECT COUNT(*) FROM comments comment WHERE comment.contribution_id = contribution.id AND comment.deleted_at IS NULL) as num_comments
FROM contributions AS contribution
JOIN projects AS project ON project.id = contribution.project_id
JOIN users AS project_owner ON project_owner.id = project.owner_user_id
JOIN project_branches AS source_branch ON source_branch.id = contribution.source_branch
LEFT JOIN users AS source_branch_contributor ON source_branch_contributor.id = source_branch.contributor_id
JOIN project_branches AS target_branch ON target_branch.id = contribution.target_branch
LEFT JOIN users AS target_branch_contributor ON target_branch_contributor.id = target_branch.contributor_id
WHERE contribution.project_id = #{projectId}
AND contribution_number = #{contributionNumber}
|]
-- | Lists all contributions for a project which match the provided filters.
-- Most recently updated first.
listContributionsByProjectId ::
ProjectId ->
Limit ->
Maybe (Cursor ListContributionsCursor) ->
Maybe UserId ->
Maybe ContributionStatus ->
Maybe ContributionKindFilter ->
PG.Transaction e (Maybe (Cursor ListContributionsCursor), [ShareContribution UserId])
listContributionsByProjectId projectId limit mayCursor mayUserFilter mayStatusFilter mayKindFilter = do
let kindFilter = case mayKindFilter of
Nothing -> "true"
Just kind -> case kind of
AllContributionKinds -> mempty
OnlyCoreContributions -> [PG.sql| source_branch.contributor_id IS NULL |]
OnlyContributorContributions -> [PG.sql| source_branch.contributor_id IS NOT NULL |]
let cursorFilter = case mayCursor of
Nothing -> "true"
Just (Cursor (beforeTime, contributionId)) ->
[PG.sql|
(contribution.updated_at, contribution.id) < (#{beforeTime}, #{contributionId})
|]
addCursor
<$> PG.queryListRows @(ShareContribution UserId)
[PG.sql|
SELECT
contribution.id,
contribution.contribution_number,
project_owner.handle,
project.slug,
contribution.title,
contribution.description,
contribution.status,
source_branch.name,
source_branch_contributor.handle,
target_branch.name,
target_branch_contributor.handle,
contribution.created_at,
contribution.updated_at,
contribution.author_id,
(SELECT COUNT(*) FROM comments comment WHERE comment.contribution_id = contribution.id AND comment.deleted_at IS NULL) as num_comments
FROM contributions AS contribution
JOIN projects AS project ON project.id = contribution.project_id
JOIN users AS project_owner ON project_owner.id = project.owner_user_id
JOIN project_branches AS source_branch ON source_branch.id = contribution.source_branch
LEFT JOIN users AS source_branch_contributor ON source_branch_contributor.id = source_branch.contributor_id
JOIN project_branches AS target_branch ON target_branch.id = contribution.target_branch
LEFT JOIN users AS target_branch_contributor ON target_branch_contributor.id = target_branch.contributor_id
WHERE
^{kindFilter}
AND ^{cursorFilter}
AND project.id = #{projectId}
AND (#{mayUserFilter} IS NULL OR contribution.author_id = #{mayUserFilter})
AND (#{mayStatusFilter} IS NULL OR contribution.status = #{mayStatusFilter})
ORDER BY contribution.updated_at DESC, contribution.id DESC
LIMIT #{limit}
|]
where
addCursor :: [ShareContribution UserId] -> (Maybe (Cursor ListContributionsCursor), [ShareContribution UserId])
addCursor xs =
( lastMay xs <&> \(ShareContribution {updatedAt, contributionId}) ->
Cursor (updatedAt, contributionId),
xs
)
contributionById :: (PG.QueryM m) => ContributionId -> m Contribution
contributionById contributionId = do
PG.queryExpect1Row @Contribution
[PG.sql|
SELECT
contribution.id,
contribution.project_id,
contribution.contribution_number,
contribution.title,
contribution.description,
contribution.status,
contribution.source_branch,
contribution.target_branch,
contribution.best_common_ancestor_causal_id,
contribution.created_at,
contribution.updated_at,
contribution.author_id
FROM contributions AS contribution
WHERE contribution.id = #{contributionId}
|]
updateContribution :: UserId -> ContributionId -> Maybe Text -> NullableUpdate Text -> Maybe ContributionStatus -> Maybe BranchId -> Maybe BranchId -> PG.Transaction e Bool
updateContribution callerUserId contributionId newTitle newDescription newStatus newSourceBranchId newTargetBranchId = do
isJust <$> runMaybeT do
Contribution {..} <- lift $ contributionById contributionId
let updatedTitle = fromMaybe title newTitle
let updatedDescription = fromNullableUpdate description newDescription
let updatedStatus = fromMaybe status newStatus
let updatedSourceBranchId = fromMaybe sourceBranchId newSourceBranchId
let updatedTargetBranchId = fromMaybe targetBranchId newTargetBranchId
-- Add a status change event
when (isJust newStatus && newStatus /= Just status) do
lift $ insertContributionStatusChangeEvent contributionId callerUserId (Just status) updatedStatus
lift $
PG.execute_
[PG.sql|
UPDATE contributions
SET
title = #{updatedTitle},
description = #{updatedDescription},
status = #{updatedStatus},
source_branch = #{updatedSourceBranchId},
target_branch = #{updatedTargetBranchId}
WHERE id = #{contributionId}
|]
insertContributionStatusChangeEvent :: ContributionId -> UserId -> Maybe ContributionStatus -> ContributionStatus -> PG.Transaction e ()
insertContributionStatusChangeEvent contributionId actorUserId oldStatus newStatus = do
PG.execute_
[PG.sql|
INSERT INTO contribution_status_events
(contribution_id, actor, old_status, new_status)
VALUES (#{contributionId}, #{actorUserId}, #{oldStatus}, #{newStatus})
|]
contributionStatusChangeEventsByContributionId :: ContributionId -> Maybe UTCTime -> Maybe UTCTime -> PG.Transaction e [StatusChangeEvent UserId]
contributionStatusChangeEventsByContributionId contributionId mayFromExclusive untilInclusive = do
PG.queryListRows
[PG.sql|
SELECT
event.old_status,
event.new_status,
event.actor,
event.created_at
FROM contribution_status_events AS event
WHERE contribution_id = #{contributionId}
AND (#{mayFromExclusive} IS NULL OR event.created_at > #{mayFromExclusive})
AND (#{untilInclusive} IS NULL OR event.created_at <= #{untilInclusive})
ORDER BY event.created_at ASC
|]
listContributionsByUserId ::
Maybe UserId ->
UserId ->
Limit ->
Maybe (Cursor (UTCTime, ContributionId)) ->
Maybe ContributionStatus ->
Maybe ContributionKindFilter ->
PG.Transaction e (Maybe (Cursor (UTCTime, ContributionId)), [ShareContribution UserId])
listContributionsByUserId callerUserId userId limit mayCursor mayStatusFilter mayKindFilter = do
let kindFilter = case mayKindFilter of
Nothing -> "true"
Just kind -> case kind of
AllContributionKinds -> mempty
OnlyCoreContributions -> [PG.sql| source_branch.contributor_id IS NULL |]
OnlyContributorContributions -> [PG.sql| source_branch.contributor_id IS NOT NULL |]
let cursorFilter = case mayCursor of
Nothing -> "true"
Just (Cursor (beforeTime, contributionId)) ->
[PG.sql|
(contribution.updated_at, contribution.id) < (#{beforeTime}, #{contributionId})
|]
addCursor
<$> PG.queryListRows @(ShareContribution UserId)
[PG.sql|
SELECT
contribution.id,
contribution.contribution_number,
project_owner.handle,
project.slug,
contribution.title,
contribution.description,
contribution.status,
source_branch.name,
source_branch_contributor.handle,
target_branch.name,
target_branch_contributor.handle,
contribution.created_at,
contribution.updated_at,
contribution.author_id,
(SELECT COUNT(*) FROM comments comment WHERE comment.contribution_id = contribution.id AND comment.deleted_at IS NULL) as num_comments
FROM contributions AS contribution
JOIN projects AS project ON project.id = contribution.project_id
WHERE
contribution.author_id = #{userId}
AND NOT project.private
OR EXISTS (
SELECT FROM accessible_private_projects ap
WHERE
ap.user_id = #{callerUserId}
AND ap.project_id = project.id
)
AND (#{mayStatusFilter} IS NULL OR contribution.status = #{mayStatusFilter})
AND ^{cursorFilter}
AND ^{kindFilter}
ORDER BY contribution.updated_at DESC, contribution.id DESC
LIMIT #{limit}
|]
where
addCursor :: [ShareContribution UserId] -> (Maybe (Cursor ListContributionsCursor), [ShareContribution UserId])
addCursor xs =
( lastMay xs <&> \(ShareContribution {updatedAt, contributionId}) ->
Cursor (updatedAt, contributionId),
xs
)
-- | Note: Doesn't perform auth checks, the assumption is that if you already have access to
-- the branchId you have access to all associated contributions.
shareContributionsByBranchOf :: Traversal s t BranchId [ShareContribution UserId] -> s -> PG.Transaction e t
shareContributionsByBranchOf trav s =
s
& unsafePartsOf trav %%~ \branchIds -> do
contributionsByBranch <-
( PG.queryListRows @(PG.Only BranchId PG.:. ShareContribution UserId)
[PG.sql|
WITH source_branches(branch_id) AS (
SELECT * FROM ^{PG.singleColumnTable branchIds}
)
SELECT
source_branches.branch_id,
contribution.id,
contribution.contribution_number,
project_owner.handle,
project.slug,
contribution.title,
contribution.description,
contribution.status,
source_branch.name,
source_branch_contributor.handle,
target_branch.name,
target_branch_contributor.handle,
contribution.created_at,
contribution.updated_at,
contribution.author_id,
(SELECT COUNT(*) FROM comments comment WHERE comment.contribution_id = contribution.id AND comment.deleted_at IS NULL) as num_comments
FROM source_branches
JOIN project_branches AS source_branch ON source_branch.id = source_branches.branch_id
JOIN contributions AS contribution ON contribution.source_branch = source_branch.id
JOIN projects AS project ON project.id = contribution.project_id
JOIN users AS project_owner ON project_owner.id = project.owner_user_id
LEFT JOIN users AS source_branch_contributor ON source_branch_contributor.id = source_branch.contributor_id
JOIN project_branches AS target_branch ON target_branch.id = contribution.target_branch
LEFT JOIN users AS target_branch_contributor ON target_branch_contributor.id = target_branch.contributor_id
|]
<&> ( \(results :: [PG.Only BranchId PG.:. ShareContribution UserId]) ->
-- Group by the source branch Id.
results
& fmap
( \(PG.Only branchId PG.:. shareContribution) ->
(branchId, [shareContribution])
)
& Map.fromListWith (<>)
)
)
pure $ branchIds <&> \branchId -> Map.findWithDefault [] branchId contributionsByBranch
getPagedShareContributionTimelineByProjectIdAndNumber ::
ProjectId ->
ContributionNumber ->
Maybe ContributionTimelineCursor ->
Limit ->
PG.Transaction SomeServerError (Maybe ContributionTimelineCursor, [ContributionTimelineEvent UserId])
getPagedShareContributionTimelineByProjectIdAndNumber projectId contributionNumber mayFromExclusive limit = do
Contribution {contributionId} <- contributionByProjectIdAndNumber projectId contributionNumber `whenNothingM` throwSomeServerError (EntityMissing (ErrorID "contribution:missing") "Contribution not found")
mayTillInclusiveTimestamp <- determineUpperDateBound contributionId
statusChangeEvents <- fmap ContributionTimelineStatusChange <$> contributionStatusChangeEventsByContributionId contributionId mayFromExclusive mayTillInclusiveTimestamp
comments <- fmap ContributionTimelineComment <$> commentsByTicketOrContribution (Left contributionId) mayFromExclusive mayTillInclusiveTimestamp
let timeline = List.sortOn eventTimestamp (statusChangeEvents <> comments)
pure (mayTillInclusiveTimestamp, timeline)
where
-- We want to page a set number of events, but are joining across many event sources.
-- This converts a page size into a timestamp range which is easily applicable across all
-- event sources.
--
-- Effectively we join all the events from all sources starting at the 'from' time, then
-- take the first <page-size> events from that set, which allows us to determine the
-- 'till' time to use when ACTUALLY fetching entities from each individual table.
--
-- We can't just grab 'at most N' elements from each table because it may result in some
-- events appearing to be missing from the timeline.
determineUpperDateBound :: ContributionId -> PG.Transaction e (Maybe UTCTime)
determineUpperDateBound contributionId = do
PG.query1Col @(UTCTime)
[PG.sql|
WITH events(timestamp, contribution_id) AS (
SELECT status_event.created_at, status_event.contribution_id FROM contribution_status_events status_event
UNION ALL
SELECT comment.created_at, comment.contribution_id FROM comments comment
), events_in_window (timestamp, contribution_id) AS (
SELECT timestamp, contribution_id FROM events
WHERE events.contribution_id = #{contributionId}
AND (#{mayFromExclusive} IS NULL OR timestamp > #{mayFromExclusive})
ORDER BY timestamp ASC
LIMIT #{limit}
)
SELECT MAX(events_in_window.timestamp) FROM events_in_window
|]
data NewBCAs = NewBCAs
{ sourceBranchCausal :: CausalId,
targetBranchCausal :: CausalId,
bca :: CausalId
}
deriving (Show)
-- | Recompute the best common ancestors for all contributions related to the branch, then
-- return the set of contribution IDs which have been marked as merged.
performMergesAndBCAUpdatesFromBranchPush :: UserId -> BranchId -> PG.Transaction e (Set ContributionId)
performMergesAndBCAUpdatesFromBranchPush callerUserId branchId = do
-- Get the new BCAs for all contributions related to the branch
contributionsToMarkAsMerged <-
PG.queryListCol @(ContributionId)
[PG.sql|
WITH new_bcas(contribution_id, source_causal_id, target_causal_id, bca_id) AS (
SELECT contr.id, source_branch.causal_id, target_branch.causal_id, best_common_causal_ancestor(source_branch.causal_id, target_branch.causal_id) FROM contributions contr
JOIN project_branches AS source_branch ON source_branch.id = contr.source_branch
JOIN project_branches AS target_branch ON target_branch.id = contr.target_branch
WHERE contr.source_branch = #{branchId} OR contr.target_branch = #{branchId}
AND status IN (#{Draft}, #{InReview})
), contributions_to_mark_as_merged(contribution_id) AS (
SELECT contribution_id FROM new_bcas
WHERE new_bcas.bca_id IS NOT NULL
AND new_bcas.bca_id = new_bcas.source_causal_id
), non_merged_bca_updates AS MATERIALIZED (
UPDATE contributions contr
SET best_common_ancestor_causal_id = new_bcas.bca_id
FROM new_bcas
WHERE
contr.id = new_bcas.contribution_id
AND contr.id NOT IN (SELECT contribution_id FROM contributions_to_mark_as_merged)
) SELECT contribution_id FROM contributions_to_mark_as_merged
|]
for_ contributionsToMarkAsMerged \contributionId -> do
_success <- updateContribution callerUserId contributionId Nothing Unchanged (Just Merged) Nothing Nothing
pure ()
pure $ Set.fromList contributionsToMarkAsMerged
rebaseContributionsFromMergedBranches :: Set ContributionId -> PG.Transaction e (Set ContributionId)
rebaseContributionsFromMergedBranches mergedContributions = do
PG.queryListCol @ContributionId
[PG.sql|
WITH merged_contribution_ids(contribution_id) AS (
SELECT * FROM ^{PG.singleColumnTable $ Set.toList mergedContributions}
)
UPDATE contributions contr
SET target_branch = merged_contribution.target_branch
-- Update all open contributions which are merging into a branch that was just merged
FROM merged_contribution_ids
JOIN contributions merged_contribution ON merged_contribution.id = merged_contribution_ids.contribution_id
WHERE merged_contribution.source_branch = contr.target_branch
AND contr.status IN (#{Draft}, #{InReview})
RETURNING contr.id
|]
<&> Set.fromList
contributionStateTokenById :: ContributionId -> PG.Transaction e ContributionStateToken
contributionStateTokenById contributionId = do
PG.queryExpect1Row @ContributionStateToken
[PG.sql|
SELECT
contribution.id,
contribution.source_branch,
contribution.target_branch,
source_causal.hash,
target_causal.hash,
contribution.status
FROM contributions contribution
JOIN project_branches AS source_branch ON source_branch.id = contribution.source_branch
JOIN project_branches AS target_branch ON target_branch.id = contribution.target_branch
JOIN causals source_causal ON source_causal.id = source_branch.causal_id
JOIN causals target_causal ON target_causal.id = target_branch.causal_id
WHERE contribution.id = #{contributionId}
|]
-- | Get whether a precomputed namespace diff exists.
existsPrecomputedNamespaceDiff :: (CodebaseEnv, CausalId) -> (CodebaseEnv, CausalId) -> PG.Transaction e Bool
existsPrecomputedNamespaceDiff
(CodebaseEnv {codebaseOwner = leftCodebaseUser}, leftCausalId)
(CodebaseEnv {codebaseOwner = rightCodebaseUser}, rightCausalId) = do
PG.queryExpect1Col @Bool
[PG.sql|
SELECT EXISTS (
SELECT
FROM namespace_diffs
WHERE left_causal_id = #{leftCausalId}
AND right_causal_id = #{rightCausalId}
AND left_codebase_owner_user_id = #{leftCodebaseUser}
AND right_codebase_owner_user_id = #{rightCodebaseUser}
)
|]
getPrecomputedNamespaceDiff ::
(PG.QueryA m) =>
(CodebaseEnv, CausalId) ->
(CodebaseEnv, CausalId) ->
m (Maybe Text)
getPrecomputedNamespaceDiff
(CodebaseEnv {codebaseOwner = leftCodebaseUser}, leftCausalId)
(CodebaseEnv {codebaseOwner = rightCodebaseUser}, rightCausalId) =
PG.query1Col @Text
[PG.sql|
SELECT (diff :: text)
FROM namespace_diffs nd
WHERE nd.left_causal_id = #{leftCausalId}
AND nd.right_causal_id = #{rightCausalId}
AND nd.left_codebase_owner_user_id = #{leftCodebaseUser}
AND nd.right_codebase_owner_user_id = #{rightCodebaseUser}
|]
savePrecomputedNamespaceDiff ::
(CodebaseEnv, CausalId) ->
(CodebaseEnv, CausalId) ->
Text ->
PG.Transaction e ()
savePrecomputedNamespaceDiff (CodebaseEnv {codebaseOwner = leftCodebaseUser}, leftCausalId) (CodebaseEnv {codebaseOwner = rightCodebaseUser}, rightCausalId) diff = do
PG.execute_
[PG.sql|
INSERT INTO namespace_diffs (left_causal_id, right_causal_id, left_codebase_owner_user_id, right_codebase_owner_user_id, diff)
VALUES (#{leftCausalId}, #{rightCausalId}, #{leftCodebaseUser}, #{rightCodebaseUser}, #{diff}::jsonb)
ON CONFLICT DO NOTHING
|]
-- | Get all draft and in-review contribution IDs for contributions which have a source or target branch in the
-- provided set.
contributionsRelatedToBranches :: Set BranchId -> PG.Transaction e [ContributionId]
contributionsRelatedToBranches branchIds = do
PG.queryListCol @ContributionId
[PG.sql|
WITH related_branches(branch_id) AS (
SELECT * FROM ^{PG.singleColumnTable $ Set.toList branchIds}
)
SELECT contr.id FROM contributions contr
WHERE
(contr.source_branch IN (SELECT branch_id FROM related_branches)
OR contr.target_branch IN (SELECT branch_id FROM related_branches)
)
AND contr.status IN (#{Draft}, #{InReview})
|]