1
+ {-# LANGUAGE ApplicativeDo #-}
1
2
{-# LANGUAGE TypeOperators #-}
2
3
3
4
module Share.Postgres.NamespaceDiffs
@@ -7,7 +8,6 @@ module Share.Postgres.NamespaceDiffs
7
8
where
8
9
9
10
import Data.Either qualified as Either
10
- import Share.Postgres (Transaction )
11
11
import Share.Postgres qualified as PG
12
12
import Share.Postgres.IDs (BranchHashId )
13
13
import Share.Postgres.NameLookups.Types (NameLookupReceipt , NamedRef (.. ), ReversedName )
@@ -25,11 +25,11 @@ import Unison.Util.Relation qualified as Rel
25
25
-- 3. Names that are in both namespaces, but have different refs
26
26
-- 4. Refs that are in both namespaces, but have different names
27
27
getRelevantTermsForDiff ::
28
+ (PG. QueryA m ) =>
28
29
NameLookupReceipt ->
29
30
BranchHashId ->
30
31
BranchHashId ->
31
- Transaction
32
- e
32
+ m
33
33
( Relation Name PGReferent {- relevant terms in old namespace -} ,
34
34
Relation Name PGReferent {- relevant terms only in new namespace -}
35
35
)
@@ -42,9 +42,8 @@ getRelevantTermsForDiff !_nameLookupReceipt oldBranchHashId newBranchHashId = do
42
42
-- 4. Find (name, ref) pairs that are in both namespaces, but have different names
43
43
-- 5. Return the results as a list of (ref, name, isNew) tuples. It's possible for the same
44
44
-- (name, ref) pair to appear with both (isNew = true) and (isNew = false) in the result.
45
- rows <-
46
- PG. queryListRows @ (NamedRef PGReferent PG. :. PG. Only Bool )
47
- [PG. sql |
45
+ PG. queryListRows @ (NamedRef PGReferent PG. :. PG. Only Bool )
46
+ [PG. sql |
48
47
WITH only_in_old AS (
49
48
( SELECT old.reversed_name, old.referent_builtin, old.referent_component_hash_id, old.referent_component_index, old.referent_constructor_index
50
49
FROM scoped_term_name_lookup old
@@ -107,12 +106,15 @@ getRelevantTermsForDiff !_nameLookupReceipt oldBranchHashId newBranchHashId = do
107
106
SELECT new.reversed_name, new.referent_builtin, new.referent_component_hash_id, new.referent_component_index, new.referent_constructor_index, true
108
107
FROM relevant_terms_in_new new
109
108
|]
110
- <&> fmap \ (NamedRef {reversedSegments, ref} PG. :. PG. Only inNew) ->
111
- if inNew
112
- then Right (from @ ReversedName @ Name reversedSegments, ref)
113
- else Left (from @ ReversedName @ Name reversedSegments, ref)
114
- let (old, new) = Either. partitionEithers rows
115
- pure $ (Rel. fromList old, Rel. fromList new)
109
+ <&> ( fmap \ (NamedRef {reversedSegments, ref} PG. :. PG. Only inNew) ->
110
+ if inNew
111
+ then Right (from @ ReversedName @ Name reversedSegments, ref)
112
+ else
113
+ Left (from @ ReversedName @ Name reversedSegments, ref)
114
+ )
115
+ <&> \ rows ->
116
+ let (old, new) = Either. partitionEithers rows
117
+ in (Rel. fromList old, Rel. fromList new)
116
118
117
119
-- | Gets the types relevant for computing the diff between two branches.
118
120
-- Where 'relevant' is defined as:
@@ -121,7 +123,7 @@ getRelevantTermsForDiff !_nameLookupReceipt oldBranchHashId newBranchHashId = do
121
123
-- 2. Types that are in the new namespace but not the old namespace
122
124
-- 3. Names that are in both namespaces, but have different refs
123
125
-- 4. Refs that are in both namespaces, but have different names
124
- getRelevantTypesForDiff :: NameLookupReceipt -> BranchHashId -> BranchHashId -> Transaction e (Relation Name PGReference , Relation Name PGReference )
126
+ getRelevantTypesForDiff :: ( PG. QueryA m ) => NameLookupReceipt -> BranchHashId -> BranchHashId -> m (Relation Name PGReference , Relation Name PGReference )
125
127
getRelevantTypesForDiff ! _nameLookupReceipt oldBranchHashId newBranchHashId = do
126
128
-- This SQL query does the following:
127
129
--
@@ -131,9 +133,8 @@ getRelevantTypesForDiff !_nameLookupReceipt oldBranchHashId newBranchHashId = do
131
133
-- 4. Find (name, ref) pairs that are in both namespaces, but have different names
132
134
-- 5. Return the results as a list of (ref, name, isNew) tuples. It's possible for the same
133
135
-- (name, ref) pair to appear with both (isNew = true) and (isNew = false) in the result.
134
- rows <-
135
- PG. queryListRows @ (NamedRef PGReference PG. :. PG. Only Bool )
136
- [PG. sql |
136
+ PG. queryListRows @ (NamedRef PGReference PG. :. PG. Only Bool )
137
+ [PG. sql |
137
138
WITH only_in_old AS (
138
139
( SELECT old.reversed_name, old.reference_builtin, old.reference_component_hash_id, old.reference_component_index
139
140
FROM scoped_type_name_lookup old
@@ -192,9 +193,11 @@ getRelevantTypesForDiff !_nameLookupReceipt oldBranchHashId newBranchHashId = do
192
193
SELECT new.reversed_name, new.reference_builtin, new.reference_component_hash_id, new.reference_component_index, true
193
194
FROM relevant_types_in_new new
194
195
|]
195
- <&> fmap \ (NamedRef {reversedSegments, ref} PG. :. PG. Only inNew) ->
196
- if inNew
197
- then Right (from @ ReversedName @ Name reversedSegments, ref)
198
- else Left (from @ ReversedName @ Name reversedSegments, ref)
199
- let (old, new) = Either. partitionEithers rows
200
- pure $ (Rel. fromList old, Rel. fromList new)
196
+ <&> ( fmap \ (NamedRef {reversedSegments, ref} PG. :. PG. Only inNew) ->
197
+ if inNew
198
+ then Right (from @ ReversedName @ Name reversedSegments, ref)
199
+ else Left (from @ ReversedName @ Name reversedSegments, ref)
200
+ )
201
+ <&> \ rows ->
202
+ let (old, new) = Either. partitionEithers rows
203
+ in (Rel. fromList old, Rel. fromList new)
0 commit comments