Skip to content

Add ResourceModel.Insert implementation for BadgerDB #211

@jim-junior

Description

@jim-junior

Description

Add the Insert method implementation for BadgerDB at

func (m *ResourceModel) Insert(name string, resourceType string, resource []byte) error {

This method is responsible for persisting a new resource and initializing its versioning history within the local key-value store.

Requirements

  • File Location: internal/models/resource.go
  • Transactionality: The operation must be wrapped in a BadgerDB Update transaction to ensure atomicity between saving the primary record and the initial version record.
  • Uniqueness Constraint: The method must verify if a resource with the same name and type already exists. If the key is present, it must return a clear "already exists" error to prevent accidental overwrites.
  • Versioning Initialization: Upon a successful insert, the method must create two distinct entries:
    1. The primary resource entry at the path: /resources/{resourceType}/{name}.
    2. The initial version entry at the path: /resources/{resourceType}/{name}/1.
  • Dependency Injection: The ResourceModel struct must be updated to use the *badger.DB instance instead of the etcd client.
  • Testing: A corresponding test case must be created in internal/models/resource_test.go (or a similar test file) to verify the migration.

Success Criteria

  • The Insert method successfully writes to BadgerDB using a write transaction.
  • Attempting to insert a duplicate resource returns an appropriate error.
  • Both the master key and the /1 version key are verifiable in the database after execution.
  • Test Case implementation: A unit test is provided that:
    • Initializes an in-memory BadgerDB instance.
    • Successfully inserts a new resource.
    • Asserts that both the primary and versioned keys exist with the correct data.
    • Confirms that a second insert with the same name/type fails.

Metadata

Metadata

Assignees

Labels

Type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions