Skip to content

Commit 199c5e5

Browse files
committed
Fix migration code in spite of conflicts
1 parent 892f55b commit 199c5e5

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

sql/2025-01-24_migrate-serialized-entities.sql

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,40 @@ CREATE TABLE migrate_serialized_queue_unsandboxed (
6767

6868
-- EXTRA
6969

70+
-- If some causals are missing:
71+
-- INSERT INTO migrate_serialized_queue_unsandboxed (hash, user_id)
72+
-- SELECT DISTINCT ON(c.hash) c.hash, co.user_id
73+
-- FROM causal_ownership co
74+
-- JOIN causals c ON co.causal_id = c.id
75+
-- WHERE NOT EXISTS(SELECT FROM serialized_causals sc WHERE sc.causal_id = c.id)
76+
-- ;
77+
78+
79+
-- If some namespaces are missing:
80+
-- INSERT INTO migrate_serialized_queue_unsandboxed (hash, user_id)
81+
-- SELECT DISTINCT ON(bh.base32) bh.base32, no.user_id
82+
-- FROM namespace_ownership no
83+
-- JOIN branch_hashes bh ON no.namespace_hash_id = bh.id
84+
-- WHERE NOT EXISTS(SELECT FROM serialized_namespaces sn WHERE sn.namespace_hash_id = bh.id)
85+
-- ;
86+
87+
-- If some term components are missing
88+
-- INSERT INTO migrate_serialized_queue_sandboxed (component_hash_id, user_id)
89+
-- SELECT DISTINCT ON (t.component_hash_id) t.component_hash_id, st.user_id
90+
-- FROM sandboxed_terms st
91+
-- JOIN terms t ON st.term_id = t.id
92+
-- WHERE NOT EXISTS(SELECT FROM serialized_components sc WHERE sc.user_id = st.user_id AND sc.component_hash_id = t.component_hash_id)
93+
-- ;
94+
95+
-- If some type components are missing
96+
-- INSERT INTO migrate_serialized_queue_sandboxed (component_hash_id, user_id)
97+
-- SELECT DISTINCT ON (t.component_hash_id) t.component_hash_id, st.user_id
98+
-- FROM sandboxed_types st
99+
-- JOIN types t ON st.type_id = t.id
100+
-- WHERE NOT EXISTS(SELECT FROM serialized_components sc WHERE sc.user_id = st.user_id AND sc.component_hash_id = t.component_hash_id)
101+
-- ;
102+
103+
70104
-- If some components were somehow missing, this will detect which ones still need processing and add them back to
71105
-- the queue.
72106
-- INSERT INTO migrate_serialized_queue_sandboxed (component_hash_id, user_id)

src/Share/BackgroundJobs/SerializedEntitiesMigration/Worker.hs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,21 @@ processComponents !_authZReceipt = do
6565
Q.claimComponent >>= \case
6666
Nothing -> pure False
6767
Just (componentHashId, userId) -> do
68-
let codebaseEnv = CodebaseEnv userId
69-
Codebase.codebaseMToTransaction codebaseEnv $ do
70-
hash32 <- (Hash32.fromHash . unComponentHash) <$> HQ.expectComponentHashesOf id componentHashId
71-
entity <- SQ.expectEntity hash32
72-
componentSummaryDigest <- HQ.expectComponentSummaryDigest componentHashId
73-
let tempEntity = SyncCommon.entityToTempEntity id entity
74-
let (SyncV2.CBORBytes bytes) = SyncV2.serialiseCBORBytes tempEntity
75-
bytesId <- DefnQ.ensureBytesIdsOf id (BL.toStrict bytes)
76-
execute_
77-
[sql|
68+
let codebaseEnv = CodebaseEnv userId
69+
Codebase.codebaseMToTransaction codebaseEnv $ do
70+
hash32 <- (Hash32.fromHash . unComponentHash) <$> HQ.expectComponentHashesOf id componentHashId
71+
entity <- SQ.expectEntity hash32
72+
componentSummaryDigest <- HQ.expectComponentSummaryDigest componentHashId
73+
let tempEntity = SyncCommon.entityToTempEntity id entity
74+
let (SyncV2.CBORBytes bytes) = SyncV2.serialiseCBORBytes tempEntity
75+
bytesId <- DefnQ.ensureBytesIdsOf id (BL.toStrict bytes)
76+
execute_
77+
[sql|
7878
INSERT INTO component_summary_digests_to_serialized_component_bytes_hash (component_hash_id, component_summary_digest, serialized_component_bytes_id)
7979
VALUES (#{componentHashId}, #{componentSummaryDigest}, #{bytesId})
80+
ON CONFLICT DO NOTHING
8081
|]
81-
pure True
82+
pure True
8283

8384
saveUnsandboxedSerializedEntities :: (QueryM m) => Hash32 -> TempEntity -> m ()
8485
saveUnsandboxedSerializedEntities hash entity = do

0 commit comments

Comments
 (0)