Skip to content

Add ResourceModel.Update implementation for BadgerDB #215

@jim-junior

Description

@jim-junior

Description

Add the Update method implementation for BadgerDB. This method is responsible for updating an existing resource, incrementing its metadata version, and persisting both the current state and a new versioned entry in the local key-value store.

func (m *ResourceModel) Update(name string, resourceType string, resource types.Resource) (types.Resource, error) {

Requirements

  • File Location: internal/models/resource.go
  • Transactionality: The entire operation must be wrapped in a BadgerDB Update transaction to ensure the version increment and dual-key writes (primary and versioned) are atomic.
  • Existence Check: The method must first verify that the resource exists. If not found, it should return a "not found" error.
  • Versioning Logic:
    1. Retrieve the current version from the existing resource's metadata.
    2. Increment the version number.
    3. Update the Metadata["version"] field in the resource object.
  • Dual-Key Write: The method must update two entries:
    1. The primary resource key: /resources/{resourceType}/{name}.
    2. A new versioned key: /resources/{resourceType}/{name}/{newVersionNumber}.
  • Dependency Injection: The method must use the *badger.DB instance injected into the ResourceModel.
  • Testing: A corresponding test case must be created in internal/models/resource_test.go.

Success Criteria

  • The Update method successfully modifies records in BadgerDB within an atomic transaction.
  • The version number in metadata is correctly incremented by 1.
  • Both the master key and the new versioned key (e.g., /2) exist in the database after the update.
  • Test Case implementation: A unit test is provided that:
    • Pre-populates BadgerDB with an initial resource (version 1).
    • Calls Update with modified data.
    • Asserts that the primary key now contains the new data and version 2.
    • Asserts that a new key ending in /2 has been created.
    • Asserts that an error is returned if trying to update a non-existent resource.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    Status

    Todo

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions