Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/store/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ func live(table string) exp.BooleanExpression {
return goqu.I(table + ".deleted_at").IsNull()
}

// softDelete marks rows as deleted instead of removing them. It skips rows
// that are already deleted, so the first delete time is kept.
func softDelete(table string) *goqu.UpdateDataset {
return dialect.Update(table).Set(goqu.Record{"deleted_at": goqu.L("now()")}).Where(live(table))
}

// fromLive reads only the rows that are not soft-deleted.
func fromLive(table string) *goqu.SelectDataset {
return dialect.From(table).Where(live(table))
Expand Down
39 changes: 39 additions & 0 deletions internal/store/postgres/postgres_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package postgres

import (
"testing"

"github.com/doug-martin/goqu/v9"
"github.com/stretchr/testify/assert"
)

func TestSoftDelete(t *testing.T) {
tests := []struct {
name string
stmt *goqu.UpdateDataset
wantSQL string
wantParams []any
}{
{
name: "marks the live rows of the table",
stmt: softDelete(TABLE_DOMAINS),
wantSQL: `UPDATE "domains" SET "deleted_at"=now() WHERE ("domains"."deleted_at" IS NULL)`,
wantParams: []any{},
},
{
name: "joins the caller's filter with AND",
stmt: softDelete(TABLE_DOMAINS).Where(goqu.Ex{"id": "8f2d5b1e-3c4a-4d6b-9e7f-1a2b3c4d5e6f"}),
wantSQL: `UPDATE "domains" SET "deleted_at"=now() WHERE (("domains"."deleted_at" IS NULL) AND ("id" = $1))`,
wantParams: []any{"8f2d5b1e-3c4a-4d6b-9e7f-1a2b3c4d5e6f"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotSQL, gotParams, err := tt.stmt.Prepared(true).ToSQL()
assert.NoError(t, err)
assert.Equal(t, tt.wantSQL, gotSQL)
assert.Equal(t, tt.wantParams, gotParams)
})
}
}
Loading