refactor(postgres): resources and statestore tests use storecontract, not internal/store
This commit is contained in:
2 files changed
+29
-29
No files matched your search
@@ -9,8 +9,8 @@ import (
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
)
|
||||
|
||||
// Resources is the PostgreSQL implementation of the State Store's
|
||||
@@ -24,18 +24,18 @@ func NewResources(pool *pgxpool.Pool) *Resources {
|
||||
return &Resources{pool: pool}
|
||||
}
|
||||
|
||||
func (s *Resources) Resource(ctx context.Context, kind string) (store.Resource, error) {
|
||||
func (s *Resources) Resource(ctx context.Context, kind string) (storecontract.Resource, error) {
|
||||
resource, err := postgresdb.New(s.pool).ResourceByKind(ctx, kind)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return store.Resource{}, store.ErrResourceNotFound
|
||||
return storecontract.Resource{}, storecontract.ErrResourceNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return store.Resource{}, err
|
||||
return storecontract.Resource{}, err
|
||||
}
|
||||
return resourceFromCurrent(resource), nil
|
||||
}
|
||||
|
||||
func (s *Resources) ResourceHistory(ctx context.Context, kind string, limit int) ([]store.Resource, error) {
|
||||
func (s *Resources) ResourceHistory(ctx context.Context, kind string, limit int) ([]storecontract.Resource, error) {
|
||||
if limit <= 0 {
|
||||
limit = 0
|
||||
}
|
||||
@@ -45,7 +45,7 @@ func (s *Resources) ResourceHistory(ctx context.Context, kind string, limit int)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resources := make([]store.Resource, 0, len(history))
|
||||
resources := make([]storecontract.Resource, 0, len(history))
|
||||
for _, entry := range history {
|
||||
resources = append(resources, resourceFromAudit(
|
||||
entry.Kind, entry.Value, entry.Version, entry.Actor, entry.Source,
|
||||
@@ -55,15 +55,15 @@ func (s *Resources) ResourceHistory(ctx context.Context, kind string, limit int)
|
||||
return resources, nil
|
||||
}
|
||||
|
||||
func (s *Resources) PutResource(ctx context.Context, write store.ResourceWrite) (_ store.Resource, retErr error) {
|
||||
func (s *Resources) PutResource(ctx context.Context, write storecontract.ResourceWrite) (_ storecontract.Resource, retErr error) {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return store.Resource{}, err
|
||||
return storecontract.Resource{}, err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
queries := postgresdb.New(tx)
|
||||
if err := queries.LockResourceWrite(ctx, write.Kind); err != nil {
|
||||
return store.Resource{}, err
|
||||
return storecontract.Resource{}, err
|
||||
}
|
||||
if existing, err := queries.ResourceByRequest(ctx, postgresdb.ResourceByRequestParams{
|
||||
Kind: write.Kind, RequestID: write.RequestID,
|
||||
@@ -73,17 +73,17 @@ func (s *Resources) PutResource(ctx context.Context, write store.ResourceWrite)
|
||||
existing.RequestID, existing.ExpectedVersion, existing.CurrentVersion, existing.At,
|
||||
), nil
|
||||
} else if !errors.Is(err, pgx.ErrNoRows) {
|
||||
return store.Resource{}, err
|
||||
return storecontract.Resource{}, err
|
||||
}
|
||||
|
||||
current, err := queries.ResourceVersion(ctx, write.Kind)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
current = 0
|
||||
} else if err != nil {
|
||||
return store.Resource{}, err
|
||||
return storecontract.Resource{}, err
|
||||
}
|
||||
if current != write.ExpectedVersion {
|
||||
return store.Resource{}, fmt.Errorf("%w: expected %d, current %d", store.ErrResourceVersionConflict, write.ExpectedVersion, current)
|
||||
return storecontract.Resource{}, fmt.Errorf("%w: expected %d, current %d", storecontract.ErrResourceVersionConflict, write.ExpectedVersion, current)
|
||||
}
|
||||
if write.At.IsZero() {
|
||||
write.At = time.Now().UTC()
|
||||
@@ -102,7 +102,7 @@ func (s *Resources) PutResource(ctx context.Context, write store.ResourceWrite)
|
||||
})
|
||||
}
|
||||
if err != nil {
|
||||
return store.Resource{}, err
|
||||
return storecontract.Resource{}, err
|
||||
}
|
||||
history, err := queries.InsertResourceHistory(ctx, postgresdb.InsertResourceHistoryParams{
|
||||
Kind: write.Kind, Value: write.Value, Version: resource.Version, Actor: write.Actor,
|
||||
@@ -110,10 +110,10 @@ func (s *Resources) PutResource(ctx context.Context, write store.ResourceWrite)
|
||||
CurrentVersion: current, At: write.At,
|
||||
})
|
||||
if err != nil {
|
||||
return store.Resource{}, err
|
||||
return storecontract.Resource{}, err
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return store.Resource{}, err
|
||||
return storecontract.Resource{}, err
|
||||
}
|
||||
return resourceFromAudit(
|
||||
history.Kind, history.Value, history.Version, history.Actor, history.Source,
|
||||
@@ -121,8 +121,8 @@ func (s *Resources) PutResource(ctx context.Context, write store.ResourceWrite)
|
||||
), nil
|
||||
}
|
||||
|
||||
func resourceFromCurrent(resource postgresdb.Resource) store.Resource {
|
||||
return store.Resource{
|
||||
func resourceFromCurrent(resource postgresdb.Resource) storecontract.Resource {
|
||||
return storecontract.Resource{
|
||||
Kind: resource.Kind, Value: resource.Value, Version: resource.Version, At: resource.UpdatedAt,
|
||||
}
|
||||
}
|
||||
@@ -134,8 +134,8 @@ func resourceFromAudit(
|
||||
actor, source, requestID string,
|
||||
expectedVersion, currentVersion int64,
|
||||
at time.Time,
|
||||
) store.Resource {
|
||||
return store.Resource{
|
||||
) storecontract.Resource {
|
||||
return storecontract.Resource{
|
||||
Kind: kind, Value: value, Version: version, Actor: actor, Source: source,
|
||||
RequestID: requestID, ExpectedVersion: expectedVersion, CurrentVersion: currentVersion, At: at,
|
||||
}
|
||||
|
||||
@@ -8,9 +8,9 @@ import (
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
)
|
||||
|
||||
// syntheticIssueBase mirrors the store's chat issue-number seed. It is the
|
||||
@@ -176,7 +176,7 @@ func TestClaimSkipsARowAnotherTransactionHolds(t *testing.T) {
|
||||
func TestResourceWritesPreserveOptimisticAndIdempotentSemantics(t *testing.T) {
|
||||
resources := resourcesFor(t)
|
||||
firstAt := time.Date(2026, 9, 24, 1, 0, 0, 0, time.UTC)
|
||||
first, err := resources.PutResource(t.Context(), store.ResourceWrite{
|
||||
first, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
Kind: "settings", Value: []byte(`{"v":1}`), Actor: "operator", Source: "archie-ui",
|
||||
RequestID: "r1", ExpectedVersion: 0, At: firstAt,
|
||||
})
|
||||
@@ -187,7 +187,7 @@ func TestResourceWritesPreserveOptimisticAndIdempotentSemantics(t *testing.T) {
|
||||
t.Fatalf("first write = %+v, want version 1 from version 0", first)
|
||||
}
|
||||
|
||||
replay, err := resources.PutResource(t.Context(), store.ResourceWrite{
|
||||
replay, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
Kind: "settings", Value: []byte(`{"v":999}`), Actor: "other", Source: "retry",
|
||||
RequestID: "r1", ExpectedVersion: 0, At: firstAt.Add(time.Minute),
|
||||
})
|
||||
@@ -198,14 +198,14 @@ func TestResourceWritesPreserveOptimisticAndIdempotentSemantics(t *testing.T) {
|
||||
t.Fatalf("replay = %+v, want original %+v", replay, first)
|
||||
}
|
||||
|
||||
if _, err := resources.PutResource(t.Context(), store.ResourceWrite{
|
||||
if _, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
Kind: "settings", Value: []byte(`{"v":2}`), Actor: "operator", Source: "archie-ui",
|
||||
RequestID: "r2", ExpectedVersion: 0, At: firstAt.Add(2 * time.Minute),
|
||||
}); !errors.Is(err, store.ErrResourceVersionConflict) {
|
||||
}); !errors.Is(err, storecontract.ErrResourceVersionConflict) {
|
||||
t.Fatalf("stale PutResource = %v, want ErrResourceVersionConflict", err)
|
||||
}
|
||||
|
||||
second, err := resources.PutResource(t.Context(), store.ResourceWrite{
|
||||
second, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
Kind: "settings", Value: []byte(`{"v":2}`), Actor: "operator", Source: "messaging",
|
||||
RequestID: "r2", ExpectedVersion: 1, At: firstAt.Add(3 * time.Minute),
|
||||
})
|
||||
@@ -215,7 +215,7 @@ func TestResourceWritesPreserveOptimisticAndIdempotentSemantics(t *testing.T) {
|
||||
if second.Version != 2 || second.CurrentVersion != 1 {
|
||||
t.Fatalf("second write = %+v, want version 2 from version 1", second)
|
||||
}
|
||||
lateReplay, err := resources.PutResource(t.Context(), store.ResourceWrite{
|
||||
lateReplay, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
Kind: "settings", Value: []byte(`{"v":999}`), Actor: "other", Source: "retry",
|
||||
RequestID: "r1", ExpectedVersion: 0, At: firstAt.Add(4 * time.Minute),
|
||||
})
|
||||
@@ -247,7 +247,7 @@ func TestResourceWritesPreserveOptimisticAndIdempotentSemantics(t *testing.T) {
|
||||
if len(history) != 2 {
|
||||
t.Fatalf("history after late replay = %+v, want two revisions", history)
|
||||
}
|
||||
if _, err := resources.Resource(t.Context(), "missing"); !errors.Is(err, store.ErrResourceNotFound) {
|
||||
if _, err := resources.Resource(t.Context(), "missing"); !errors.Is(err, storecontract.ErrResourceNotFound) {
|
||||
t.Fatalf("missing Resource = %v, want ErrResourceNotFound", err)
|
||||
}
|
||||
}
|
||||
@@ -263,7 +263,7 @@ func TestConcurrentResourceWritesAcceptOnlyOneExpectedVersion(t *testing.T) {
|
||||
for _, requestID := range []string{"first", "second"} {
|
||||
go func() {
|
||||
<-start
|
||||
_, err := resources.PutResource(t.Context(), store.ResourceWrite{
|
||||
_, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
Kind: "settings", Value: []byte(`{"v":1}`), Actor: "operator", Source: "test",
|
||||
RequestID: requestID, ExpectedVersion: 0,
|
||||
})
|
||||
@@ -278,7 +278,7 @@ func TestConcurrentResourceWritesAcceptOnlyOneExpectedVersion(t *testing.T) {
|
||||
switch {
|
||||
case outcome.err == nil:
|
||||
succeeded++
|
||||
case errors.Is(outcome.err, store.ErrResourceVersionConflict):
|
||||
case errors.Is(outcome.err, storecontract.ErrResourceVersionConflict):
|
||||
conflicted++
|
||||
default:
|
||||
t.Fatalf("concurrent PutResource: %v", outcome.err)
|
||||
|
||||
Reference in new issue
Block a user