Skip to content

Commit

Permalink
storage API for reading mod time of a declaration (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepeterson committed Aug 11, 2023
1 parent dfa390f commit b1e9cba
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions storage/file/declarations.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path"
"path/filepath"
"time"

"github.com/jessepeterson/kmfddm/ddm"
)
Expand Down Expand Up @@ -142,6 +143,13 @@ func (s *File) readDeclarationFile(declarationID string) (*ddm.Declaration, erro
return d, nil
}

// RetrieveDeclarationModTime retrieves the last modification time of the declaration.
// See also the storage package for documentation on the storage interfaces.
func (s *File) RetrieveDeclarationModTime(ctx context.Context, declarationID string) (time.Time, error) {
fi, err := os.Stat(s.declarationFilename(declarationID))
return fi.ModTime(), err
}

// DeleteDeclaration deletes a declaration by its ID.
// See also the storage package for documentation on the storage interfaces.
func (s *File) DeleteDeclaration(_ context.Context, identifier string) (bool, error) {
Expand Down
11 changes: 11 additions & 0 deletions storage/mysql/declarations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"strings"
"time"

"github.com/jessepeterson/kmfddm/ddm"
)
Expand Down Expand Up @@ -129,6 +130,16 @@ WHERE
return
}

// RetrieveDeclarationModTime retrieves the last modification time of the declaration.
// See also the storage package for documentation on the storage interfaces.
func (s *MySQLStorage) RetrieveDeclarationModTime(ctx context.Context, declarationID string) (time.Time, error) {
var dbTimestamp string
if err := s.db.QueryRowContext(ctx, "SELECT updated_at FROM declarations WHERE identifier = ? LIMIT 1;", declarationID).Scan(&dbTimestamp); err != nil {
return time.Time{}, err
}
return time.Parse(mysqlTimeFormat, dbTimestamp)
}

// DeleteDeclaration deletes a declaration and returns whether it was deleted or already existed.
// See also the storage package for documentation on the storage interfaces.
func (s *MySQLStorage) DeleteDeclaration(ctx context.Context, declarationID string) (bool, error) {
Expand Down
3 changes: 3 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package storage

import (
"context"
"time"

"github.com/jessepeterson/kmfddm/ddm"
)
Expand Down Expand Up @@ -33,6 +34,8 @@ type DeclarationDeleter interface {
type DeclarationAPIRetriever interface {
// RetrieveDeclaration retrieves a declaration from storage.
RetrieveDeclaration(ctx context.Context, declarationID string) (*ddm.Declaration, error)
// RetrieveDeclarationModTime retrieves the last modification time of the declaration.
RetrieveDeclarationModTime(ctx context.Context, declarationID string) (time.Time, error)
}

type EnrollmentIDRetriever interface {
Expand Down
7 changes: 7 additions & 0 deletions storage/test/declarations.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ func testStoreDeclaration(t *testing.T, storage storage.DeclarationAPIStorage, c
if have, want := decl2.Type, decl.Type; have != want {
t.Errorf("have %q; want %q", have, want)
}
modTime, err := storage.RetrieveDeclarationModTime(ctx, decl.Identifier)
if err != nil {
t.Fatal(err)
}
if modTime.IsZero() {
t.Error("declaration mod time is zero")
}
changed, err := storage.StoreDeclaration(ctx, decl2)
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit b1e9cba

Please sign in to comment.