Added automatic pruning for configured Postgres sinks
This commit is contained in:
@@ -395,6 +395,94 @@ func TestNewPostgresSinkFromConfig_InitFailureClosesDB(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewPostgresSinkFromConfig_PruneParamAccepted(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
in string
|
||||
want time.Duration
|
||||
}{
|
||||
{name: "go duration", in: "72h", want: 72 * time.Hour},
|
||||
{name: "days suffix", in: "3d", want: 72 * time.Hour},
|
||||
{name: "weeks suffix", in: "2w", want: 14 * 24 * time.Hour},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
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
|
||||
}
|
||||
|
||||
s, err := NewPostgresSinkFromConfig(config.SinkConfig{
|
||||
Name: "pg",
|
||||
Driver: "postgres",
|
||||
Params: map[string]any{
|
||||
"uri": "postgres://localhost/db",
|
||||
"username": "user",
|
||||
"password": "pass",
|
||||
"prune": tc.in,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("new postgres sink: %v", err)
|
||||
}
|
||||
|
||||
pg, ok := s.(*PostgresSink)
|
||||
if !ok {
|
||||
t.Fatalf("expected *PostgresSink, got %T", s)
|
||||
}
|
||||
if pg.pruneWindow != tc.want {
|
||||
t.Fatalf("prune window = %s, want %s", pg.pruneWindow, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewPostgresSinkFromConfig_PruneParamRejected(t *testing.T) {
|
||||
withPostgresTestState(t)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
in any
|
||||
}{
|
||||
{name: "empty", in: ""},
|
||||
{name: "zero", in: "0"},
|
||||
{name: "negative", in: "-1h"},
|
||||
{name: "malformed", in: "abc"},
|
||||
{name: "fractional day", in: "1.5d"},
|
||||
{name: "wrong type", in: 5},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
_, err := NewPostgresSinkFromConfig(config.SinkConfig{
|
||||
Name: "pg",
|
||||
Driver: "postgres",
|
||||
Params: map[string]any{
|
||||
"uri": "postgres://localhost/db",
|
||||
"username": "user",
|
||||
"password": "pass",
|
||||
"prune": tc.in,
|
||||
},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatalf("expected error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "params.prune") {
|
||||
t.Fatalf("expected params.prune error, got %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresSinkConsume_InvalidEvent(t *testing.T) {
|
||||
db := &fakeDB{}
|
||||
called := 0
|
||||
@@ -497,6 +585,71 @@ func TestPostgresSinkConsume_InsertFailureRollsBack(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresSinkConsume_AutoPruneRunsInSameTransaction(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) {
|
||||
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}},
|
||||
}, nil
|
||||
})),
|
||||
pruneWindow: 24 * time.Hour,
|
||||
}
|
||||
|
||||
if err := sink.Consume(context.Background(), validTestEvent()); err != nil {
|
||||
t.Fatalf("consume: %v", err)
|
||||
}
|
||||
if len(tx.execCalls) != 4 {
|
||||
t.Fatalf("expected 4 tx statements (2 inserts + 2 prunes), got %d", len(tx.execCalls))
|
||||
}
|
||||
if !strings.Contains(tx.execCalls[2].query, `DELETE FROM "events"`) {
|
||||
t.Fatalf("expected prune delete for events, got %s", tx.execCalls[2].query)
|
||||
}
|
||||
if !strings.Contains(tx.execCalls[3].query, `DELETE FROM "event_payloads"`) {
|
||||
t.Fatalf("expected prune delete for event_payloads, got %s", tx.execCalls[3].query)
|
||||
}
|
||||
if tx.commitCalls != 1 {
|
||||
t.Fatalf("expected one commit, got %d", tx.commitCalls)
|
||||
}
|
||||
if tx.rollbackCalls != 0 {
|
||||
t.Fatalf("expected zero rollbacks, got %d", tx.rollbackCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresSinkConsume_AutoPruneFailureRollsBack(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) {
|
||||
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}},
|
||||
}, nil
|
||||
})),
|
||||
pruneWindow: 24 * time.Hour,
|
||||
}
|
||||
|
||||
err := sink.Consume(context.Background(), validTestEvent())
|
||||
if err == nil {
|
||||
t.Fatalf("expected prune error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "prune older than") {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if tx.commitCalls != 0 {
|
||||
t.Fatalf("expected no commit")
|
||||
}
|
||||
if tx.rollbackCalls != 1 {
|
||||
t.Fatalf("expected rollback, got %d", tx.rollbackCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresSinkPrune_PerTable(t *testing.T) {
|
||||
db := &fakeDB{execRows: 7}
|
||||
sink := &PostgresSink{
|
||||
|
||||
Reference in New Issue
Block a user