@@ -6,81 +6,21 @@ import (
66 "fmt"
77)
88
9- // SyncDataStoreToCatalog pushes all data from a local DataStore to a remote CatalogStore within
10- // a transaction. This ensures atomic updates - either all data is successfully synced or the
11- // entire operation is rolled back on failure.
9+ // MergeDataStoreToCatalog merges data from a source DataStore (either full local state or migration-specific)
10+ // into a remote CatalogStore within a transaction. This ensures atomic updates - either all data is
11+ // successfully merged or the entire operation is rolled back on failure.
1212//
13- // This function is the inverse of LoadDataStoreFromCatalog - while that function reads from
14- // catalog to local, this function writes from local to catalog.
15- func SyncDataStoreToCatalog (ctx context.Context , localDS DataStore , catalog CatalogStore ) error {
16- return catalog .WithTransaction (ctx , func (ctx context.Context , txCatalog BaseCatalogStore ) error {
17- // Sync all address references to the catalog
18- addressRefs , err := localDS .Addresses ().Fetch ()
19- if err != nil {
20- return fmt .Errorf ("failed to fetch address references from local store: %w" , err )
21- }
22-
23- for _ , ref := range addressRefs {
24- if upsertErr := txCatalog .Addresses ().Upsert (ctx , ref ); upsertErr != nil {
25- return fmt .Errorf ("failed to upsert address reference to catalog: %w" , upsertErr )
26- }
27- }
28-
29- // Sync all chain metadata to the catalog
30- chainMetadata , err := localDS .ChainMetadata ().Fetch ()
31- if err != nil {
32- return fmt .Errorf ("failed to fetch chain metadata from local store: %w" , err )
33- }
34-
35- for _ , metadata := range chainMetadata {
36- key := NewChainMetadataKey (metadata .ChainSelector )
37- if upsertErr := txCatalog .ChainMetadata ().Upsert (ctx , key , metadata .Metadata ); upsertErr != nil {
38- return fmt .Errorf ("failed to upsert chain metadata to catalog: %w" , upsertErr )
39- }
40- }
41-
42- // Sync all contract metadata to the catalog
43- contractMetadata , err := localDS .ContractMetadata ().Fetch ()
44- if err != nil {
45- return fmt .Errorf ("failed to fetch contract metadata from local store: %w" , err )
46- }
47-
48- for _ , metadata := range contractMetadata {
49- key := NewContractMetadataKey (metadata .ChainSelector , metadata .Address )
50- if upsertErr := txCatalog .ContractMetadata ().Upsert (ctx , key , metadata .Metadata ); upsertErr != nil {
51- return fmt .Errorf ("failed to upsert contract metadata to catalog: %w" , upsertErr )
52- }
53- }
54-
55- // Sync environment metadata to the catalog
56- envMetadata , err := localDS .EnvMetadata ().Get ()
57- if err != nil {
58- // EnvMetadata might not be set, which is okay
59- if ! errors .Is (err , ErrEnvMetadataNotSet ) {
60- return fmt .Errorf ("failed to fetch environment metadata from local store: %w" , err )
61- }
62- // If it's ErrEnvMetadataNotSet, skip syncing env metadata
63- return nil
64- }
65-
66- // Sync the environment metadata
67- if setErr := txCatalog .EnvMetadata ().Set (ctx , envMetadata .Metadata ); setErr != nil {
68- return fmt .Errorf ("failed to set environment metadata in catalog: %w" , setErr )
69- }
70-
71- return nil
72- })
73- }
74-
75- // MergeDataStoreToCatalog merges data from a migration/changeset DataStore into the catalog
76- // within a transaction. This is used after a migration/changeset execution to persist new
77- // contract deployments and metadata to the catalog.
78- func MergeDataStoreToCatalog (ctx context.Context , migrationDS DataStore , catalog CatalogStore ) error {
13+ // This function serves two purposes:
14+ // 1. Initial migration: sync entire local datastore to catalog (full state push)
15+ // 2. Ongoing operations: merge migration/changeset artifacts into catalog (incremental updates)
16+ //
17+ // The operation is transactional to prevent partial failures that could lead to data inconsistencies.
18+ func MergeDataStoreToCatalog (ctx context.Context , sourceDS DataStore , catalog CatalogStore ) error {
7919 return catalog .WithTransaction (ctx , func (ctx context.Context , txCatalog BaseCatalogStore ) error {
8020 // Merge all address references to the catalog
81- addressRefs , err := migrationDS .Addresses ().Fetch ()
21+ addressRefs , err := sourceDS .Addresses ().Fetch ()
8222 if err != nil {
83- return fmt .Errorf ("failed to fetch address references from migration store: %w" , err )
23+ return fmt .Errorf ("failed to fetch address references from source store: %w" , err )
8424 }
8525
8626 for _ , ref := range addressRefs {
@@ -90,9 +30,9 @@ func MergeDataStoreToCatalog(ctx context.Context, migrationDS DataStore, catalog
9030 }
9131
9232 // Merge all chain metadata to the catalog
93- chainMetadata , err := migrationDS .ChainMetadata ().Fetch ()
33+ chainMetadata , err := sourceDS .ChainMetadata ().Fetch ()
9434 if err != nil {
95- return fmt .Errorf ("failed to fetch chain metadata from migration store: %w" , err )
35+ return fmt .Errorf ("failed to fetch chain metadata from source store: %w" , err )
9636 }
9737
9838 for _ , metadata := range chainMetadata {
@@ -103,9 +43,9 @@ func MergeDataStoreToCatalog(ctx context.Context, migrationDS DataStore, catalog
10343 }
10444
10545 // Merge all contract metadata to the catalog
106- contractMetadata , err := migrationDS .ContractMetadata ().Fetch ()
46+ contractMetadata , err := sourceDS .ContractMetadata ().Fetch ()
10747 if err != nil {
108- return fmt .Errorf ("failed to fetch contract metadata from migration store: %w" , err )
48+ return fmt .Errorf ("failed to fetch contract metadata from source store: %w" , err )
10949 }
11050
11151 for _ , metadata := range contractMetadata {
@@ -116,10 +56,10 @@ func MergeDataStoreToCatalog(ctx context.Context, migrationDS DataStore, catalog
11656 }
11757
11858 // Merge environment metadata to the catalog
119- envMetadata , err := migrationDS .EnvMetadata ().Get ()
59+ envMetadata , err := sourceDS .EnvMetadata ().Get ()
12060 if err != nil {
12161 if ! errors .Is (err , ErrEnvMetadataNotSet ) {
122- return fmt .Errorf ("failed to fetch environment metadata from migration store: %w" , err )
62+ return fmt .Errorf ("failed to fetch environment metadata from source store: %w" , err )
12363 }
12464
12565 return nil
0 commit comments