@@ -102,7 +102,7 @@ type TableDiffTask struct {
102102 blockHashSQLCache map [hashBoundsKey ]string
103103 blockHashSQLMu sync.Mutex
104104
105- NodeOriginNames map [string ]string
105+ NodeOriginNames map [string ]map [ string ] string
106106
107107 CompareUnitSize int
108108 MaxDiffRows int64
@@ -204,53 +204,66 @@ func (t *TableDiffTask) loadNodeOriginNames() error {
204204 return nil
205205 }
206206
207- var firstPool * pgxpool.Pool
208- for _ , pool := range t .Pools {
209- firstPool = pool
210- break
211- }
207+ t .NodeOriginNames = make (map [string ]map [string ]string )
212208
213- if firstPool == nil {
214- t .NodeOriginNames = make (map [string ]string )
215- return fmt .Errorf ("no connection pool available to load node origin names" )
209+ var lastErr error
210+ for name , pool := range t .Pools {
211+ names , err := queries .GetNodeOriginNames (t .Ctx , pool )
212+ if err != nil {
213+ lastErr = err
214+ continue
215+ }
216+ t .NodeOriginNames [name ] = names
216217 }
217218
218- names , err := queries .GetNodeOriginNames (t .Ctx , firstPool )
219- if err != nil {
220- t .NodeOriginNames = make (map [string ]string )
221- return err
219+ if len (t .NodeOriginNames ) == 0 && lastErr != nil {
220+ return lastErr
222221 }
223-
224- t .NodeOriginNames = names
225222 return nil
226223}
227224
225+ // flatNodeOriginNames merges all per-node origin maps into a single map for
226+ // lookup purposes (e.g. resolving --against-origin). If the same roident
227+ // appears on multiple nodes, the last one wins — this is acceptable because
228+ // resolveAgainstOrigin is only used with spock (where roidents are global)
229+ // or for user-facing name resolution where any match suffices.
230+ func (t * TableDiffTask ) flatNodeOriginNames () map [string ]string {
231+ flat := make (map [string ]string )
232+ for _ , nodeMap := range t .NodeOriginNames {
233+ for id , name := range nodeMap {
234+ flat [id ] = name
235+ }
236+ }
237+ return flat
238+ }
239+
228240func (t * TableDiffTask ) resolveAgainstOrigin () error {
229241 if strings .TrimSpace (t .AgainstOrigin ) == "" {
230242 return nil
231243 }
232- if len (t .NodeOriginNames ) == 0 {
244+ flat := t .flatNodeOriginNames ()
245+ if len (flat ) == 0 {
233246 return fmt .Errorf ("unable to resolve --against-origin: no node origin names available" )
234247 }
235248
236249 orig := strings .TrimSpace (t .AgainstOrigin )
237250 // direct match on origin id
238- if _ , ok := t . NodeOriginNames [orig ]; ok {
251+ if _ , ok := flat [orig ]; ok {
239252 t .resolvedAgainstOrigin = orig
240253 return nil
241254 }
242255
243256 // match on origin name
244- for id , name := range t . NodeOriginNames {
257+ for id , name := range flat {
245258 if name == orig {
246259 t .resolvedAgainstOrigin = id
247260 return nil
248261 }
249262 }
250263
251264 // build a list of available origins for the error message
252- available := make ([]string , 0 , len (t . NodeOriginNames ))
253- for id , name := range t . NodeOriginNames {
265+ available := make ([]string , 0 , len (flat ))
266+ for id , name := range flat {
254267 if id != name {
255268 available = append (available , fmt .Sprintf ("%s (%s)" , id , name ))
256269 } else {
@@ -294,8 +307,8 @@ func (t *TableDiffTask) buildEffectiveFilter() (string, error) {
294307 return strings .Join (parts , " AND " ), nil
295308}
296309
297- func (t * TableDiffTask ) withSpockMetadata (row map [string ]any ) map [string ]any {
298- row ["node_origin" ] = utils .TranslateNodeOrigin (row ["node_origin" ], t .NodeOriginNames )
310+ func (t * TableDiffTask ) withSpockMetadata (row map [string ]any , nodeName string ) map [string ]any {
311+ row ["node_origin" ] = utils .TranslateNodeOrigin (row ["node_origin" ], t .NodeOriginNames [ nodeName ] )
299312 return utils .AddSpockMetadata (row )
300313}
301314
@@ -1375,8 +1388,9 @@ func (t *TableDiffTask) ExecuteTask() (err error) {
13751388 DiffRowsCount : make (map [string ]int ),
13761389 AgainstOrigin : t .AgainstOrigin ,
13771390 AgainstOriginResolved : func () string {
1378- if t .resolvedAgainstOrigin != "" && t .NodeOriginNames != nil {
1379- if name , ok := t .NodeOriginNames [t .resolvedAgainstOrigin ]; ok {
1391+ if t .resolvedAgainstOrigin != "" {
1392+ flat := t .flatNodeOriginNames ()
1393+ if name , ok := flat [t .resolvedAgainstOrigin ]; ok {
13801394 return name
13811395 }
13821396 }
@@ -1995,7 +2009,7 @@ func (t *TableDiffTask) recursiveDiff(
19952009 break
19962010 }
19972011 rowAsMap := utils .OrderedMapToMap (row )
1998- rowWithMeta := t .withSpockMetadata (rowAsMap )
2012+ rowWithMeta := t .withSpockMetadata (rowAsMap , node1Name )
19992013 rowAsOrderedMap := utils .MapToOrderedMap (rowWithMeta , t .Cols )
20002014 t .DiffResult .NodeDiffs [pairKey ].Rows [node1Name ] = append (t .DiffResult .NodeDiffs [pairKey ].Rows [node1Name ], rowAsOrderedMap )
20012015 currentDiffRowsForPair ++
@@ -2012,7 +2026,7 @@ func (t *TableDiffTask) recursiveDiff(
20122026 break
20132027 }
20142028 rowAsMap := utils .OrderedMapToMap (row )
2015- rowWithMeta := t .withSpockMetadata (rowAsMap )
2029+ rowWithMeta := t .withSpockMetadata (rowAsMap , node2Name )
20162030 rowAsOrderedMap := utils .MapToOrderedMap (rowWithMeta , t .Cols )
20172031 t .DiffResult .NodeDiffs [pairKey ].Rows [node2Name ] = append (t .DiffResult .NodeDiffs [pairKey ].Rows [node2Name ], rowAsOrderedMap )
20182032 currentDiffRowsForPair ++
@@ -2030,12 +2044,12 @@ func (t *TableDiffTask) recursiveDiff(
20302044 break
20312045 }
20322046 node1DataAsMap := utils .OrderedMapToMap (modRow .Node1Data )
2033- node1DataWithMeta := t .withSpockMetadata (node1DataAsMap )
2047+ node1DataWithMeta := t .withSpockMetadata (node1DataAsMap , node1Name )
20342048 node1DataAsOrderedMap := utils .MapToOrderedMap (node1DataWithMeta , t .Cols )
20352049 t .DiffResult .NodeDiffs [pairKey ].Rows [node1Name ] = append (t .DiffResult .NodeDiffs [pairKey ].Rows [node1Name ], node1DataAsOrderedMap )
20362050
20372051 node2DataAsMap := utils .OrderedMapToMap (modRow .Node2Data )
2038- node2DataWithMeta := t .withSpockMetadata (node2DataAsMap )
2052+ node2DataWithMeta := t .withSpockMetadata (node2DataAsMap , node2Name )
20392053 node2DataAsOrderedMap := utils .MapToOrderedMap (node2DataWithMeta , t .Cols )
20402054 t .DiffResult .NodeDiffs [pairKey ].Rows [node2Name ] = append (t .DiffResult .NodeDiffs [pairKey ].Rows [node2Name ], node2DataAsOrderedMap )
20412055 currentDiffRowsForPair ++
0 commit comments