Refactor feedkit boundaries ahead of v1
Remove global Postgres schema registration in favor of explicit schema-aware sink factory wiring, and update weatherfeeder to register the Postgres sink explicitly. Add optional per-source HTTP timeout and response body limit overrides while keeping feedkit defaults. Remove remaining legacy source/config compatibility surfaces, including singular kind support and old source registry/type aliases, and migrate weatherfeeder sources to plural `Kinds()` metadata. Clean up related docs, tests, and sample config to match the new Postgres, HTTP, and NATS configuration model.
This commit is contained in:
@@ -12,11 +12,6 @@ func RegisterBuiltins(r *Registry) {
|
||||
return NewStdoutSink(cfg.Name), nil
|
||||
})
|
||||
|
||||
// Postgres sink: persists events durably.
|
||||
r.Register("postgres", func(cfg config.SinkConfig) (Sink, error) {
|
||||
return NewPostgresSinkFromConfig(cfg)
|
||||
})
|
||||
|
||||
// NATS sink: publishes events to a broker for downstream consumers.
|
||||
r.Register("nats", func(cfg config.SinkConfig) (Sink, error) {
|
||||
return NewNATSSinkFromConfig(cfg)
|
||||
|
||||
14
sinks/doc.go
14
sinks/doc.go
@@ -4,16 +4,16 @@
|
||||
// External API surface:
|
||||
// - Sink: adapter interface that consumes event.Event values
|
||||
// - Registry / NewRegistry: named sink factory registry
|
||||
// - RegisterBuiltins: registers the built-in sink drivers in this binary
|
||||
// - RegisterBuiltins: registers the schema-free built-in sink drivers
|
||||
//
|
||||
// Built-in drivers:
|
||||
// Built-in sink implementations:
|
||||
// - stdout
|
||||
// - nats
|
||||
// - postgres
|
||||
//
|
||||
// Optional helpers from helpers.go:
|
||||
// - RegisterPostgresSchemaForConfiguredSinks: registers one Postgres schema
|
||||
// for each configured sink using driver=postgres
|
||||
// - PostgresFactory: returns a sink factory for the built-in Postgres sink
|
||||
// using a provided downstream schema
|
||||
//
|
||||
// # NATS built-in overview
|
||||
//
|
||||
@@ -59,7 +59,9 @@
|
||||
//
|
||||
// Example downstream wiring:
|
||||
//
|
||||
// sinks.MustRegisterPostgresSchema("pg_main", sinks.PostgresSchema{
|
||||
// sinkReg := sinks.NewRegistry()
|
||||
// sinks.RegisterBuiltins(sinkReg)
|
||||
// sinkReg.Register("postgres", sinks.PostgresFactory(sinks.PostgresSchema{
|
||||
// Tables: []sinks.PostgresTable{
|
||||
// {
|
||||
// Name: "events",
|
||||
@@ -88,7 +90,7 @@
|
||||
// },
|
||||
// }, nil
|
||||
// },
|
||||
// })
|
||||
// }))
|
||||
//
|
||||
// Manual pruning via type assertion (administrative helpers):
|
||||
//
|
||||
|
||||
@@ -26,21 +26,10 @@ func requireStringParam(cfg config.SinkConfig, key string) (string, error) {
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// RegisterPostgresSchemaForConfiguredSinks registers one Postgres schema for each
|
||||
// configured sink using driver=postgres.
|
||||
func RegisterPostgresSchemaForConfiguredSinks(cfg *config.Config, schema PostgresSchema) error {
|
||||
if cfg == nil {
|
||||
return fmt.Errorf("register postgres schemas: config is nil")
|
||||
// PostgresFactory returns a sink factory that builds the built-in Postgres sink
|
||||
// using the provided downstream schema definition.
|
||||
func PostgresFactory(schema PostgresSchema) Factory {
|
||||
return func(cfg config.SinkConfig) (Sink, error) {
|
||||
return NewPostgresSinkFromConfig(cfg, schema)
|
||||
}
|
||||
|
||||
for i, sk := range cfg.Sinks {
|
||||
if !strings.EqualFold(strings.TrimSpace(sk.Driver), "postgres") {
|
||||
continue
|
||||
}
|
||||
if err := RegisterPostgresSchema(sk.Name, schema); err != nil {
|
||||
return fmt.Errorf("register postgres schema for sinks[%d] name=%q: %w", i, sk.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,55 +2,19 @@ package sinks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/feedkit/config"
|
||||
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||
)
|
||||
|
||||
func TestRegisterPostgresSchemaForConfiguredSinksNilConfig(t *testing.T) {
|
||||
err := RegisterPostgresSchemaForConfiguredSinks(nil, testPostgresSchema())
|
||||
if err == nil {
|
||||
t.Fatalf("RegisterPostgresSchemaForConfiguredSinks(nil) expected error")
|
||||
func TestPostgresFactoryReturnsWorkingFactory(t *testing.T) {
|
||||
factory := PostgresFactory(testPostgresSchema())
|
||||
if factory == nil {
|
||||
t.Fatalf("PostgresFactory() returned nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "config is nil") {
|
||||
t.Fatalf("error = %q, want config is nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterPostgresSchemaForConfiguredSinksNonPostgresNoOp(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Sinks: []config.SinkConfig{
|
||||
{Name: uniqueSinkName("stdout"), Driver: "stdout"},
|
||||
{Name: uniqueSinkName("nats"), Driver: "nats"},
|
||||
},
|
||||
}
|
||||
|
||||
if err := RegisterPostgresSchemaForConfiguredSinks(cfg, testPostgresSchema()); err != nil {
|
||||
t.Fatalf("RegisterPostgresSchemaForConfiguredSinks(non-postgres) error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterPostgresSchemaForConfiguredSinksDuplicateRegistrationFails(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Sinks: []config.SinkConfig{
|
||||
{Name: uniqueSinkName("pg"), Driver: "postgres"},
|
||||
},
|
||||
}
|
||||
|
||||
if err := RegisterPostgresSchemaForConfiguredSinks(cfg, testPostgresSchema()); err != nil {
|
||||
t.Fatalf("first RegisterPostgresSchemaForConfiguredSinks() error = %v", err)
|
||||
}
|
||||
|
||||
err := RegisterPostgresSchemaForConfiguredSinks(cfg, testPostgresSchema())
|
||||
if err == nil {
|
||||
t.Fatalf("second RegisterPostgresSchemaForConfiguredSinks() expected duplicate error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "already registered") {
|
||||
t.Fatalf("error = %q, want already registered", err)
|
||||
if _, err := factory(config.SinkConfig{}); err == nil {
|
||||
t.Fatalf("factory(config) expected parameter validation error")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +44,3 @@ func testPostgresSchema() PostgresSchema {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func uniqueSinkName(prefix string) string {
|
||||
return fmt.Sprintf("%s_%d", prefix, time.Now().UnixNano())
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ type PostgresSink struct {
|
||||
pruneWindow time.Duration
|
||||
}
|
||||
|
||||
func NewPostgresSinkFromConfig(cfg config.SinkConfig) (Sink, error) {
|
||||
func NewPostgresSinkFromConfig(cfg config.SinkConfig, schemaDef PostgresSchema) (Sink, error) {
|
||||
uri, err := requireStringParam(cfg, "uri")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -106,9 +106,9 @@ func NewPostgresSinkFromConfig(cfg config.SinkConfig) (Sink, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
schema, ok := lookupPostgresSchema(cfg.Name)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("postgres sink %q: no schema registered (call sinks.RegisterPostgresSchema before building sinks)", cfg.Name)
|
||||
schema, err := compilePostgresSchema(schemaDef)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("postgres sink %q: compile schema: %w", cfg.Name, err)
|
||||
}
|
||||
|
||||
dsn, err := buildPostgresDSN(uri, username, password)
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||
@@ -72,51 +71,6 @@ type postgresTableCompiled struct {
|
||||
indexes []PostgresIndex
|
||||
}
|
||||
|
||||
var (
|
||||
postgresSchemaRegistryMu sync.RWMutex
|
||||
postgresSchemaRegistry = map[string]postgresSchemaCompiled{}
|
||||
)
|
||||
|
||||
// RegisterPostgresSchema registers one downstream schema by sink name.
|
||||
//
|
||||
// This should be called by downstream daemon wiring code before sink
|
||||
// construction. Duplicate sink-name registrations are rejected.
|
||||
func RegisterPostgresSchema(sinkName string, schema PostgresSchema) error {
|
||||
sinkName = strings.TrimSpace(sinkName)
|
||||
if sinkName == "" {
|
||||
return fmt.Errorf("postgres schema: sink name cannot be empty")
|
||||
}
|
||||
|
||||
compiled, err := compilePostgresSchema(schema)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
postgresSchemaRegistryMu.Lock()
|
||||
defer postgresSchemaRegistryMu.Unlock()
|
||||
|
||||
if _, exists := postgresSchemaRegistry[sinkName]; exists {
|
||||
return fmt.Errorf("postgres schema: sink %q already registered", sinkName)
|
||||
}
|
||||
|
||||
postgresSchemaRegistry[sinkName] = compiled
|
||||
return nil
|
||||
}
|
||||
|
||||
func MustRegisterPostgresSchema(sinkName string, schema PostgresSchema) {
|
||||
if err := RegisterPostgresSchema(sinkName, schema); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func lookupPostgresSchema(sinkName string) (postgresSchemaCompiled, bool) {
|
||||
postgresSchemaRegistryMu.RLock()
|
||||
defer postgresSchemaRegistryMu.RUnlock()
|
||||
|
||||
s, ok := postgresSchemaRegistry[sinkName]
|
||||
return s, ok
|
||||
}
|
||||
|
||||
func compilePostgresSchema(schema PostgresSchema) (postgresSchemaCompiled, error) {
|
||||
if schema.MapEvent == nil {
|
||||
return postgresSchemaCompiled{}, fmt.Errorf("postgres schema: map function is required")
|
||||
|
||||
@@ -96,20 +96,12 @@ func (d *fakeDB) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func resetPostgresSchemaRegistryForTest() {
|
||||
postgresSchemaRegistryMu.Lock()
|
||||
defer postgresSchemaRegistryMu.Unlock()
|
||||
postgresSchemaRegistry = map[string]postgresSchemaCompiled{}
|
||||
}
|
||||
|
||||
func withPostgresTestState(t *testing.T) {
|
||||
t.Helper()
|
||||
|
||||
resetPostgresSchemaRegistryForTest()
|
||||
oldOpen := openPostgresDB
|
||||
t.Cleanup(func() {
|
||||
openPostgresDB = oldOpen
|
||||
resetPostgresSchemaRegistryForTest()
|
||||
})
|
||||
}
|
||||
|
||||
@@ -183,35 +175,8 @@ func mustCompileSchema(t *testing.T, s PostgresSchema) postgresSchemaCompiled {
|
||||
return compiled
|
||||
}
|
||||
|
||||
func TestRegisterPostgresSchema(t *testing.T) {
|
||||
withPostgresTestState(t)
|
||||
|
||||
err := RegisterPostgresSchema("pg", schemaOneTable(func(_ context.Context, _ event.Event) ([]PostgresWrite, error) {
|
||||
return nil, nil
|
||||
}))
|
||||
if err != nil {
|
||||
t.Fatalf("register schema: %v", err)
|
||||
}
|
||||
|
||||
if _, ok := lookupPostgresSchema("pg"); !ok {
|
||||
t.Fatalf("expected schema registration")
|
||||
}
|
||||
|
||||
err = RegisterPostgresSchema("pg", schemaOneTable(func(_ context.Context, _ event.Event) ([]PostgresWrite, error) {
|
||||
return nil, nil
|
||||
}))
|
||||
if err == nil {
|
||||
t.Fatalf("expected duplicate registration error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "already registered") {
|
||||
t.Fatalf("unexpected duplicate error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterPostgresSchema_RejectsInvalidSchema(t *testing.T) {
|
||||
withPostgresTestState(t)
|
||||
|
||||
err := RegisterPostgresSchema("pg", PostgresSchema{
|
||||
func TestCompilePostgresSchemaRejectsInvalidSchema(t *testing.T) {
|
||||
_, err := compilePostgresSchema(PostgresSchema{
|
||||
Tables: []PostgresTable{
|
||||
{
|
||||
Name: "events",
|
||||
@@ -230,7 +195,7 @@ func TestRegisterPostgresSchema_RejectsInvalidSchema(t *testing.T) {
|
||||
t.Fatalf("unexpected schema validation error: %v", err)
|
||||
}
|
||||
|
||||
err = RegisterPostgresSchema("pg2", PostgresSchema{
|
||||
_, err = compilePostgresSchema(PostgresSchema{
|
||||
Tables: []PostgresTable{
|
||||
{
|
||||
Name: "events",
|
||||
@@ -254,7 +219,50 @@ func TestRegisterPostgresSchema_RejectsInvalidSchema(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewPostgresSinkFromConfig_MissingParams(t *testing.T) {
|
||||
func TestPostgresFactoryBuildsMultipleSinksWithSameSchema(t *testing.T) {
|
||||
withPostgresTestState(t)
|
||||
|
||||
dbs := []*fakeDB{{}, {}}
|
||||
var gotDSNs []string
|
||||
openPostgresDB = func(dsn string) (postgresDB, error) {
|
||||
gotDSNs = append(gotDSNs, dsn)
|
||||
db := dbs[len(gotDSNs)-1]
|
||||
return db, nil
|
||||
}
|
||||
|
||||
factory := PostgresFactory(schemaOneTable(func(_ context.Context, _ event.Event) ([]PostgresWrite, error) {
|
||||
return nil, nil
|
||||
}))
|
||||
|
||||
for _, name := range []string{"pg_a", "pg_b"} {
|
||||
sink, err := factory(config.SinkConfig{
|
||||
Name: name,
|
||||
Driver: "postgres",
|
||||
Params: map[string]any{
|
||||
"uri": "postgres://localhost/db",
|
||||
"username": "user",
|
||||
"password": "pass",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("factory(%q) error = %v", name, err)
|
||||
}
|
||||
if sink == nil {
|
||||
t.Fatalf("factory(%q) returned nil sink", name)
|
||||
}
|
||||
}
|
||||
|
||||
if len(gotDSNs) != 2 {
|
||||
t.Fatalf("len(gotDSNs) = %d, want 2", len(gotDSNs))
|
||||
}
|
||||
for i, db := range dbs {
|
||||
if db.pingCalls != 1 {
|
||||
t.Fatalf("db[%d] pingCalls = %d, want 1", i, db.pingCalls)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewPostgresSinkFromConfigMissingParams(t *testing.T) {
|
||||
withPostgresTestState(t)
|
||||
|
||||
tests := []struct {
|
||||
@@ -273,7 +281,7 @@ func TestNewPostgresSinkFromConfig_MissingParams(t *testing.T) {
|
||||
Name: "pg",
|
||||
Driver: "postgres",
|
||||
Params: tc.params,
|
||||
})
|
||||
}, schemaOneTable(func(_ context.Context, _ event.Event) ([]PostgresWrite, error) { return nil, nil }))
|
||||
if err == nil {
|
||||
t.Fatalf("expected error")
|
||||
}
|
||||
@@ -284,7 +292,7 @@ func TestNewPostgresSinkFromConfig_MissingParams(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewPostgresSinkFromConfig_MissingSchemaRegistration(t *testing.T) {
|
||||
func TestNewPostgresSinkFromConfigRejectsInvalidSchema(t *testing.T) {
|
||||
withPostgresTestState(t)
|
||||
|
||||
_, err := NewPostgresSinkFromConfig(config.SinkConfig{
|
||||
@@ -295,25 +303,29 @@ func TestNewPostgresSinkFromConfig_MissingSchemaRegistration(t *testing.T) {
|
||||
"username": "user",
|
||||
"password": "pass",
|
||||
},
|
||||
}, PostgresSchema{
|
||||
Tables: []PostgresTable{
|
||||
{
|
||||
Name: "events",
|
||||
Columns: []PostgresColumn{
|
||||
{Name: "id", Type: "TEXT", Nullable: false},
|
||||
},
|
||||
PruneColumn: "missing_col",
|
||||
},
|
||||
},
|
||||
MapEvent: func(_ context.Context, _ event.Event) ([]PostgresWrite, error) { return nil, nil },
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatalf("expected error")
|
||||
t.Fatalf("expected invalid schema error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "no schema registered") {
|
||||
if !strings.Contains(err.Error(), "compile schema") {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewPostgresSinkFromConfig_EagerInit(t *testing.T) {
|
||||
func TestNewPostgresSinkFromConfigEagerInit(t *testing.T) {
|
||||
withPostgresTestState(t)
|
||||
|
||||
err := RegisterPostgresSchema("pg", schemaOneTable(func(_ context.Context, _ event.Event) ([]PostgresWrite, error) {
|
||||
return nil, nil
|
||||
}))
|
||||
if err != nil {
|
||||
t.Fatalf("register schema: %v", err)
|
||||
}
|
||||
|
||||
db := &fakeDB{}
|
||||
var gotDSN string
|
||||
openPostgresDB = func(dsn string) (postgresDB, error) {
|
||||
@@ -329,7 +341,7 @@ func TestNewPostgresSinkFromConfig_EagerInit(t *testing.T) {
|
||||
"username": "app_user",
|
||||
"password": "app_pass",
|
||||
},
|
||||
})
|
||||
}, schemaOneTable(func(_ context.Context, _ event.Event) ([]PostgresWrite, error) { return nil, nil }))
|
||||
if err != nil {
|
||||
t.Fatalf("new postgres sink: %v", err)
|
||||
}
|
||||
@@ -363,22 +375,15 @@ func TestNewPostgresSinkFromConfig_EagerInit(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewPostgresSinkFromConfig_InitFailureClosesDB(t *testing.T) {
|
||||
func TestNewPostgresSinkFromConfigInitFailureClosesDB(t *testing.T) {
|
||||
withPostgresTestState(t)
|
||||
|
||||
err := RegisterPostgresSchema("pg", schemaOneTable(func(_ context.Context, _ event.Event) ([]PostgresWrite, error) {
|
||||
return nil, nil
|
||||
}))
|
||||
if err != nil {
|
||||
t.Fatalf("register schema: %v", err)
|
||||
}
|
||||
|
||||
db := &fakeDB{execErrOnCall: 1, execErr: errors.New("ddl failed")}
|
||||
openPostgresDB = func(_ string) (postgresDB, error) {
|
||||
return db, nil
|
||||
}
|
||||
|
||||
_, err = NewPostgresSinkFromConfig(config.SinkConfig{
|
||||
_, err := NewPostgresSinkFromConfig(config.SinkConfig{
|
||||
Name: "pg",
|
||||
Driver: "postgres",
|
||||
Params: map[string]any{
|
||||
@@ -386,7 +391,7 @@ func TestNewPostgresSinkFromConfig_InitFailureClosesDB(t *testing.T) {
|
||||
"username": "user",
|
||||
"password": "pass",
|
||||
},
|
||||
})
|
||||
}, schemaOneTable(func(_ context.Context, _ event.Event) ([]PostgresWrite, error) { return nil, nil }))
|
||||
if err == nil {
|
||||
t.Fatalf("expected init error")
|
||||
}
|
||||
@@ -395,7 +400,7 @@ func TestNewPostgresSinkFromConfig_InitFailureClosesDB(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewPostgresSinkFromConfig_PruneParamAccepted(t *testing.T) {
|
||||
func TestNewPostgresSinkFromConfigPruneParamAccepted(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
in string
|
||||
@@ -410,13 +415,6 @@ func TestNewPostgresSinkFromConfig_PruneParamAccepted(t *testing.T) {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
withPostgresTestState(t)
|
||||
|
||||
err := RegisterPostgresSchema("pg", schemaOneTable(func(_ context.Context, _ event.Event) ([]PostgresWrite, error) {
|
||||
return nil, nil
|
||||
}))
|
||||
if err != nil {
|
||||
t.Fatalf("register schema: %v", err)
|
||||
}
|
||||
|
||||
openPostgresDB = func(_ string) (postgresDB, error) {
|
||||
return &fakeDB{}, nil
|
||||
}
|
||||
@@ -430,7 +428,7 @@ func TestNewPostgresSinkFromConfig_PruneParamAccepted(t *testing.T) {
|
||||
"password": "pass",
|
||||
"prune": tc.in,
|
||||
},
|
||||
})
|
||||
}, schemaOneTable(func(_ context.Context, _ event.Event) ([]PostgresWrite, error) { return nil, nil }))
|
||||
if err != nil {
|
||||
t.Fatalf("new postgres sink: %v", err)
|
||||
}
|
||||
@@ -446,7 +444,7 @@ func TestNewPostgresSinkFromConfig_PruneParamAccepted(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewPostgresSinkFromConfig_PruneParamRejected(t *testing.T) {
|
||||
func TestNewPostgresSinkFromConfigPruneParamRejected(t *testing.T) {
|
||||
withPostgresTestState(t)
|
||||
|
||||
tests := []struct {
|
||||
@@ -472,7 +470,7 @@ func TestNewPostgresSinkFromConfig_PruneParamRejected(t *testing.T) {
|
||||
"password": "pass",
|
||||
"prune": tc.in,
|
||||
},
|
||||
})
|
||||
}, schemaOneTable(func(_ context.Context, _ event.Event) ([]PostgresWrite, error) { return nil, nil }))
|
||||
if err == nil {
|
||||
t.Fatalf("expected error")
|
||||
}
|
||||
@@ -483,7 +481,7 @@ func TestNewPostgresSinkFromConfig_PruneParamRejected(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresSinkConsume_InvalidEvent(t *testing.T) {
|
||||
func TestPostgresSinkConsumeInvalidEvent(t *testing.T) {
|
||||
db := &fakeDB{}
|
||||
called := 0
|
||||
sink := &PostgresSink{
|
||||
@@ -507,7 +505,7 @@ func TestPostgresSinkConsume_InvalidEvent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresSinkConsume_UnmappedEventIsNoOp(t *testing.T) {
|
||||
func TestPostgresSinkConsumeUnmappedEventIsNoOp(t *testing.T) {
|
||||
db := &fakeDB{}
|
||||
sink := &PostgresSink{
|
||||
name: "pg",
|
||||
@@ -525,7 +523,7 @@ func TestPostgresSinkConsume_UnmappedEventIsNoOp(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresSinkConsume_OneEventWritesMultipleTablesAtomically(t *testing.T) {
|
||||
func TestPostgresSinkConsumeOneEventWritesMultipleTablesAtomically(t *testing.T) {
|
||||
tx := &fakeTx{}
|
||||
db := &fakeDB{tx: tx}
|
||||
sink := &PostgresSink{
|
||||
@@ -556,7 +554,7 @@ func TestPostgresSinkConsume_OneEventWritesMultipleTablesAtomically(t *testing.T
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresSinkConsume_InsertFailureRollsBack(t *testing.T) {
|
||||
func TestPostgresSinkConsumeInsertFailureRollsBack(t *testing.T) {
|
||||
tx := &fakeTx{execErrOnCall: 2, execErr: errors.New("duplicate key")}
|
||||
db := &fakeDB{tx: tx}
|
||||
sink := &PostgresSink{
|
||||
@@ -585,13 +583,13 @@ func TestPostgresSinkConsume_InsertFailureRollsBack(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresSinkConsume_AutoPruneRunsInSameTransaction(t *testing.T) {
|
||||
func TestPostgresSinkConsumeAutoPruneRunsInSameTransaction(t *testing.T) {
|
||||
tx := &fakeTx{}
|
||||
db := &fakeDB{tx: tx}
|
||||
sink := &PostgresSink{
|
||||
name: "pg",
|
||||
db: db,
|
||||
schema: mustCompileSchema(t, schemaTwoTables(func(_ context.Context, e event.Event) ([]PostgresWrite, error) {
|
||||
name: "pg",
|
||||
db: db,
|
||||
schema: mustCompileSchema(t, schemaTwoTables(func(_ context.Context, e event.Event) ([]PostgresWrite, error) {
|
||||
return []PostgresWrite{
|
||||
{Table: "events", Values: map[string]any{"event_id": e.ID, "emitted_at": e.EmittedAt}},
|
||||
{Table: "event_payloads", Values: map[string]any{"event_id": e.ID, "payload_json": `{}`, "emitted_at": e.EmittedAt}},
|
||||
@@ -620,13 +618,13 @@ func TestPostgresSinkConsume_AutoPruneRunsInSameTransaction(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresSinkConsume_AutoPruneFailureRollsBack(t *testing.T) {
|
||||
func TestPostgresSinkConsumeAutoPruneFailureRollsBack(t *testing.T) {
|
||||
tx := &fakeTx{execErrOnCall: 3, execErr: errors.New("prune failed")}
|
||||
db := &fakeDB{tx: tx}
|
||||
sink := &PostgresSink{
|
||||
name: "pg",
|
||||
db: db,
|
||||
schema: mustCompileSchema(t, schemaTwoTables(func(_ context.Context, e event.Event) ([]PostgresWrite, error) {
|
||||
name: "pg",
|
||||
db: db,
|
||||
schema: mustCompileSchema(t, schemaTwoTables(func(_ context.Context, e event.Event) ([]PostgresWrite, error) {
|
||||
return []PostgresWrite{
|
||||
{Table: "events", Values: map[string]any{"event_id": e.ID, "emitted_at": e.EmittedAt}},
|
||||
{Table: "event_payloads", Values: map[string]any{"event_id": e.ID, "payload_json": `{}`, "emitted_at": e.EmittedAt}},
|
||||
@@ -650,7 +648,7 @@ func TestPostgresSinkConsume_AutoPruneFailureRollsBack(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresSinkPrune_PerTable(t *testing.T) {
|
||||
func TestPostgresSinkPrunePerTable(t *testing.T) {
|
||||
db := &fakeDB{execRows: 7}
|
||||
sink := &PostgresSink{
|
||||
name: "pg",
|
||||
@@ -693,7 +691,7 @@ func TestPostgresSinkPrune_PerTable(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresSinkPrune_AllTables(t *testing.T) {
|
||||
func TestPostgresSinkPruneAllTables(t *testing.T) {
|
||||
db := &fakeDB{execRows: 3}
|
||||
sink := &PostgresSink{
|
||||
name: "pg",
|
||||
@@ -724,7 +722,7 @@ func TestPostgresSinkPrune_AllTables(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresSinkPrune_Errors(t *testing.T) {
|
||||
func TestPostgresSinkPruneErrors(t *testing.T) {
|
||||
db := &fakeDB{}
|
||||
sink := &PostgresSink{
|
||||
name: "pg",
|
||||
|
||||
@@ -112,15 +112,15 @@ func TestRegisterBuiltinsExposesExpectedDrivers(t *testing.T) {
|
||||
r := NewRegistry()
|
||||
RegisterBuiltins(r)
|
||||
|
||||
if len(r.byDriver) != 3 {
|
||||
t.Fatalf("len(byDriver) = %d, want 3", len(r.byDriver))
|
||||
if len(r.byDriver) != 2 {
|
||||
t.Fatalf("len(byDriver) = %d, want 2", len(r.byDriver))
|
||||
}
|
||||
for _, driver := range []string{"stdout", "nats", "postgres"} {
|
||||
for _, driver := range []string{"stdout", "nats"} {
|
||||
if _, ok := r.byDriver[driver]; !ok {
|
||||
t.Fatalf("builtins missing driver %q", driver)
|
||||
}
|
||||
}
|
||||
if _, ok := r.byDriver["file"]; ok {
|
||||
t.Fatalf("builtins unexpectedly registered file driver")
|
||||
if _, ok := r.byDriver["postgres"]; ok {
|
||||
t.Fatalf("builtins unexpectedly registered postgres driver")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user