PutRootObject adds the given root object to the respective Collection in the root, returning the updated root.
(ctx context.Context, root objinterface.RootValue, tName doltdb.TableName, rootObj objinterface.RootObject)
| 418 | |
| 419 | // PutRootObject adds the given root object to the respective Collection in the root, returning the updated root. |
| 420 | func PutRootObject(ctx context.Context, root objinterface.RootValue, tName doltdb.TableName, rootObj objinterface.RootObject) (objinterface.RootValue, error) { |
| 421 | if rootObj == nil { |
| 422 | return root, nil |
| 423 | } |
| 424 | coll, err := LoadCollection(ctx, root, rootObj.GetRootObjectID()) |
| 425 | if err != nil { |
| 426 | return nil, err |
| 427 | } |
| 428 | identifier := coll.TableNameToID(tName) |
| 429 | exists, err := coll.HasRootObject(ctx, identifier) |
| 430 | if err != nil { |
| 431 | return nil, err |
| 432 | } |
| 433 | // If this doesn't exist, it may be because the name is slightly different (e.g. missing schema), and we want to resolve it properly |
| 434 | if !exists { |
| 435 | _, resolvedID, err := coll.ResolveName(ctx, tName) |
| 436 | if err != nil { |
| 437 | return nil, err |
| 438 | } |
| 439 | if resolvedID.IsValid() { |
| 440 | identifier = resolvedID |
| 441 | exists = true |
| 442 | } |
| 443 | } |
| 444 | if exists { |
| 445 | if err = coll.DropRootObject(ctx, identifier); err != nil { |
| 446 | return nil, err |
| 447 | } |
| 448 | } |
| 449 | // If this is a conflict, then we only want to put the conflict in the collection if it produces conflict diffs. |
| 450 | // Otherwise, we'll put the merged root object instead. |
| 451 | if conflict, ok := rootObj.(objinterface.Conflict); ok { |
| 452 | diffs, merged, err := conflict.Diffs(ctx) |
| 453 | if err != nil { |
| 454 | return nil, err |
| 455 | } |
| 456 | if len(diffs) == 0 { |
| 457 | // If we deleted from the conflicts collection, then we need to update the collection on the root before returning |
| 458 | if exists { |
| 459 | root, err = coll.UpdateRoot(ctx, root) |
| 460 | if err != nil { |
| 461 | return nil, err |
| 462 | } |
| 463 | } |
| 464 | if merged == nil { |
| 465 | return RemoveRootObjectIfExists(ctx, root, conflict.GetID(), conflict.GetContainedRootObjectID()) |
| 466 | } else { |
| 467 | return PutRootObject(ctx, root, tName, merged) |
| 468 | } |
| 469 | } else { |
| 470 | if merged == nil { |
| 471 | root, err = RemoveRootObjectIfExists(ctx, root, conflict.GetID(), conflict.GetContainedRootObjectID()) |
| 472 | if err != nil { |
| 473 | return nil, err |
| 474 | } |
| 475 | } else { |
| 476 | root, err = PutRootObject(ctx, root, tName, merged) |
| 477 | if err != nil { |
no test coverage detected