Skip to content

Commit 23543b4

Browse files
committed
Run the migration on this iteration.
1 parent 932b2d1 commit 23543b4

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

sql/2025-02-25_causal_depth_migration.sql renamed to sql/migration-helpers/2025-02-25_causal_depth_migration.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@ INSERT INTO unfinished_component_depths (id)
4343
SELECT FROM component_depth cd WHERE cd.component_hash_id = ch.id
4444
);
4545

46+
-- Afterwards
47+
DROP TABLE unfinished_causal_depths;
48+
DROP TABLE unfinished_namespace_depths;
49+
DROP TABLE unfinished_patch_depths;
50+
DROP TABLE unfinished_component_depths;

src/Share/BackgroundJobs/EntityDepthMigration/Queries.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ updateComponentDepths = do
3838
t.component_hash_id = ch.id
3939
AND cr_sub.type_id = t.id AND cd.depth IS NULL
4040
)
41+
LIMIT 10000
4142
), updated(component_hash_id, x) AS (
4243
SELECT ch.component_hash_id, update_component_depth(ch.component_hash_id)
4344
FROM updatable_components ch
@@ -89,6 +90,7 @@ updatePatchDepths = do
8990
WHERE ptm.patch_id = p.id
9091
AND cd.depth IS NULL
9192
)
93+
LIMIT 10000
9294
), updated(patch_id, x) AS (
9395
SELECT up.patch_id, update_patch_depth(up.patch_id)
9496
FROM updatable_patches up
@@ -176,6 +178,7 @@ updateNamespaceDepths = do
176178
WHERE nt.namespace_hash_id = n.id
177179
AND cd.depth IS NULL
178180
)
181+
LIMIT 10000
179182
), updated(namespace_hash_id, x) AS (
180183
SELECT un.namespace_hash_id, update_namespace_depth(un.namespace_hash_id)
181184
FROM updatable_namespaces un
@@ -210,6 +213,7 @@ updateCausalDepths = do
210213
WHERE ca.causal_id = c.id
211214
AND cd.depth IS NULL
212215
)
216+
LIMIT 10000
213217
), updated(causal_id) AS (
214218
SELECT c.id, update_causal_depth(c.id)
215219
FROM updatable_causals c

src/Share/BackgroundJobs/EntityDepthMigration/Worker.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ worker scope = do
3232
-- NOTE: this process doesn't insert the row into serialized_components, you'll need to do that manually after the automated migration is finished.
3333
computeComponentDepths :: AuthZ.AuthZReceipt -> Background ()
3434
computeComponentDepths !_authZReceipt = do
35+
Logging.logInfoText $ "Computing component depth for batch"
3536
PG.runTransaction Q.updateComponentDepths >>= \case
3637
0 -> do
3738
Logging.logInfoText $ "Done processing component depth"
@@ -43,6 +44,7 @@ computeComponentDepths !_authZReceipt = do
4344

4445
computePatchDepths :: AuthZ.AuthZReceipt -> Background ()
4546
computePatchDepths !_authZReceipt = do
47+
Logging.logInfoText $ "Computing patch depth for batch"
4648
PG.runTransaction Q.updatePatchDepths >>= \case
4749
0 -> do
4850
Logging.logInfoText $ "Done processing patch depth"
@@ -54,6 +56,7 @@ computePatchDepths !_authZReceipt = do
5456

5557
computeNamespaceAndCausalDepths :: AuthZ.AuthZReceipt -> Background ()
5658
computeNamespaceAndCausalDepths !authZReceipt = do
59+
Logging.logInfoText $ "Computing namespace and causal depth for batch"
5760
PG.runTransaction Q.updateNamespaceDepths >>= \case
5861
namespaceN -> do
5962
PG.runTransaction Q.updateCausalDepths >>= \case

src/Share/Web/UCM/SyncV2/Impl.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ batchSize :: Int32
4848
batchSize = 1000
4949

5050
streamSettings :: Hash32 -> Maybe SyncV2.BranchRef -> StreamInitInfo
51-
streamSettings rootCausalHash rootBranchRef = StreamInitInfo {version = SyncV2.Version 1, entitySorting = SyncV2.DependenciesFirst, numEntities = Nothing, rootCausalHash, rootBranchRef}
51+
streamSettings rootCausalHash rootBranchRef = StreamInitInfo {version = SyncV2.Version 1, entitySorting = SyncV2.Unsorted, numEntities = Nothing, rootCausalHash, rootBranchRef}
5252

5353
server :: Maybe UserId -> SyncV2.Routes WebAppServer
5454
server mayUserId =

src/Share/Web/UCM/SyncV2/Queries.hs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ allSerializedDependenciesOfCausalCursor cid exceptCausalHashes = do
5050
JOIN component_hashes ch ON dh.hash = ch.base32
5151
|]
5252
cursor <-
53-
PGCursor.newRowCursor
53+
PGCursor.newRowCursor @(CBORBytes TempEntity, Hash32, Maybe Int32)
5454
"serialized_entities"
5555
[sql|
5656
WITH RECURSIVE transitive_causals(causal_id, causal_hash, causal_namespace_hash_id) AS (
@@ -178,32 +178,44 @@ allSerializedDependenciesOfCausalCursor cid exceptCausalHashes = do
178178
JOIN serialized_components sc ON sc.user_id = #{ownerUserId} AND tc.component_hash_id = sc.component_hash_id
179179
JOIN bytes ON sc.bytes_id = bytes.id
180180
JOIN component_hashes ch ON tc.component_hash_id = ch.id
181-
JOIN component_depth cd ON ch.id = cd.component_hash_id
181+
LEFT JOIN component_depth cd ON ch.id = cd.component_hash_id
182182
)
183183
UNION ALL
184184
(SELECT bytes.bytes, ap.patch_hash, pd.depth
185185
FROM all_patches ap
186186
JOIN serialized_patches sp ON ap.patch_id = sp.patch_id
187187
JOIN bytes ON sp.bytes_id = bytes.id
188-
JOIN patch_depth pd ON ap.patch_id = pd.patch_id
188+
LEFT JOIN patch_depth pd ON ap.patch_id = pd.patch_id
189189
)
190190
UNION ALL
191191
(SELECT bytes.bytes, an.namespace_hash, nd.depth
192192
FROM all_namespaces an
193193
JOIN serialized_namespaces sn ON an.namespace_hash_id = sn.namespace_hash_id
194194
JOIN bytes ON sn.bytes_id = bytes.id
195-
JOIN namespace_depth nd ON an.namespace_hash_id = nd.namespace_hash_id
195+
LEFT JOIN namespace_depth nd ON an.namespace_hash_id = nd.namespace_hash_id
196196
)
197197
UNION ALL
198198
(SELECT bytes.bytes, tc.causal_hash, cd.depth
199199
FROM transitive_causals tc
200200
JOIN serialized_causals sc ON tc.causal_id = sc.causal_id
201201
JOIN bytes ON sc.bytes_id = bytes.id
202-
JOIN causal_depth cd ON tc.causal_id = cd.causal_id
202+
LEFT JOIN causal_depth cd ON tc.causal_id = cd.causal_id
203203
)
204-
ORDER BY depth ASC
204+
-- Re-add this once the migration is done.
205+
-- Put them in dependency order, nulls come first because we want to bail and
206+
-- report an error
207+
-- if we somehow are missing a depth.
208+
-- ORDER BY depth ASC NULLS FIRST
205209
|]
206-
pure cursor
210+
-- pure
211+
-- ( cursor <&> \(bytes, hash, depth) -> case depth of
212+
-- -- This should never happen, but is a sanity check in case we're missing a depth.
213+
-- -- Better than silently omitting a required result.
214+
-- Nothing -> error $ "allSerializedDependenciesOfCausalCursor: Missing depth for entity: " <> show hash
215+
-- Just _ -> (bytes, hash)
216+
-- )
217+
pure
218+
(cursor <&> \(bytes, hash, _depth) -> (bytes, hash))
207219

208220
spineAndLibDependenciesOfCausalCursor :: CausalId -> CodebaseM e (PGCursor (Hash32, IsCausalSpine, IsLibRoot))
209221
spineAndLibDependenciesOfCausalCursor cid = do

0 commit comments

Comments
 (0)