UpdateServer updates an existing server record with new server details
(ctx context.Context, tx pgx.Tx, serverName, version string, serverJSON *apiv0.ServerJSON)
| 541 | |
| 542 | // UpdateServer updates an existing server record with new server details |
| 543 | func (db *PostgreSQL) UpdateServer(ctx context.Context, tx pgx.Tx, serverName, version string, serverJSON *apiv0.ServerJSON) (*apiv0.ServerResponse, error) { |
| 544 | if ctx.Err() != nil { |
| 545 | return nil, ctx.Err() |
| 546 | } |
| 547 | |
| 548 | // Validate inputs |
| 549 | if serverJSON == nil { |
| 550 | return nil, fmt.Errorf("serverJSON is required") |
| 551 | } |
| 552 | |
| 553 | // Ensure the serverJSON matches the provided serverName and version |
| 554 | if serverJSON.Name != serverName || serverJSON.Version != version { |
| 555 | return nil, fmt.Errorf("%w: server name and version in JSON must match parameters", ErrInvalidInput) |
| 556 | } |
| 557 | |
| 558 | // Marshal updated ServerJSON |
| 559 | valueJSON, err := json.Marshal(serverJSON) |
| 560 | if err != nil { |
| 561 | return nil, fmt.Errorf("failed to marshal updated server: %w", err) |
| 562 | } |
| 563 | |
| 564 | // Update only the JSON data (keep existing metadata columns) |
| 565 | query := ` |
| 566 | UPDATE servers |
| 567 | SET value = $1, updated_at = NOW() |
| 568 | WHERE server_name = $2 AND version = $3 |
| 569 | RETURNING server_name, version, status, status_changed_at, status_message, published_at, updated_at, is_latest |
| 570 | ` |
| 571 | |
| 572 | var name, vers, status string |
| 573 | var statusChangedAt, publishedAt, updatedAt time.Time |
| 574 | var statusMessage *string |
| 575 | var isLatest bool |
| 576 | |
| 577 | err = db.getExecutor(tx).QueryRow(ctx, query, valueJSON, serverName, version).Scan(&name, &vers, &status, &statusChangedAt, &statusMessage, &publishedAt, &updatedAt, &isLatest) |
| 578 | if err != nil { |
| 579 | if errors.Is(err, pgx.ErrNoRows) { |
| 580 | return nil, ErrNotFound |
| 581 | } |
| 582 | return nil, fmt.Errorf("failed to update server: %w", err) |
| 583 | } |
| 584 | |
| 585 | // Return the updated ServerResponse |
| 586 | serverResponse := &apiv0.ServerResponse{ |
| 587 | Server: *serverJSON, |
| 588 | Meta: apiv0.ResponseMeta{ |
| 589 | Official: &apiv0.RegistryExtensions{ |
| 590 | Status: model.Status(status), |
| 591 | StatusChangedAt: statusChangedAt, |
| 592 | StatusMessage: statusMessage, |
| 593 | PublishedAt: publishedAt, |
| 594 | UpdatedAt: updatedAt, |
| 595 | IsLatest: isLatest, |
| 596 | }, |
| 597 | }, |
| 598 | } |
| 599 | |
| 600 | return serverResponse, nil |
nothing calls this directly
no test coverage detected