Skip to content

Commit

Permalink
set instance should be automatic
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <[email protected]>
  • Loading branch information
wagoodman committed Sep 26, 2024
1 parent 3c3bb95 commit 6fd39e0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
15 changes: 13 additions & 2 deletions grype/db/v6/db_metadata_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package v6

import (
"fmt"
"time"

"github.com/anchore/grype/internal/log"
)

type DBMetadataStoreWriter interface {
SetDBMetadata(instance DBMetadata) error
SetDBMetadata() error
}

type DBMetadataStoreReader interface {
Expand All @@ -33,11 +34,21 @@ func (s *dbMetadataStore) GetDBMetadata() (*DBMetadata, error) {
return &model, result.Error
}

func (s *dbMetadataStore) SetDBMetadata(instance DBMetadata) error {
func (s *dbMetadataStore) SetDBMetadata() error {
log.Trace("writing DB metadata record")

if err := s.db.Unscoped().Where("true").Delete(&DBMetadata{}).Error; err != nil {
return fmt.Errorf("failed to delete existing DB metadata record: %w", err)
}

ts := time.Now().UTC()
instance := &DBMetadata{
BuildTimestamp: &ts,
Model: ModelVersion,
Revision: Revision,
Addition: Addition,
}

if err := s.db.Create(instance).Error; err != nil {
return fmt.Errorf("failed to create DB metadata record: %w", err)
}
Expand Down
49 changes: 26 additions & 23 deletions grype/db/v6/db_metadata_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,46 @@ package v6

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
)

func TestDbMetadataStore(t *testing.T) {
func TestDbMetadataStore_empty(t *testing.T) {
conn := setupTestDB(t)
s := newDBMetadataStore(*conn)

// set one record
ts := time.Date(2021, 11, 22, 33, 44, 55, 66, time.UTC)
expectedMetadata := DBMetadata{
BuildTimestamp: &ts,
Model: 66,
Revision: 77,
Addition: 88,
}
err := s.SetDBMetadata(expectedMetadata)
require.NoError(t, err)
// attempt to fetch a non-existent record
actualMetadata, err := s.GetDBMetadata()
require.ErrorIs(t, err, gorm.ErrRecordNotFound)
require.NotNil(t, actualMetadata)
}

// overwrite with a new record
ts = time.Date(2022, 2, 3, 4, 5, 6, 7, time.UTC)
expectedMetadata = DBMetadata{
BuildTimestamp: &ts,
Model: 6,
Revision: 9,
Addition: 10,
}
err = s.SetDBMetadata(expectedMetadata)
require.NoError(t, err)
func TestDbMetadataStore(t *testing.T) {
conn := setupTestDB(t)
s := newDBMetadataStore(*conn)

require.NoError(t, s.SetDBMetadata())

// fetch the record
actualMetadata, err := s.GetDBMetadata()
require.NoError(t, err)
require.NotNil(t, actualMetadata)
assert.Equal(t, expectedMetadata, *actualMetadata)

assert.NotZero(t, *actualMetadata.BuildTimestamp) // a timestamp was set
name, _ := actualMetadata.BuildTimestamp.Zone()
assert.Equal(t, "UTC", name) // the timestamp is in UTC

actualMetadata.BuildTimestamp = nil // value not under test

assert.Equal(t, DBMetadata{
BuildTimestamp: nil,
// expect the correct version info
Model: ModelVersion,
Revision: Revision,
Addition: Addition,
}, *actualMetadata)
}

func setupTestDB(t *testing.T) *connection {
Expand Down

0 comments on commit 6fd39e0

Please sign in to comment.