Let the database stamp a site's archive time on every write

#246. Only the console's own status write set or cleared a site's
archived_at; the sync copied a status the farm reported without touching
it. A site archived on the farm outside the console therefore never
reached the retention purge, and a site archived, restored on the farm
and archived again kept its first time, so the purge at the end of the
same sync run could delete it at once.

A BEFORE INSERT OR UPDATE trigger on fedwiki.sites now owns the column
(FedWiki migration 00007): a row whose new status is not archived gets
NULL, a row inserted archived or entering archived gets
clock_timestamp(), and a row that stays archived keeps its time, or is
stamped then when it has none. A time the statement supplies is
ignored. SetSiteStatusByDomain loses its CASE, so no query assigns the
column, and a writer added later cannot forget the rule. #245's purge,
its locks and its strict cutoff are unchanged.

The migration changes no existing row; a row is corrected on its next
write, which for a sync-enabled deployment is the next sync pass.
Before the migration deploys, a deployment counts its archived rows and
corrects any suspect old time by hand; afterwards, a correction moves
the row out of and back into archived in one transaction. Reverting
the migration also needs the query's CASE restored. Unchanged: a farm
restore and re-archive between two sync passes is invisible to the
console, so the row keeps its time.

Tests: TestSiteArchiveTime_DB, TestSiteArchiveTimeQueries_DB and
TestArchiveTimeMigration_DB on a scratch database migrated with the
FedWiki stream; TestArchiveRetentionBoundary_DB,
TestSyncArchiveWindow_DB and TestRecordArchiveTime_DB on the shared
test database. #245's purge tests now archive a row through a status
write, and a helper ages it by disabling the trigger, moving the time
back and enabling the trigger again in one transaction.

Full suite on reset databases: 5232 passed, 16 skipped, none failed.
No page changed.

Breaks, all CAUGHT (30; the first 14 are #245's, kept because this
change rewrites the fixtures they break):
- current archive time
- strict cutoff
- candidate identity
- open request skips
- lifecycle instance lock
- request snapshot after waiting
- row update is a successful skip
- site row lock
- locks last through HTTP
- local deletion
- placement release
- continue after candidate failure
- farm refusal keeps the row
- 404 is success
- trigger omits INSERT
- archive insert accepts caller time
- non-archived preserves time: archived_to_active
- non-archived preserves time: insert_readonly_with_time
- readonly entry fails to stamp
- entry uses transaction start
- repeated archive restarts time
- status-only trigger: clear_time_only
- status-only trigger: replace_time_only
- missing archive time has no fallback
- Down leaves enforcement
- upsert omits status
- listing includes cutoff
- activation preserves archived: restore
- activation preserves archived: swap_activation
- archive preserves active
This commit is contained in:
2026-10-30 03:50:43 -05:00
parent c2ae2ace0c
commit d87cb15a76
15 changed files with 942 additions and 74 deletions
@@ -0,0 +1,280 @@
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
// SPDX-FileCopyrightText: 2025-2026 Christian Galo
package fedwiki_test
import (
"database/sql"
"errors"
"math"
"testing"
"time"
"github.com/google/uuid"
"git.coopcloud.tech/wiki-cafe/member-console/internal/db"
"git.coopcloud.tech/wiki-cafe/member-console/internal/integrations/fedwiki/fedwikitest"
fwmod "git.coopcloud.tech/wiki-cafe/member-console/internal/integrations/fedwiki/store"
"git.coopcloud.tech/wiki-cafe/member-console/internal/migrate"
"git.coopcloud.tech/wiki-cafe/member-console/internal/testkit/dbtest"
)
// archiveTimeVersion is this migration's number; settle it at landing.
const archiveTimeVersion int64 = 7
// archiveDatabase migrates a scratch database from the canonical source list.
func archiveDatabase(t *testing.T, version int64) *sql.DB {
t.Helper()
database := dbtest.Connect(t, dbtest.Scratch(t))
for _, source := range migrate.Sources() {
target := int64(math.MaxInt64)
if source.Name == "fedwiki" {
target = version
}
dbtest.UpTo(t, database, source, target)
}
return database
}
// archiveState holds the status and timestamp read from one site.
type archiveState struct {
status string
stamp sql.NullTime
}
// archiveRead reads a site's status and archive time.
func archiveRead(t *testing.T, executor dbtest.Execer, id string) archiveState {
t.Helper()
var state archiveState
dbtest.On(t, executor).Scan([]any{&state.status, &state.stamp}, `SELECT status, archived_at FROM fedwiki.sites WHERE site_id = $1`, id)
return state
}
// archiveClock reads the database clock used to bound a write.
func archiveClock(t *testing.T, executor dbtest.Execer) time.Time {
t.Helper()
var stamp time.Time
dbtest.On(t, executor).Scan([]any{&stamp}, `SELECT clock_timestamp()`)
return stamp
}
// archiveWithin reports whether a timestamp falls within the write's bounds.
func archiveWithin(stamp sql.NullTime, before, after time.Time) bool {
return stamp.Valid && !stamp.Time.Before(before) && !stamp.Time.After(after)
}
// archiveCase describes a raw write and its expected timestamp.
type archiveCase struct {
name, initial, statement, want string
supplied bool
}
// TestSiteArchiveTime_DB proves raw runtime writes cannot choose archive timestamps.
func TestSiteArchiveTime_DB(t *testing.T) {
database := archiveDatabase(t, math.MaxInt64)
workspace := fedwikitest.New(t, database).Tenant("Archive").Workspace.ID
cases := []archiveCase{
{name: "insert archived without time", initial: "archived", want: "fresh"},
{name: "insert archived with old time", initial: "archived", supplied: true, want: "fresh"},
{name: "insert active with time", initial: "active", supplied: true, want: "null"},
{name: "insert readonly with time", initial: "readonly", supplied: true, want: "null"},
{name: "active to archived", initial: "active", statement: `status = 'archived', archived_at = '2000-01-01'`, want: "fresh"},
{name: "readonly to archived", initial: "readonly", statement: `status = 'archived', archived_at = '2000-01-01'`, want: "fresh"},
{name: "archived to active", initial: "archived", statement: `status = 'active'`, want: "null"},
{name: "archived to readonly", initial: "archived", statement: `status = 'readonly'`, want: "null"},
{name: "repeat archived", initial: "archived", statement: `status = 'archived'`, want: "same"},
{name: "metadata only", initial: "archived", statement: `storage_bytes = 42`, want: "same"},
{name: "clear time only", initial: "archived", statement: `archived_at = NULL`, want: "same"},
{name: "replace time only", initial: "archived", statement: `archived_at = '2000-01-01'`, want: "same"},
}
for _, row := range cases {
t.Run(row.name, func(t *testing.T) { checkArchiveCase(t, database, workspace, row) })
}
t.Run("archive rollback", func(t *testing.T) { checkArchiveRollback(t, database, workspace) })
}
// checkArchiveCase executes one runtime write and checks its timestamp.
func checkArchiveCase(t *testing.T, database *sql.DB, workspace string, row archiveCase) {
query := dbtest.Tx(t, database)
query.Exec(`SET LOCAL ROLE ` + db.RuntimeGroupRole)
before := archiveClock(t, query.Tx())
var supplied sql.NullTime
if row.supplied {
supplied = sql.NullTime{Time: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Valid: true}
}
id := query.ID(`INSERT INTO fedwiki.sites (workspace_id, domain, status, archived_at) VALUES ($1,$2,$3,$4) RETURNING site_id`, workspace, dbtest.Name("archive")+".test", row.initial, supplied)
original := archiveRead(t, query.Tx(), id)
if row.statement != "" {
before = archiveClock(t, query.Tx())
query.Exec(`UPDATE fedwiki.sites SET `+row.statement+` WHERE site_id = $1`, id)
}
after := archiveClock(t, query.Tx())
actual := archiveRead(t, query.Tx(), id)
switch row.want {
case "fresh":
if !archiveWithin(actual.stamp, before, after) {
t.Errorf("%s: archive time = %v, want a time within the write", row.name, actual.stamp)
}
case "null":
if actual.stamp.Valid {
t.Errorf("%s: archive time = %v, want NULL", row.name, actual.stamp)
}
case "same":
if !sameStamp(actual.stamp, original.stamp) {
t.Errorf("%s: archive time changed from %v to %v", row.name, original.stamp, actual.stamp)
}
}
}
// checkArchiveRollback proves status and timestamp roll back together.
func checkArchiveRollback(t *testing.T, database *sql.DB, workspace string) {
id := fedwikitest.Site(t, database, workspace, dbtest.Name("archive")+".test", "active")
before := archiveRead(t, database, id)
query := dbtest.Tx(t, database)
query.Exec(`SET LOCAL ROLE ` + db.RuntimeGroupRole)
query.Exec(`UPDATE fedwiki.sites SET status = 'archived' WHERE site_id = $1`, id)
if err := query.Tx().Rollback(); err != nil {
t.Fatal(err)
}
if after := archiveRead(t, database, id); after.status != before.status || !sameStamp(after.stamp, before.stamp) {
t.Error("rollback changed status or archive time")
}
}
// TestSiteArchiveTimeQueries_DB proves every query follows the database archive rule.
func TestSiteArchiveTimeQueries_DB(t *testing.T) {
database := archiveDatabase(t, math.MaxInt64)
workspace := fedwikitest.New(t, database).Tenant("Queries").Workspace.ID
for _, writer := range []string{"create", "insert", "upsert", "status"} {
for _, status := range []string{"active", "readonly", "archived"} {
if writer == "create" && status != "active" {
continue
}
t.Run(writer+" "+status, func(t *testing.T) { checkArchiveQuery(t, database, workspace, writer, status) })
}
}
t.Run("repeat and reenter", func(t *testing.T) { checkArchiveQuerySequence(t, database, workspace) })
}
// queryArchiveWrite executes the selected site writer.
func queryArchiveWrite(t *testing.T, query *fwmod.Queries, workspace, domain, writer, status string) fwmod.Site {
t.Helper()
var site fwmod.Site
var err error
switch writer {
case "create":
id, mintErr := uuid.NewV7()
if mintErr != nil {
t.Fatal(mintErr)
}
site, err = query.CreateSite(t.Context(), fwmod.CreateSiteParams{SiteID: id.String(), WorkspaceID: workspace, Domain: domain})
case "insert":
site, err = query.InsertSiteFromFarm(t.Context(), fwmod.InsertSiteFromFarmParams{WorkspaceID: workspace, Domain: domain, Status: status})
case "upsert":
site, err = query.UpsertSiteFromFarm(t.Context(), fwmod.UpsertSiteFromFarmParams{WorkspaceID: workspace, Domain: domain, Status: status})
case "status":
site, err = query.SetSiteStatusByDomain(t.Context(), fwmod.SetSiteStatusByDomainParams{Domain: domain, Status: status})
case "observe":
site, err = query.SetSiteObservedByDomain(t.Context(), fwmod.SetSiteObservedByDomainParams{Domain: domain, StorageBytes: sql.NullInt64{Int64: 42, Valid: true}})
}
if err != nil {
t.Fatal(err)
}
return site
}
// checkArchiveQuery compares returned and stored values for one query.
func checkArchiveQuery(t *testing.T, database *sql.DB, workspace, writer, status string) {
domain := dbtest.Name("archive") + ".test"
if writer == "status" {
fedwikitest.Site(t, database, workspace, domain, "active")
}
query := dbtest.Tx(t, database)
query.Exec(`SET LOCAL ROLE ` + db.RuntimeGroupRole)
before := archiveClock(t, query.Tx())
site := queryArchiveWrite(t, fwmod.New(query.Tx()), workspace, domain, writer, status)
after := archiveClock(t, query.Tx())
stored := archiveRead(t, query.Tx(), site.SiteID)
if !sameStamp(stored.stamp, site.ArchivedAt) {
t.Errorf("%s: returned and stored archive times differ", writer)
}
if stored.status != status || (status == "archived" && !archiveWithin(stored.stamp, before, after)) || (status != "archived" && stored.stamp.Valid) {
t.Errorf("%s: archive time violates transition", writer)
}
}
// checkArchiveQuerySequence checks conflicts, repeated observations and reentry.
func checkArchiveQuerySequence(t *testing.T, database *sql.DB, workspace string) {
domain := dbtest.Name("archive") + ".test"
other := fedwikitest.New(t, database).Tenant("Other").Workspace.ID
query := fwmod.New(database)
first := queryArchiveWrite(t, query, workspace, domain, "upsert", "archived")
repeat := queryArchiveWrite(t, query, other, domain, "upsert", "archived")
if !sameStamp(repeat.ArchivedAt, first.ArchivedAt) {
t.Error("repeated upsert restarted the archive window")
}
if repeat.WorkspaceID != workspace {
t.Error("conflict changed the existing row")
}
_, err := query.InsertSiteFromFarm(t.Context(), fwmod.InsertSiteFromFarmParams{WorkspaceID: other, Domain: domain, Status: "active"})
if err != nil && !errors.Is(err, sql.ErrNoRows) {
t.Fatalf("conflict insert: %v", err)
}
if err == nil || !sameStamp(archiveRead(t, database, first.SiteID).stamp, first.ArchivedAt) {
t.Error("conflict changed the existing row")
}
for _, writer := range []string{"status", "observe"} {
site := queryArchiveWrite(t, query, workspace, domain, writer, "archived")
if !sameStamp(site.ArchivedAt, first.ArchivedAt) {
t.Errorf("%s: archive time violates transition", writer)
}
}
for _, writer := range []string{"upsert", "status"} {
for _, status := range []string{"active", "archived", "readonly", "archived"} {
before := archiveClock(t, database)
site := queryArchiveWrite(t, query, workspace, domain, writer, status)
after := archiveClock(t, database)
if (status == "archived" && !archiveWithin(site.ArchivedAt, before, after)) || (status != "archived" && site.ArchivedAt.Valid) {
t.Errorf("%s: archive time violates transition", writer)
}
if !sameStamp(site.ArchivedAt, archiveRead(t, database, site.SiteID).stamp) {
t.Errorf("%s: returned and stored archive times differ", writer)
}
}
}
}
// TestArchiveTimeMigration_DB proves installation leaves rows unchanged and their next writes correct them.
func TestArchiveTimeMigration_DB(t *testing.T) {
database := archiveDatabase(t, archiveTimeVersion-1)
workspace := fedwikitest.New(t, database).Tenant("Legacy").Workspace.ID
query := dbtest.On(t, database)
archived := query.ID(`INSERT INTO fedwiki.sites (workspace_id, domain, status) VALUES ($1,$2,'archived') RETURNING site_id`, workspace, dbtest.Name("archive")+".test")
active := query.ID(`INSERT INTO fedwiki.sites (workspace_id, domain, status, archived_at) VALUES ($1,$2,'active','2000-01-01') RETURNING site_id`, workspace, dbtest.Name("archive")+".test")
previous := archiveRead(t, database, active)
dbtest.ApplyVersion(t, database, fwmod.MigrationSource(), archiveTimeVersion, true)
if archiveRead(t, database, archived).stamp.Valid || !sameStamp(archiveRead(t, database, active).stamp, previous.stamp) {
t.Error("migration changed existing archive times")
}
for _, row := range []struct {
name, id string
archived bool
}{{"archived", archived, true}, {"active", active, false}} {
before := archiveClock(t, database)
query.Exec(`UPDATE fedwiki.sites SET storage_bytes = 123 WHERE site_id = $1`, row.id)
after := archiveClock(t, database)
stamp := archiveRead(t, database, row.id).stamp
if (row.archived && !archiveWithin(stamp, before, after)) || (!row.archived && stamp.Valid) {
t.Errorf("%s: legacy row not corrected on its next write", row.name)
}
}
dbtest.ApplyVersion(t, database, fwmod.MigrationSource(), archiveTimeVersion, false)
if query.Int(`SELECT count(*) FROM pg_trigger WHERE tgname = 'trigger_sites_archived_at' AND tgrelid = 'fedwiki.sites'::regclass`) != 0 || query.Int(`SELECT count(*) FROM pg_proc WHERE proname = 'set_site_archived_at' AND pronamespace = 'fedwiki'::regnamespace`) != 0 {
t.Error("down left archive enforcement installed")
}
}
// sameStamp compares nullable timestamps by instant rather than location.
func sameStamp(first, second sql.NullTime) bool {
return first.Valid == second.Valid && (!first.Valid || first.Time.Equal(second.Time))
}
@@ -0,0 +1,45 @@
-- SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
-- SPDX-FileCopyrightText: 2025-2026 Christian Galo
-- +goose Up
-- +goose StatementBegin
-- The database owns fedwiki.sites.archived_at, so no writer chooses a
-- site's retention window. On every INSERT and UPDATE a row whose new status
-- is not archived gets NULL; an inserted archived row, or a row entering
-- archived, gets clock_timestamp(), the moment of the write rather than the
-- transaction's start, which can precede a wait for the row's lock; a row
-- that stays archived keeps its time, or is stamped now when it has none.
-- A value the statement supplies is ignored. This migration changes no row:
-- an existing row is corrected on its next write.
CREATE FUNCTION fedwiki.set_site_archived_at()
RETURNS trigger
LANGUAGE plpgsql
SET search_path = pg_catalog, pg_temp
AS $$
BEGIN
IF NEW.status <> 'archived' THEN
NEW.archived_at := NULL;
ELSIF TG_OP = 'INSERT' THEN
NEW.archived_at := pg_catalog.clock_timestamp();
ELSIF OLD.status <> 'archived' THEN
NEW.archived_at := pg_catalog.clock_timestamp();
ELSE
NEW.archived_at := COALESCE(OLD.archived_at, pg_catalog.clock_timestamp());
END IF;
RETURN NEW;
END;
$$;
CREATE TRIGGER trigger_sites_archived_at
BEFORE INSERT OR UPDATE ON fedwiki.sites
FOR EACH ROW EXECUTE FUNCTION fedwiki.set_site_archived_at();
-- +goose StatementEnd
-- +goose Down
-- No query writes archived_at once this trigger exists; reverting it means
-- restoring SetSiteStatusByDomain's archive-time CASE as well.
-- +goose StatementBegin
DROP TRIGGER trigger_sites_archived_at ON fedwiki.sites;
DROP FUNCTION fedwiki.set_site_archived_at();
-- +goose StatementEnd
@@ -150,10 +150,10 @@ type Querier interface {
// returns carries the status the row holds now, which the sync's placement
// projection follows. No row is sql.ErrNoRows.
SetSiteObservedByDomain(ctx context.Context, arg SetSiteObservedByDomainParams) (Site, error)
// Project a lifecycle status change locally. archived_at is set on entering
// `archived` (idempotent — preserves the original timestamp) and cleared on
// leaving it, driving the Trash retention purge. force_reduced_at marks a
// site a downgrade left read-only, so it is kept only while the site stays
// Project a lifecycle status change locally. archived_at is maintained by
// the trigger function fedwiki.set_site_archived_at on every site write.
// force_reduced_at marks a site a downgrade left read-only, so it is kept
// only while the site stays
// `readonly`: a site that becomes active or archived is no longer parked,
// and the re-upgrade sweep must not bring it back.
SetSiteStatusByDomain(ctx context.Context, arg SetSiteStatusByDomainParams) (Site, error)
@@ -97,19 +97,14 @@ ON CONFLICT(domain) DO NOTHING
RETURNING *;
-- name: SetSiteStatusByDomain :one
-- Project a lifecycle status change locally. archived_at is set on entering
-- `archived` (idempotent — preserves the original timestamp) and cleared on
-- leaving it, driving the Trash retention purge. force_reduced_at marks a
-- site a downgrade left read-only, so it is kept only while the site stays
-- Project a lifecycle status change locally. archived_at is maintained by
-- the trigger function fedwiki.set_site_archived_at on every site write.
-- force_reduced_at marks a site a downgrade left read-only, so it is kept
-- only while the site stays
-- `readonly`: a site that becomes active or archived is no longer parked,
-- and the re-upgrade sweep must not bring it back.
UPDATE fedwiki.sites
SET status = $2,
archived_at = CASE
WHEN $2 = 'archived' AND status <> 'archived' THEN NOW()
WHEN $2 <> 'archived' THEN NULL
ELSE archived_at
END,
force_reduced_at = CASE WHEN $2 = 'readonly' THEN force_reduced_at END,
updated_at = NOW()
WHERE domain = $1
@@ -1059,11 +1059,6 @@ func (q *Queries) SetSiteObservedByDomain(ctx context.Context, arg SetSiteObserv
const setSiteStatusByDomain = `-- name: SetSiteStatusByDomain :one
UPDATE fedwiki.sites
SET status = $2,
archived_at = CASE
WHEN $2 = 'archived' AND status <> 'archived' THEN NOW()
WHEN $2 <> 'archived' THEN NULL
ELSE archived_at
END,
force_reduced_at = CASE WHEN $2 = 'readonly' THEN force_reduced_at END,
updated_at = NOW()
WHERE domain = $1
@@ -1075,10 +1070,10 @@ type SetSiteStatusByDomainParams struct {
Status string `json:"status"`
}
// Project a lifecycle status change locally. archived_at is set on entering
// `archived` (idempotent — preserves the original timestamp) and cleared on
// leaving it, driving the Trash retention purge. force_reduced_at marks a
// site a downgrade left read-only, so it is kept only while the site stays
// Project a lifecycle status change locally. archived_at is maintained by
// the trigger function fedwiki.set_site_archived_at on every site write.
// force_reduced_at marks a site a downgrade left read-only, so it is kept
// only while the site stays
// `readonly`: a site that becomes active or archived is no longer parked,
// and the re-upgrade sweep must not bring it back.
func (q *Queries) SetSiteStatusByDomain(ctx context.Context, arg SetSiteStatusByDomainParams) (Site, error) {
@@ -0,0 +1,226 @@
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
// SPDX-FileCopyrightText: 2025-2026 Christian Galo
package workflows
import (
"database/sql"
"slices"
"testing"
"time"
"git.coopcloud.tech/wiki-cafe/member-console/internal/integration"
fwmod "git.coopcloud.tech/wiki-cafe/member-console/internal/integrations/fedwiki/store"
"git.coopcloud.tech/wiki-cafe/member-console/internal/testkit/dbtest"
)
// TestArchiveRetentionBoundary_DB proves both retention predicates exclude the cutoff itself.
func TestArchiveRetentionBoundary_DB(t *testing.T) {
for _, row := range []struct {
name string
offset time.Duration
deleted bool
}{
{"before cutoff", time.Microsecond, true}, {"at cutoff", 0, false}, {"after cutoff", -time.Microsecond, false},
} {
t.Run(row.name, func(t *testing.T) { retentionBoundary(t, row.name, row.offset, row.deleted) })
}
}
// retentionBoundary checks listing and locked deletion on separate fixture rows.
func retentionBoundary(t *testing.T, name string, offset time.Duration, want bool) {
env := newReqEnv(t, 5, 0)
listedSite := archivedPurgeSite(env, "listed")
listed, err := fwmod.New(env.db).ListArchivedSitesArchivedBefore(env.ctx, sql.NullTime{Time: listedSite.ArchivedAt.Time.Add(offset), Valid: true})
if err != nil {
t.Fatal(err)
}
found := slices.ContainsFunc(listed, func(site fwmod.Site) bool { return site.SiteID == listedSite.SiteID })
if found != want {
t.Errorf("%s: retention candidates = %v, want %v", name, found, want)
}
site := archivedPurgeSite(env, "locked")
deleted, err := env.acts.purgeArchivedSite(env.ctx, env.db, site, site.ArchivedAt.Time.Add(offset))
if err != nil {
t.Fatal(err)
}
if deleted != want {
t.Errorf("%s: retention deleted = %t, want %t", name, deleted, want)
}
}
// archiveTimeOf reads the timestamp stored for a fixture.
func archiveTimeOf(env *reqEnv, id string) sql.NullTime {
env.t.Helper()
var stamp sql.NullTime
dbtest.On(env.t, env.db).Scan([]any{&stamp}, `SELECT archived_at FROM fedwiki.sites WHERE site_id = $1`, id)
return stamp
}
// syncArchiveCase describes the observed statuses and expected window.
type syncArchiveCase struct {
name, initial, target string
ageInitial, restoreFirst, wantSame bool
}
// TestSyncArchiveWindow_DB proves observed archives receive a full retention window.
func TestSyncArchiveWindow_DB(t *testing.T) {
for _, row := range []syncArchiveCase{
{name: "first seen archived", target: "archived"},
{name: "farm archives active", initial: "active", target: "archived"},
{name: "farm restores archived", initial: "archived", ageInitial: true, target: "active"},
{name: "farm rearchives restored", initial: "archived", ageInitial: true, restoreFirst: true, target: "archived"},
{name: "repeat archived observation", initial: "archived", target: "archived", wantSame: true},
} {
t.Run(row.name, func(t *testing.T) { syncArchiveWindow(t, row) })
}
}
// syncArchiveWindow checks one observation sequence and its retention outcome.
func syncArchiveWindow(t *testing.T, row syncArchiveCase) {
env := newReqEnv(t, 5, 0)
domain := "observed." + env.root
id := ""
if row.initial != "" {
id = env.insertSite(domain, row.initial, false)
env.placeServable(domain, id)
}
if row.ageInitial {
ageArchive(t, env.db, id)
}
if row.restoreFirst {
syncArchiveStatus(env, domain, "active")
if archiveTimeOf(env, id).Valid {
t.Errorf("%s: sync archive time violates transition", row.name)
}
}
var original sql.NullTime
if row.wantSame {
original = archiveTimeOf(env, id)
}
before := env.databaseNow()
syncArchiveStatus(env, domain, row.target)
after := env.databaseNow()
site, err := fwmod.New(env.db).GetSiteByDomain(env.ctx, domain)
if err != nil {
t.Fatal(err)
}
id = site.SiteID
if !env.placed(domain) {
env.placeServable(domain, id)
}
stamp := site.ArchivedAt
valid := !stamp.Valid
if row.target == "archived" {
valid = stamp.Valid && !stamp.Time.Before(before) && !stamp.Time.After(after)
}
if row.wantSame {
valid = sameStamp(stamp, original)
}
if site.Status != row.target || !valid {
t.Errorf("%s: sync archive time violates transition", row.name)
}
farm := observePurgeFarm(env, nil)
env.acts.SiteQ = purgeCandidates{Querier: env.acts.SiteQ, ids: []string{id}}
checkSyncRetention(env, farm, row.name, id, domain, false)
if row.target == "archived" {
ageArchive(t, env.db, id)
checkSyncRetention(env, farm, row.name, id, domain, true)
}
}
// syncArchiveStatus projects one fake farm observation through the real sync activity.
func syncArchiveStatus(env *reqEnv, domain, status string) {
env.t.Helper()
env.farm.HoldIn(domain, env.subject, status)
_, err := env.acts.SyncSitesToDBActivity(env.ctx, SyncSitesToDBInput{DefaultWorkspaceID: env.ws, FarmSites: []SiteInfo{{Name: domain, Status: status}}})
if err != nil {
env.t.Fatal(err)
}
}
// checkSyncRetention checks deletion, placement release and usage for one fixture.
func checkSyncRetention(env *reqEnv, farm *purgeFarm, name, id, domain string, deleted bool) {
env.t.Helper()
output, err := env.acts.PurgeExpiredArchivedActivity(env.ctx, struct{}{})
if err != nil {
env.t.Fatal(err)
}
var wantCalls []string
wantCount := 0
if deleted {
wantCalls = []string{"DELETE /plugin/farmmanager/sites/" + domain + "?hard=true"}
wantCount = 1
}
if !slices.Equal(farm.calls(), wantCalls) {
env.t.Errorf("%s: sync retention hard deletes = %v, want %v", name, farm.calls(), wantCalls)
}
count := dbtest.On(env.t, env.db).Int(`SELECT count(*) FROM fedwiki.sites WHERE site_id = $1`, id)
if output.Purged != wantCount || (count == 0) != deleted || env.placed(domain) == deleted {
env.t.Errorf("%s: sync retention left wrong rows or placements", name)
}
if figure, rows := env.figureAndRows(); figure != rows {
env.t.Errorf("%s: sync retention usage differs from counted rows", name)
}
}
// recordArchiveCase describes the initial state and requested activation or archive.
type recordArchiveCase struct {
name, initial, target string
swap bool
}
// TestRecordArchiveTime_DB proves recorded lifecycle outcomes maintain archive time.
func TestRecordArchiveTime_DB(t *testing.T) {
for _, row := range []recordArchiveCase{
{name: "archive", initial: "active", target: "archived"},
{name: "restore", initial: "archived", target: "active"},
{name: "swap activation", initial: "archived", target: "active", swap: true},
} {
t.Run(row.name, func(t *testing.T) { recordArchiveTime(t, row) })
}
}
// recordArchiveTime checks the timestamp, status, request outcome and usage.
func recordArchiveTime(t *testing.T, row recordArchiveCase) {
env := newReqEnv(t, 5, 0)
initial, target := row.initial, row.target
domain := "recorded." + env.root
id := env.insertSite(domain, initial, false)
var generation int64
var err error
if row.swap {
parkedDomain := "incumbent." + env.root
parked := env.insertSite(parkedDomain, "active", false)
var parkedGeneration int64
generation, parkedGeneration = env.swapRequests(id, parked, "archive-time-"+id)
err = env.acts.RecordSwapActivity(env.ctx, RecordSwapInput{Activate: LifecycleRequestRef{InstanceID: id, Generation: generation}, Park: LifecycleRequestRef{InstanceID: parked, Generation: parkedGeneration}, ActivateDomain: domain, ParkDomain: parkedDomain, FinishedAt: time.Now()})
if status, _ := env.request(parked, parkedGeneration); status != "succeeded" {
t.Errorf("parked request = %s", status)
}
env.wantSiteStatus(parkedDomain, "readonly")
} else {
generation = env.record(id, integration.LifecycleSetStatus, "archive-time-"+id, map[string]any{"domain": domain, "status": target})
err = env.acts.RecordSiteStatusActivity(env.ctx, RecordSiteStatusInput{InstanceID: id, Generation: generation, WorkspaceID: env.ws, Domain: domain, Status: target, FinishedAt: time.Now()})
}
if err != nil {
t.Error(err)
}
stamp := archiveTimeOf(env, id)
if target == "archived" && !stamp.Valid {
t.Error("recorded archive has no archive time")
}
if target == "active" && stamp.Valid {
t.Error("recorded activation retained archive time")
}
env.wantSiteStatus(domain, target)
if status, _ := env.request(id, generation); status != "succeeded" {
t.Errorf("request = %s", status)
}
env.wantFigure("after recording archive time")
}
// sameStamp compares nullable timestamps by instant rather than location.
func sameStamp(first, second sql.NullTime) bool {
return first.Valid == second.Valid && (!first.Valid || first.Time.Equal(second.Time))
}
@@ -55,7 +55,7 @@ func TestRetentionPurgeFailures_DB(t *testing.T) {
// retentionFailure creates the fixture and applies one table row's failure.
func retentionFailure(t *testing.T, row purgeFailureCase) {
env := newReqEnv(t, 5, 0)
site := expiredPurgeSite(env, "failure")
site := archivedPurgeSite(env, "failure")
if row.absent {
if _, err := env.acts.Client.HardDeleteSite(env.ctx, site.Domain); err != nil {
t.Fatal(err)
@@ -69,7 +69,7 @@ func retentionBeginFailure(env *reqEnv, site fwmod.Site, row purgeFailureCase) {
env.t.Helper()
farm := observePurgeFarm(env, nil)
database := dbtest.Closed(env.t)
deleted, err := env.acts.purgeArchivedSite(env.ctx, database, site, time.Now().Add(-ArchiveRetention))
deleted, err := env.acts.purgeArchivedSite(env.ctx, database, site, site.ArchivedAt.Time.Add(time.Microsecond))
checkPurgeHelperFailure(env, site, row, deleted, err, farm)
}
@@ -87,7 +87,7 @@ func retentionCancelledWait(env *reqEnv, site fwmod.Site, row purgeFailureCase)
var deleted bool
done := inGoroutine(func() error {
var err error
deleted, err = env.acts.purgeArchivedSite(ctx, connection, site, time.Now().Add(-ArchiveRetention))
deleted, err = env.acts.purgeArchivedSite(ctx, connection, site, site.ArchivedAt.Time.Add(time.Microsecond))
return err
})
defer func() {
@@ -138,7 +138,7 @@ func retentionLocalFailure(env *reqEnv, site fwmod.Site, row purgeFailureCase) {
ctx, cancel := context.WithCancel(env.ctx)
defer cancel()
env.acts.Client.httpClient.Transport = purgeCancelTransport{cancel: cancel}
deleted, err := env.acts.purgeArchivedSite(ctx, env.db, site, time.Now().Add(-ArchiveRetention))
deleted, err := env.acts.purgeArchivedSite(ctx, env.db, site, site.ArchivedAt.Time.Add(time.Microsecond))
checkPurgeHelperFailure(env, site, row, deleted, err, farm)
if errors.Is(err, errRetentionFarmDelete) {
env.t.Errorf("%s: purge error = %v, want local failure", row.name, err)
@@ -167,6 +167,7 @@ func checkPurgeHelperFailure(env *reqEnv, site fwmod.Site, row purgeFailureCase,
// retentionActivityFailure exercises real HTTP responses and checks the activity's warning category.
func retentionActivityFailure(env *reqEnv, site fwmod.Site, row purgeFailureCase) {
env.t.Helper()
ageArchive(env.t, env.db, site.SiteID)
var logs bytes.Buffer
env.acts.Logger = slog.New(slog.NewTextHandler(&logs, nil))
farm := observePurgeFarm(env, func(writer http.ResponseWriter, request *http.Request) bool {
@@ -179,13 +180,13 @@ func retentionActivityFailure(env *reqEnv, site fwmod.Site, row purgeFailureCase
listError := errors.New("candidate list failed")
queries := purgeCandidates{Querier: env.acts.SiteQ, ids: []string{site.SiteID}}
if row.listError {
queries.after = func(time.Time, []fwmod.Site) ([]fwmod.Site, error) { return nil, listError }
queries.after = func([]fwmod.Site) ([]fwmod.Site, error) { return nil, listError }
}
var later fwmod.Site
if row.later {
later = expiredPurgeSite(env, "later")
queries.ids = append(queries.ids, later.SiteID)
queries.after = func(_ time.Time, listed []fwmod.Site) ([]fwmod.Site, error) {
queries.after = func(listed []fwmod.Site) ([]fwmod.Site, error) {
return []fwmod.Site{listed[0], {SiteID: "invalid", Domain: "invalid"}, listed[1]}, nil
}
}
@@ -35,7 +35,7 @@ func startPurge(env *reqEnv, database db.Beginner, site fwmod.Site) *purgeCall {
call := &purgeCall{done: make(chan struct{})}
go func() {
defer close(call.done)
call.deleted, call.err = env.acts.purgeArchivedSite(ctx, database, site, time.Now().Add(-ArchiveRetention))
call.deleted, call.err = env.acts.purgeArchivedSite(ctx, database, site, site.ArchivedAt.Time.Add(time.Microsecond))
}()
env.t.Cleanup(func() { cancel(); <-call.done })
return call
@@ -88,7 +88,7 @@ func purgeRepeatableRead(t *testing.T) *sql.DB {
type purgeWaitCase struct {
name string
repeatable bool
update string
updates []string
wantDeleted bool
wantError bool
wantCalls int
@@ -109,7 +109,7 @@ func TestRetentionPurgeWaitsForLifecycleIntake_DB(t *testing.T) {
// retentionWaitsForIntake commits a real accepted request while the purge waits for it.
func retentionWaitsForIntake(t *testing.T, row purgeWaitCase) {
env := newReqEnv(t, 5, 0)
site := expiredPurgeSite(env, "intake")
site := archivedPurgeSite(env, "intake")
farm := observePurgeFarm(env, nil)
side := env.beginSide(env.lockWaitContext())
answer, err := integration.RecordLifecycleRequest(side.ctx, side.tx, integration.LifecycleRequestSpec{
@@ -142,9 +142,9 @@ func retentionWaitsForIntake(t *testing.T, row purgeWaitCase) {
// lock wait is skipped normally, not rejected with a serialization error.
func TestRetentionPurgeWaitsForSiteEligibility_DB(t *testing.T) {
cases := []purgeWaitCase{
{name: "active", update: "UPDATE fedwiki.sites SET status = 'active' WHERE site_id = $1", wantDeleted: false, wantError: false, wantCalls: 0},
{name: "fresh archive", update: "UPDATE fedwiki.sites SET archived_at = now() WHERE site_id = $1", wantDeleted: false, wantError: false, wantCalls: 0},
{name: "cleared archive time", update: "UPDATE fedwiki.sites SET archived_at = NULL WHERE site_id = $1", wantDeleted: false, wantError: false, wantCalls: 0},
{name: "active", updates: []string{"UPDATE fedwiki.sites SET status = 'active' WHERE site_id = $1"}, wantDeleted: false, wantError: false, wantCalls: 0},
{name: "fresh archive", updates: []string{"UPDATE fedwiki.sites SET status = 'active' WHERE site_id = $1", "UPDATE fedwiki.sites SET status = 'archived' WHERE site_id = $1"}, wantDeleted: false, wantError: false, wantCalls: 0},
{name: "readonly", updates: []string{"UPDATE fedwiki.sites SET status = 'readonly' WHERE site_id = $1"}, wantDeleted: false, wantError: false, wantCalls: 0},
}
for _, row := range cases {
t.Run(row.name, func(t *testing.T) { retentionWaitsForSite(t, row) })
@@ -154,14 +154,16 @@ func TestRetentionPurgeWaitsForSiteEligibility_DB(t *testing.T) {
// retentionWaitsForSite commits the table's writer while retention waits on the site row.
func retentionWaitsForSite(t *testing.T, row purgeWaitCase) {
env := newReqEnv(t, 5, 0)
site := expiredPurgeSite(env, "eligibility")
site := archivedPurgeSite(env, "eligibility")
farm := observePurgeFarm(env, nil)
side := env.beginSide(env.lockWaitContext())
side.exec(`SELECT site_id FROM fedwiki.sites WHERE site_id = $1 FOR UPDATE`, site.SiteID)
connection, pid := purgeConnection(t, purgeRepeatableRead(t))
call := startPurge(env, connection, site)
dbtest.AwaitLockWait(t, env.db, dbtest.Backend{PID: pid, Abort: call.abort("purge did not wait for the site row")})
side.exec(row.update, site.SiteID)
for _, update := range row.updates {
side.exec(update, site.SiteID)
}
side.commit()
<-call.done
if call.deleted != row.wantDeleted || (call.err != nil) != row.wantError {
@@ -170,6 +172,7 @@ func retentionWaitsForSite(t *testing.T, row purgeWaitCase) {
if len(farm.calls()) != row.wantCalls {
t.Errorf("%s: purge used eligibility from before the row lock", row.name)
}
checkArchiveShape(env, row.name, site.SiteID)
checkPurgeRows(env, row.name, site.Domain, site.SiteID, true)
}
@@ -177,7 +180,7 @@ func retentionWaitsForSite(t *testing.T, row purgeWaitCase) {
// farm call and prevent a concurrent purge from deleting the same site twice.
func TestRetentionPurgeHoldsLocksThroughFarmDelete_DB(t *testing.T) {
env := newReqEnv(t, 5, 0)
site := expiredPurgeSite(env, "locked")
site := archivedPurgeSite(env, "locked")
arrived, release := make(chan struct{}, 2), make(chan struct{})
var once sync.Once
open := func() { once.Do(func() { close(release) }) }
@@ -17,6 +17,7 @@ import (
"git.coopcloud.tech/wiki-cafe/member-console/internal/integration"
fwmod "git.coopcloud.tech/wiki-cafe/member-console/internal/integrations/fedwiki/store"
"git.coopcloud.tech/wiki-cafe/member-console/internal/testkit/dbtest"
)
// purgeCandidates filters the real list to this fixture; after mutates it between
@@ -24,7 +25,7 @@ import (
type purgeCandidates struct {
fwmod.Querier
ids []string
after func(time.Time, []fwmod.Site) ([]fwmod.Site, error)
after func([]fwmod.Site) ([]fwmod.Site, error)
}
// ListArchivedSitesArchivedBefore takes the real cutoff and preserves stale candidates.
@@ -42,7 +43,7 @@ func (q purgeCandidates) ListArchivedSitesArchivedBefore(ctx context.Context, cu
}
}
if q.after != nil {
return q.after(cutoff.Time, candidates)
return q.after(candidates)
}
return candidates, nil
}
@@ -85,33 +86,69 @@ func (f *purgeFarm) calls() []string {
return slices.Clone(f.deletes)
}
// expiredPurgeSite creates an expired archive with its name and farm content.
// Cleanup refreshes its time so the unfiltered TestRetentionPurge cannot purge
// a surviving fixture on a later run.
func expiredPurgeSite(env *reqEnv, label string) fwmod.Site {
// archivedPurgeSite creates an archive with its placement and farm content.
// Direct purge calls use its stored time plus one microsecond as the cutoff.
func archivedPurgeSite(env *reqEnv, label string) fwmod.Site {
env.t.Helper()
domain := label + "." + env.root
id := env.insertSite(domain, "archived", false)
env.exec(`UPDATE fedwiki.sites SET archived_at = now() - interval '31 days' WHERE site_id = $1`, id)
env.placeServable(domain, id)
env.farm.HoldIn(domain, env.subject, "archived")
env.t.Cleanup(func() { env.exec(`UPDATE fedwiki.sites SET archived_at = now() WHERE domain = $1`, domain) })
return fwmod.Site{SiteID: id, Domain: domain}
site, err := fwmod.New(env.db).GetSiteByID(env.ctx, id)
if err != nil {
env.t.Fatal(err)
}
return fwmod.Site{SiteID: id, Domain: domain, ArchivedAt: site.ArchivedAt}
}
// purgeCandidateCase states the concurrent write and the expected retained state.
type purgeCandidateCase struct {
name string
status string
archiveTime string
remove bool
replace bool
request *purgeRequest
wantRow bool
wantStatus string
wantPlacement bool
wantPurged int
wantUsage int64
consoleRestore bool
name string
status string
rearchive bool
remove bool
replace bool
request *purgeRequest
wantRow bool
wantStatus string
wantPlacement bool
wantPurged int
wantUsage int64
}
// expiredPurgeSite builds an archive aged past the activity's retention window.
func expiredPurgeSite(env *reqEnv, label string) fwmod.Site {
env.t.Helper()
site := archivedPurgeSite(env, label)
ageArchive(env.t, env.db, site.SiteID)
return site
}
// ageArchive moves an archived fixture's time 40 days back. Only this helper
// switches the rule off: it disables, writes and re-enables in one transaction.
// The ALTER TABLE lock prevents other connections from writing sites until
// the commit restores enforcement.
func ageArchive(t *testing.T, database *sql.DB, id string) {
t.Helper()
query := dbtest.Tx(t, database)
query.Exec(`ALTER TABLE fedwiki.sites DISABLE TRIGGER trigger_sites_archived_at`)
query.Int(`UPDATE fedwiki.sites SET archived_at = now() - interval '40 days' WHERE site_id = $1 AND status = 'archived' RETURNING 1`, id)
query.Exec(`ALTER TABLE fedwiki.sites ENABLE TRIGGER trigger_sites_archived_at`)
if err := query.Tx().Commit(); err != nil {
t.Fatal(err)
}
}
// checkArchiveShape requires an archive time exactly when the row is archived.
func checkArchiveShape(env *reqEnv, name, id string) {
env.t.Helper()
var status string
var stamp sql.NullTime
dbtest.On(env.t, env.db).Scan([]any{&status, &stamp}, `SELECT status, archived_at FROM fedwiki.sites WHERE site_id = $1`, id)
if (status == "archived") != stamp.Valid {
env.t.Errorf("%s: writer left an invalid archive time", name)
}
}
// purgeRequest describes an accepted request, its optional hold and its outcome.
@@ -130,9 +167,8 @@ func TestRetentionPurgeRechecksCandidates_DB(t *testing.T) {
{name: "expired", wantPurged: 1},
{name: "active", status: "active", wantRow: true, wantStatus: "active", wantPlacement: true, wantUsage: 1},
{name: "readonly", status: "readonly", wantRow: true, wantStatus: "readonly", wantPlacement: true},
{name: "fresh archive", archiveTime: "fresh", wantRow: true, wantStatus: "archived", wantPlacement: true},
{name: "null archive time", archiveTime: "null", wantRow: true, wantStatus: "archived", wantPlacement: true},
{name: "at cutoff", archiveTime: "cutoff", wantRow: true, wantStatus: "archived", wantPlacement: true},
{name: "fresh archive", rearchive: true, wantRow: true, wantStatus: "archived", wantPlacement: true},
{name: "restored archive time cleared", consoleRestore: true, wantRow: true, wantStatus: "active", wantPlacement: true, wantUsage: 1},
{name: "missing", remove: true, wantPlacement: true},
{name: "replacement", remove: true, replace: true, wantRow: true, wantStatus: "archived", wantPlacement: true},
{name: "open restore before gate", request: &purgeRequest{operation: integration.LifecycleSetStatus, status: "active"}, wantRow: true, wantStatus: "archived", wantPlacement: true},
@@ -153,8 +189,8 @@ func retentionCandidate(t *testing.T, row purgeCandidateCase) {
site := expiredPurgeSite(env, "candidate")
farm := observePurgeFarm(env, nil)
currentID := site.SiteID
env.acts.SiteQ = purgeCandidates{Querier: env.acts.SiteQ, ids: []string{site.SiteID}, after: func(cutoff time.Time, listed []fwmod.Site) ([]fwmod.Site, error) {
currentID = changePurgeCandidate(env, site, row, cutoff)
env.acts.SiteQ = purgeCandidates{Querier: env.acts.SiteQ, ids: []string{site.SiteID}, after: func(listed []fwmod.Site) ([]fwmod.Site, error) {
currentID = changePurgeCandidate(env, site, row)
env.recount()
return listed, nil
}}
@@ -186,19 +222,23 @@ func retentionCandidate(t *testing.T, row purgeCandidateCase) {
}
// changePurgeCandidate applies a writer after listing and returns the current row's ID.
func changePurgeCandidate(env *reqEnv, site fwmod.Site, row purgeCandidateCase, cutoff time.Time) string {
func changePurgeCandidate(env *reqEnv, site fwmod.Site, row purgeCandidateCase) string {
env.t.Helper()
id := site.SiteID
if row.consoleRestore {
if _, err := fwmod.New(env.db).SetSiteStatusByDomain(env.ctx, fwmod.SetSiteStatusByDomainParams{Domain: site.Domain, Status: "active"}); err != nil {
env.t.Fatal(err)
}
}
if row.status != "" {
env.exec(`UPDATE fedwiki.sites SET status = $2 WHERE site_id = $1`, id, row.status)
}
switch row.archiveTime {
case "fresh":
env.exec(`UPDATE fedwiki.sites SET archived_at = now() WHERE site_id = $1`, id)
case "null":
env.exec(`UPDATE fedwiki.sites SET archived_at = NULL WHERE site_id = $1`, id)
case "cutoff":
env.exec(`UPDATE fedwiki.sites SET archived_at = $2 WHERE site_id = $1`, id, cutoff)
if row.rearchive {
env.exec(`UPDATE fedwiki.sites SET status = 'active' WHERE site_id = $1`, id)
env.exec(`UPDATE fedwiki.sites SET status = 'archived' WHERE site_id = $1`, id)
}
if row.status != "" || row.rearchive || row.consoleRestore {
checkArchiveShape(env, row.name, id)
}
if row.remove {
env.exec(`DELETE FROM fedwiki.sites WHERE site_id = $1`, id)
@@ -206,7 +246,7 @@ func changePurgeCandidate(env *reqEnv, site fwmod.Site, row purgeCandidateCase,
}
if row.replace {
id = env.insertSite(site.Domain, "archived", false)
env.exec(`UPDATE fedwiki.sites SET archived_at = now() - interval '31 days' WHERE site_id = $1`, id)
ageArchive(env.t, env.db, id)
}
if row.request != nil {
purgeCandidateRequest(env, site, *row.request)
@@ -242,16 +242,19 @@ func TestRetentionPurge(t *testing.T) {
fresh := "fresh-" + uniq + ".example"
if _, err := database.ExecContext(ctx,
`INSERT INTO fedwiki.sites (workspace_id, domain, status, archived_at) VALUES ($1,$2,'archived',$3)`,
ws, expired, time.Now().Add(-40*24*time.Hour)); err != nil {
`INSERT INTO fedwiki.sites (workspace_id, domain, status) VALUES ($1,$2,'archived')`,
ws, expired); err != nil {
t.Fatalf("insert expired: %v", err)
}
if _, err := database.ExecContext(ctx,
`INSERT INTO fedwiki.sites (workspace_id, domain, status, archived_at) VALUES ($1,$2,'archived',NOW())`,
`INSERT INTO fedwiki.sites (workspace_id, domain, status) VALUES ($1,$2,'archived')`,
ws, fresh); err != nil {
t.Fatalf("insert fresh: %v", err)
}
ageArchive(t, database, siteIDOf(t, database, expired))
acts.SiteQ = purgeCandidates{Querier: acts.SiteQ, ids: []string{siteIDOf(t, database, expired), siteIDOf(t, database, fresh)}}
// Give the expiring site the registry state a real one carries: an
// external claim over its (foreign) name plus an unservable placement.
registry := domains.NewRegistry(database)
@@ -0,0 +1 @@
schema: spec-driven
@@ -0,0 +1,75 @@
## Writers at HEAD
Baseline: `0b34afa8`. All production writes to `fedwiki.sites.status` reach four queries in `internal/integrations/fedwiki/store/queries/sites.sql`.
| Writer | Present path and archive-time behavior |
|---|---|
| Console create | `CreateSite` inserts default `active`, with no timestamp (`sites.sql:6`); `RecordCreatedSiteActivity` calls it (`internal/integrations/fedwiki/workflows/request_activities.go:320`). |
| Farm import | `InsertSiteFromFarm` inserts the observed status, without a timestamp; conflicts do nothing (`sites.sql:88`; `internal/integrations/fedwiki/workflows/sync.go:454`). A first observation of an archived site is already broken. |
| Farm upsert | `UpsertSiteFromFarm` inserts or updates status without touching archive time (`sites.sql:75`; `sync.go:432`, `sync.go:512`). An archive can lack a time; a restore can retain one. |
| Console status, including status reconcile | `SetSiteStatusByDomain` stamps entry and clears exit (`sites.sql:99`; `request_activities.go:379`). Repeating archived preserves even a missing timestamp. |
| Swap | Both writes use `SetSiteStatusByDomain`, parking one site and activating the other (`internal/integrations/fedwiki/workflows/swap.go:182`). |
| Quota park/reactivation | Both use `SetSiteStatusByDomain` (`internal/integrations/fedwiki/workflows/reconcile.go:280`, `reconcile.go:319`). |
| Observation and marks | `SetSiteObservedByDomain`, `SetForceReducedByDomain`, `ClearForceReducedByDomain` update other columns (`sites.sql:178`, `sites.sql:190`, `sites.sql:195`). They must preserve an ongoing archive's time. |
| Migrations and backfills | `internal/integrations/fedwiki/store/migrations/00001_init.sql:36` defines default active and nullable archive time. Versions 2–6 contain no site-status repair. Boot's `internal/integrations/fedwiki/reconcile_domains.go:192` reads sites to backfill placements; it does not write site status. No separate production SQL import, seed or backfill writer was found. |
| Test SQL and builders | `fedwikitest.Site` uses create then status; `SiteModifiedAt` uses the upsert (`internal/integrations/fedwiki/fedwikitest/fedwikitest.go:173`, `:189`). Tests also write raw SQL, including the old archive fixtures in `workflows/purge_requests_db_test.go:91`, `workflows/purge_locks_db_test.go:142`, and `workflows/reconcile_db_test.go:232`. |
## Decisions
### One database trigger owns the timestamp
Per-query CASE expressions fix the known queries but require every future writer to remember the rule. A CHECK that equates archived status with a non-null timestamp catches missing and stray values, but cannot stamp them, compare OLD and NEW, or prevent reuse of an old non-null value. A CHECK alone would also turn today's farm archives into failed writes.
Choose a row trigger, `trigger_sites_archived_at`, calling `fedwiki.set_site_archived_at`, before every INSERT or UPDATE. It changes NEW only, with the caller's privileges, and reads no other table. Its complete rule is:
| Operation | Timestamp stored |
|---|---|
| NEW status is not archived | NULL, regardless of supplied value |
| Insert archived | `pg_catalog.clock_timestamp()`, regardless of supplied value |
| Update from a non-archived status to archived | `pg_catalog.clock_timestamp()`, regardless of supplied value |
| Update while OLD and NEW are archived | OLD.archived_at, regardless of supplied value; if OLD is missing a time, stamp now |
Use the database clock at the write, rather than transaction start, which can precede a lock wait. Farm last-modified time measures content, not when the console observed archival. Repeated observations and timestamp-only writes preserve the archive's original deadline. An upsert runs its insert trigger before resolving a conflict, so its UPDATE branch must use the existing row's status and time. When `InsertSiteFromFarm` finds an existing domain and does nothing, the existing row keeps its timestamp.
Remove the archive CASE from `SetSiteStatusByDomain`; the trigger becomes its sole implementation. Keep force-reduction logic as it is. A second CHECK adds no enforcement for ordinary writes once this trigger owns every resulting value, so do not add one. Runtime writers cannot disable the trigger under the existing DML-only grants (`docs/database-management.md`, Roles). Schema owners can deliberately bypass database enforcement; neither triggers nor CHECK constraints protect against an owner changing the schema.
### Install the rule without a data repair
Add `00007_enforce_site_archive_time.sql` to the FedWiki stream; settle its number at landing. Up creates only the function and trigger; Down drops them. Follow `docs/database-management.md` for qualified names and transactional goose sections, with StatementBegin/StatementEnd around both directions. The function uses `LANGUAGE plpgsql SET search_path = pg_catalog, pg_temp` and the caller's rights, following the row-editing trigger in `internal/db/migrations/00026_outgoing_default_rule.sql:48`. State the archive rule in the migration's comment and the spec, not in the general database-management guide.
Rows written before the trigger may have missing, stray or old archive times; the migration accepts and leaves them unchanged. The next write, including sync's per-pass `SetSiteObservedByDomain` when the sync is enabled (`internal/integrations/fedwiki/workflows/sync.go:487`), fills missing times and clears stray ones; a suspect old non-null time needs manual correction. Count and correct before the migration deploys. After it, a correction must move the row out of and back into `archived` in one transaction. Otherwise the first purge after deployment can delete the suspect row before correction.
Before public launch, deployment-specific repairs belong in manual deployment work, not compatibility migrations. This change therefore neither restarts existing windows nor decides which historical rows a deployment should repair. Removing the trigger requires restoring the prior query implementation too, because the new queries rely on the trigger to maintain archive time.
### Keep retention's locks and cutoff
`ListArchivedSitesArchivedBefore` and `LockArchivedSiteForPurge` keep their status, non-null and strict-before checks (`sites.sql:344`, `sites.sql:350`). `PurgeExpiredArchivedActivity` keeps one cutoff and #245's transaction (`reconcile.go:101`, `reconcile.go:148`). A fresh archive is not eligible at the end of the sync that observes it. Equality with the cutoff remains ineligible.
The trigger takes no additional runtime lock. The order in `docs/database-locks.md` stays instance locks, site rows, usage, request outcomes; the trigger executes within the site's write. Sync still takes no instance lock. Retention holds instance and site through the farm call and commit, then releases the placement separately. No lock-document change is needed.
### Rewrite the fixtures without weakening enforcement
#245's active/readonly rows must now have null timestamps. Replace its `null archive time` and `cleared archive time` cases with real departures from archived; a row that remains archived cannot lose its timestamp. Make `fresh archive` perform archived → active → archived, including in the transaction retention waits for. Check both status and timestamp after the writer, as well as the retained content, row, name and usage.
Keep workflow fixtures on `dbtest.Open`. Tests that call `PurgeExpiredArchivedActivity` need a past timestamp: a test-local helper disables this one trigger, ages only the fixture row, re-enables the trigger and commits in one transaction, following `internal/db/integration_delivery_schema_test.go:417`. The transaction prevents other connections from writing while enforcement is disabled. Filter activity candidates through `purgeCandidates`, including `TestRetentionPurge`, and leave fixture rows for the test database reset; the old timestamp-refresh cleanup would now be ignored.
Tests that call `purgeArchivedSite` directly need no aged row: pass its stored archive time plus one microsecond as the cutoff, captured before starting concurrent work. A later observed rearchive must have a time at or after that cutoff. The workflows package cannot import the canonical migration source list without an import cycle, and maintaining a separate source list would violate `internal/migrate/sources.go:15`; no scratch factory is needed there.
## Failure table
| Path | Today | After |
|---|---|---|
| Create active / import active or readonly | NULL time | Same; any supplied stray time is cleared. |
| Import or upsert inserts archived | NULL time; never selected | Stamped at insertion; expires after its window. |
| Farm upsert enters archived | Missing or stale time | Fresh time, even if a caller supplies an old one. |
| Farm upsert leaves archived | Old time survives | Time cleared in the status write. |
| Console status, swap, quota sweep | Console CASE maintains time | Trigger maintains time; existing transactions and recounts remain. |
| Repeat archived, metadata update, timestamp-only write | Query-dependent; raw SQL can reset time | Original time preserved. |
| Insert conflict does nothing | Existing row survives | Existing row and time survive. |
| Status write or transaction fails | No committed status change | Neither status nor time commits. |
| Sync records a restore, then a rearchive before retention | The old time can survive both writes and cause immediate deletion. | The restore clears the time and the rearchive starts a new window; #245 still checks under both locks. |
| Farm restores and rearchives between observations | The console cannot see the intervening restore. | Unchanged: the row stays archived, keeps its time and may be purged immediately. |
| Migration runs or fails | Existing rows can have missing or stale times. | Installation leaves rows unchanged; failure rolls back the function and trigger creation. |
| Down | Queries maintain archive time inconsistently. | Removing the trigger removes enforcement; restore the prior query implementation with it. |
No user-visible strings are added or changed. No page changes or hand captures.
@@ -0,0 +1,28 @@
## Why
Farm sync writes a site's status without maintaining `archived_at`. A farm archive can therefore never expire, or inherit an earlier archive's expired window and be deleted immediately. The console promises 30 days. Fixing only the sync leaves other SQL writers able to repeat the defect (#246).
## What Changes
- Make the database own archive time for every site insert and update: stamp entry into `archived`, preserve the timestamp while archived, and clear it on exit. Ignore caller-supplied archive times.
- Install the rule with a migration that creates only the function and trigger.
- Remove timestamp maintenance from the console query. Keep #245's locked purge checks and rewrite its fixtures to represent valid transitions.
- Test raw SQL, the query writers, and sync followed by retention; rerun the applicable #245 breaks against its rewritten fixtures.
## Capabilities
### New Capabilities
None.
### Modified Capabilities
- `fedwiki-sites`: database-owned archive time and the start of each observed archive's retention window.
## Impact
One new FedWiki migration, `store/queries/sites.sql` and its generated files, and database and workflow tests. The migration leaves existing rows unchanged. No page, copy, API shape or workflow command changes.
The console cannot detect a farm restore followed by a rearchive between two sync passes. If the row stays archived throughout, the trigger preserves its earlier timestamp and retention may delete it immediately. A new window requires the console to record the intervening restore.
This follows `serialize-retention-purge-with-lifecycle` (#245), whose spec delta is still unarchived. The retention delta here carries its protections forward. Scheduling (#283), placement servability (#247), request lookup (#284), and remote-delete recovery remain separate work.
@@ -0,0 +1,117 @@
## ADDED Requirements
### Requirement: The database maintains each site's archive time
The database SHALL maintain `fedwiki.sites.archived_at` for every INSERT and UPDATE, including farm projection, console status writes, creation, imports and direct SQL. Inserting an archived row or changing a non-archived row to `archived` SHALL stamp the database's current clock time at the write. Every INSERT or UPDATE SHALL leave a row outside `archived` with a NULL archive time. A row that stays archived SHALL keep its original archive time across repeated status writes, metadata writes and attempts to change only the timestamp. Caller-supplied timestamps SHALL NOT override these rules. An archived row missing its time SHALL receive a current time on its next write. Status and archive time SHALL commit or roll back together. Rows written before this rule keep their values until their next write; the migration does not correct them.
#### Scenario: A farm archive starts its window when observed
- **WHEN** sync first inserts an archived farm site or projects an archived status onto an active or readonly site
- **THEN** the database SHALL stamp the archive time at that write
- **AND** retention at the end of that sync SHALL leave the newly archived site intact
#### Scenario: Restore and rearchive receive separate windows
- **WHEN** sync observes a previously archived site as active or readonly
- **THEN** its archive time SHALL be cleared
- **WHEN** a later sync observes that site archived again
- **THEN** its archive time SHALL be newly stamped, independent of the first archive's time
#### Scenario: Console status and swap writes use the same rule
- **WHEN** a console status outcome records an archive, a restore, or a swap activation
- **THEN** the archive SHALL receive a timestamp and the activation SHALL clear any archive timestamp in the transaction that records the status
#### Scenario: Repeated observations preserve the window
- **WHEN** an archived site receives another archived status through an upsert, or receives only storage or last-modified updates
- **THEN** its existing archive time SHALL remain unchanged
- **AND** an insert that does nothing on conflict SHALL leave the existing row's archive time unchanged
#### Scenario: A SQL writer cannot choose the archive window
- **WHEN** a writer inserts or newly archives a site with an old supplied timestamp, clears or replaces the timestamp of a site that remains archived, or supplies a timestamp for an active or readonly row
- **THEN** the database SHALL stamp a new archive at the write, preserve an ongoing archive's original time, or clear the non-archived row's time, respectively
#### Scenario: A failed transaction does not change the window
- **WHEN** a transaction changes a site's status and then rolls back
- **THEN** both status and archive time SHALL retain their values from before the transaction
## MODIFIED Requirements
### Requirement: Member site archival is reversible
A member "Archive" action SHALL move the site to the `archived` state — offline but with content retained — rather than immediately erasing it. Archived sites SHALL be hidden from the member's active sites list and presented in a separate "Archived" section. A member SHALL be able to restore an archived site to `active` (quota-gated). An archived site SHALL be permanently purged (FarmManager `?hard=true`) after a retention window of 30 days only while its current row is still archived, its non-null archive timestamp is strictly before the retention cutoff, and no lifecycle request on its instance is open (`accepted`), or by an explicit "Delete permanently" action confirmed by the member (operators may also purge). Permanent purge is the only irreversible operation.
For archives recorded under this rule, the retention window SHALL start at the database-maintained archive time, regardless of which writer records it. Rows written before this rule keep their values until their next write; the migration does not correct them. Repeated observations SHALL NOT restart that window. A recorded transition out of archived followed by a recorded entry into archived SHALL start a new window.
The retention list SHALL supply candidates, not authority to delete. For each candidate, the purge SHALL take the lifecycle instance lock and then lock the eligible site row by its immutable identifier in a READ COMMITTED transaction. It SHALL check open requests after those locks and SHALL keep both locks through the farm hard delete and local row deletion's commit. A missing or ineligible row or an open request SHALL be skipped until a later tick; a failed eligibility or request read SHALL make no farm call. The local delete SHALL use the locked site's identifier. Only a successful local commit SHALL count as purged and permit placement release. An eligible archived deletion SHALL leave usage unchanged. A farm failure or local transaction failure SHALL leave cleanup to a later eligible attempt; the console SHALL NOT treat a database rollback as reversing a remote deletion.
#### Scenario: Archive moves the site to the archived state
- **WHEN** a member archives a site
- **THEN** the site SHALL move to `archived` with its content retained
- **AND** the site SHALL be restorable
- **AND** the site SHALL no longer appear in the active sites list
#### Scenario: Restore from archived is quota-gated
- **WHEN** a member restores an archived site and the pool's active sites and holds are below the limit
- **THEN** the restore's gate SHALL hold a slot, and the site SHALL return to `active` and count toward the pool as an active row
- **WHEN** the pool's active sites and holds already reach the limit
- **THEN** the restore SHALL be refused
#### Scenario: Archived site is purged after retention
- **WHEN** an archived site's 30-day retention window has elapsed and its locked row is still eligible with no accepted lifecycle request
- **THEN** the system SHALL permanently purge it via a hard delete
#### Scenario: Explicit delete-permanently purges immediately
- **WHEN** a member confirms "Delete permanently" on an archived site
- **THEN** the system SHALL permanently purge it via a hard delete
#### Scenario: A restore is in flight when retention reaches the site
- **WHEN** the retention list contains an expired archived site whose restore request was accepted before the purge acquired its instance lock
- **AND** the restore's farm call is still in progress
- **THEN** retention SHALL skip the site without a hard delete, local deletion, placement release or usage change
- **AND** the restore SHALL retain its slot hold and be able to record its outcome
#### Scenario: A restore finishes after the candidate list
- **WHEN** a restore makes a listed candidate active before the purge acquires its site row lock
- **THEN** retention SHALL leave the site and its placement intact and SHALL leave the restore's usage figure unchanged
#### Scenario: The archive time changes after the candidate list
- **WHEN** a listed candidate leaves archived and clears its timestamp, or leaves and reenters archived with a new time at or after the cutoff, before the purge locks its row
- **THEN** retention SHALL send no hard delete and SHALL leave the row for a later tick
#### Scenario: Archive time equal to the cutoff is not expired
- **WHEN** an archived site's timestamp equals the retention cutoff
- **THEN** neither the candidate list nor the locked eligibility check SHALL select it for deletion
#### Scenario: A replacement under the same name is not a purge candidate
- **WHEN** a candidate's identifier no longer has a row and its former domain now belongs to a different site identifier
- **THEN** retention SHALL send no hard delete for that candidate and SHALL leave the replacement's row and placement intact
#### Scenario: Retention holds its decision through the farm call
- **WHEN** retention has locked an eligible site with no accepted request and is waiting for the farm hard delete
- **THEN** new lifecycle intake on that instance and status writes on that row SHALL wait until the purge transaction ends
- **AND** a concurrent purge SHALL check eligibility again after acquiring the instance lock
#### Scenario: A failed check cannot authorize deletion
- **WHEN** retention cannot begin its transaction, acquire its locks or read eligibility or open requests
- **THEN** it SHALL send no farm delete for that candidate and SHALL report the failure rather than treat it as eligibility
#### Scenario: A failed candidate does not prevent another purge
- **WHEN** one eligible candidate's farm delete fails and the next candidate is eligible and can be deleted
- **THEN** the first candidate's row and placement SHALL remain and the second candidate SHALL be purged
- **AND** the reported purge count SHALL include only successful local commits
@@ -0,0 +1,59 @@
Tests follow `docs/testing.md`: `dbtest` opens databases, existing builders create related rows, and assertions filter to fixture IDs. Schema tests write SQL directly, under `SET LOCAL ROLE console_runtime` for runtime writes. Tests pass twice without reset. Lock tests use channels and `dbtest.AwaitLockWait`, never sleeps; assertions run on the test goroutine and cleanup releases waits before joining workers.
Implementation, tests and documents form one commit. The caller handles landing: the full suite, break runner, captures only if a page changes, and commit. No golden, page, route, workflow command or screen baseline changes are expected; investigate unexpected differences. Learn scripts from their source.
**Breaks.** Phase 3 writes the section 4 records, including the retained #245 records, to **a file outside the repository**, at the path the caller gives. Use `scripts/mutation-check/main.go`'s format. New tests have only a new side. Every `want_new` below is assertion text printed only on failure. Apply each break, `go vet` its package, restore it. Migration breaks use test records against the migration file, caught by store tests that apply it to fresh scratch databases. The caller runs the list after green controls and expects every retained record CAUGHT.
## 1. Database and query
- [x] 1.1 Add `internal/integrations/fedwiki/store/migrations/00007_enforce_site_archive_time.sql`. Up creates only `fedwiki.set_site_archived_at` and its BEFORE INSERT OR UPDATE row trigger; Down drops them. Declare the function `LANGUAGE plpgsql SET search_path = pg_catalog, pg_temp`, with the caller's rights, following `internal/db/migrations/00026_outgoing_default_rule.sql:48`. Wrap both directions in goose StatementBegin/StatementEnd. State the archive rule in the migration's comment and the spec. No data repair, explicit table lock, new grants or runtime bypass.
- [x] 1.2 Ensure no site query assigns `archived_at`; only the two retention predicates read it. At this HEAD, remove its CASE from `SetSiteStatusByDomain` in `store/queries/sites.sql`. Keep returned site columns, both retention predicates and force-reduction behavior. Resolve the query names against #284's move to lookup by site ID at landing. Run `make sqlc-generate`, including its header restoration, and retain only relevant generated differences. No edits to the general database-management or lock guides are needed.
## 2. Database tests
- [x] 2.1 Add `store/archive_time_db_test.go` in external package `fedwiki_test`, which can import `migrate.Sources()` without the workflows package's import cycle. `TestSiteArchiveTime_DB` builds a fully migrated scratch database using that canonical source list and runs named table rows through a helper: `insert archived without time`, `insert archived with old time`, `insert active with time`, `insert readonly with time`, `active to archived`, `readonly to archived`, `archived to active`, `archived to readonly`, `repeat archived`, `metadata only`, `clear time only`, `replace time only`, `archive rollback`. Exercise raw SQL as `console_runtime`; both entry updates also supply an old timestamp. Bound new times by database clock reads immediately around the write, after the transaction has begun, compare preserved timestamps exactly, require NULL outside archived, and assert rolled-back status and time equal their starting values. Failure texts: `%s: archive time = %v, want a time within the write`; `%s: archive time = %v, want NULL`; `%s: archive time changed from %v to %v`; `rollback changed status or archive time`.
- [x] 2.2 `TestSiteArchiveTimeQueries_DB` uses the same scratch-database setup as 2.1, not `dbtest.Open`, and runs `CreateSite` (active/NULL), `InsertSiteFromFarm` (all three states and conflict-do-nothing), `UpsertSiteFromFarm` (all three insert states, archive/restore/rearchive and repeated archived conflict update), `SetSiteStatusByDomain` (both directions and repeated archive), and `SetSiteObservedByDomain` (archived metadata update). Assert returned and stored timestamps agree; conflicts preserve workspace and time; archive repeats preserve time; reentry stamps anew. Use separate transactions and database bounds rather than sleeps. Failure texts: `%s: returned and stored archive times differ`; `%s: archive time violates transition`; `conflict changed the existing row`; `repeated upsert restarted the archive window`.
- [x] 2.3 `TestArchiveTimeMigration_DB` uses `migrate.Sources()` to migrate a scratch database with FedWiki through version 6. Insert an archived row with NULL time and an active row with an old time before applying version 7. Require installation to leave both unchanged, then make one metadata write per row and assert the archived row gains a current time and the active row has none; failure text `%s: legacy row not corrected on its next write`. Apply version 7 Down through `dbtest.ApplyVersion`; require the function and trigger to be gone, with failure text `down left archive enforcement installed`.
## 3. Workflow tests and #245 fixtures
- [x] 3.1 Keep workflow fixtures on `dbtest.Open`, including `newReqEnv` and `purgeRepeatableRead`. Split archive creation from aging in the purge helpers. Only activity tests age rows: disable `trigger_sites_archived_at`, update the fixture row's time, re-enable the trigger and commit in one transaction, with deferred rollback on failure (`internal/db/integration_delivery_schema_test.go:417`). This covers `TestRetentionPurge`, the candidate and restore-in-flight activity tests, and the activity rows of `TestRetentionPurgeFailures_DB`. Filter `TestRetentionPurge` through `purgeCandidates` too. Remove timestamp-refresh cleanup; leave fixture rows for the test database reset. Direct helper calls in `purge_locks_db_test.go` and `purge_failures_db_test.go` use the stored archive time plus one microsecond as their cutoff, read before concurrent work starts; they do not age rows. No scratch factory, duplicate migration source list or application clock is needed.
- [x] 3.2 Rewrite `TestRetentionPurgeRechecksCandidates_DB`: `active` and `readonly` assert NULL after the status write; `fresh archive` leaves and reenters archived; replace `null archive time` with `restored archive time cleared`, an actual active transition. Keep all request, missing, expired and replacement cases. Age the replacement with the transaction helper so the immutable-ID check is still tested independently of its age, and leave both fixtures for the test database reset. Move `at cutoff` to item 3.4. Keep existing failure texts and add `%s: writer left an invalid archive time`.
- [x] 3.3 Rewrite `TestRetentionPurgeWaitsForSiteEligibility_DB`: `active` clears its time; `fresh archive` executes two status writes in the side transaction; replace `cleared archive time` with `readonly`, which leaves archived. Keep the READ COMMITTED override test, actual row-lock wait, no farm DELETE and nil-error assertions. Add `%s: writer left an invalid archive time`. Retain all other #245 lock, intake, restore-in-flight and failure tests unchanged in behavior. The historical #245 change text stays a record of its implementation; this change supersedes the malformed fixtures it explicitly assigned to #246.
- [x] 3.4 Add `TestArchiveRetentionBoundary_DB` in `workflows/archive_time_db_test.go`, on the shared test database through `dbtest.Open`: rows `before cutoff`, `at cutoff`, `after cutoff` describe the archive time relative to the cutoff. Archive a row, read its stored time, and pass a cutoff one microsecond later, equal, or one microsecond earlier, respectively. Exercise both `ListArchivedSitesArchivedBefore` (filter to fixture IDs) and `purgeArchivedSite` on separate rows. Only `before cutoff` deletes. Failure texts: `%s: retention candidates = %v, want %v`; `%s: retention deleted = %t, want %t`. Keep the real 30-day activity covered by `TestRetentionPurge`.
- [x] 3.5 Add `TestSyncArchiveWindow_DB` in that file, on `dbtest.Open`. Run real `SyncSitesToDBActivity` and then `PurgeExpiredArchivedActivity`, filtering purge candidates to fixture IDs. Rows: `first seen archived`, `farm archives active`, `farm restores archived`, `farm rearchives restored`, `repeat archived observation`. Use the fake farm and item 3.1's age helper to start the restore/rearchive case with an expired first archive; assert the restore sync clears time before the next sync, rearchive gets a current time, repeated observation keeps it, and fresh archives survive retention with no hard DELETE and their rows/placements intact. Also age a farm-observed archive past 30 days and require deletion and name release on the next purge. Leave fixture rows for the test database reset. Failure texts: `%s: sync archive time violates transition`; `%s: sync retention hard deletes = %v, want %v`; `%s: sync retention left wrong rows or placements`. Check usage against the fixture's active rows and holds; failure `%s: sync retention usage differs from counted rows`.
- [x] 3.6 Add `TestRecordArchiveTime_DB` in `workflows/archive_time_db_test.go`, using `reqEnv` and its request builders: rows `archive` (active site), `restore` (archived site), `swap activation` (archived target and active incumbent). Run `RecordSiteStatusActivity` for the first two and `RecordSwapActivity` for the third; require successful request outcomes, correct statuses and recounted usage. Assert the archive stamps and both activations clear archive time, with failure texts `recorded archive has no archive time` and `recorded activation retained archive time`. Make state assertions nonfatal so the timestamp failure remains visible under a break. No new golden files; no edits to existing goldens.
## 4. Deliberate breaks
Each migration edit below targets version 7 and runs the named store test on a newly migrated scratch database. Expand rows that name two tests into two records. Resolve exact replacement text against the implementation before writing the external list. Resolve item 3.6's status-query breaks again at landing, because #284 changes the record queries to look up sites by ID; keep the same faulty status behavior and failure text regardless of query name or parameter order.
- [x] 4.1 Carry the 16 records from `openspec/changes/serialize-retention-purge-with-lifecycle/tasks.md:59` into the phase 3 break-list work. Retire **`current status`** and **`null archive time`** by name: the trigger clears times outside archived and supplies them inside archived, so neither mutation can fail these fixtures. Put the other 14 records in this change's external list, resolve their replacement text against HEAD, and vet and rerun all of them. Preserve their test patterns and failure texts except `strict cutoff`, which now runs `TestArchiveRetentionBoundary_DB/at_cutoff` with `want_new` `at cutoff: retention deleted =`.
The retained names are `current archive time`, `strict cutoff`, `candidate identity`, `open request skips`, `lifecycle instance lock`, `request snapshot after waiting`, `row update is a successful skip`, `site row lock`, `locks last through HTTP`, `local deletion`, `placement release`, `continue after candidate failure`, `farm refusal keeps the row`, and `404 is success`. Their source records supply the exact tests and failure messages. Do not recreate invalid rows to make the two retired records fail.
- [x] 4.2 Add these records to the same external list:
| Break | Test / row | `want_new` |
|---|---|---|
| Trigger UPDATE only, omitting INSERT | `TestSiteArchiveTime_DB/insert_archived_without_time` | `want a time within the write` |
| New archive uses caller's old value instead of database time | `TestSiteArchiveTime_DB/insert_archived_with_old_time` | `want a time within the write` |
| Non-archived branch preserves NEW.archived_at | `TestSiteArchiveTime_DB/archived_to_active` and `/insert_readonly_with_time` | `want NULL` |
| Entry from readonly fails to stamp | `TestSiteArchiveTime_DB/readonly_to_archived` | `want a time within the write` |
| Entry update uses transaction-start time instead of clock time | `TestSiteArchiveTime_DB/active_to_archived` | `want a time within the write` |
| Repeated archived branch stamps again | `TestSiteArchiveTimeQueries_DB` | `repeated upsert restarted the archive window` |
| Trigger fires only on UPDATE OF status, allowing timestamp-only changes | `TestSiteArchiveTime_DB/clear_time_only` and `/replace_time_only` | `archive time changed from` |
| Archived repeat uses `OLD.archived_at` without its COALESCE fallback | `TestArchiveTimeMigration_DB` | `legacy row not corrected on its next write` |
| Down leaves the trigger/function installed (valid no-op SQL) | `TestArchiveTimeMigration_DB` | `down left archive enforcement installed` |
| Upsert's generated SQL omits its conflict status assignment | `TestSyncArchiveWindow_DB/farm_archives_active` | `sync archive time violates transition` |
| Generated listing changes `<` to `<=` | `TestArchiveRetentionBoundary_DB/at_cutoff` | `retention candidates =` |
| Generated status query replaces `SET status = $2` with `SET status = CASE WHEN $2 = 'active' THEN status ELSE $2 END` | `TestRecordArchiveTime_DB/restore` and `/swap_activation` | `recorded activation retained archive time` |
| Generated status query keeps status active when asked to archive, retaining both parameters | `TestRecordArchiveTime_DB/archive` | `recorded archive has no archive time` |
Rollback atomicity is a database control; no artificial production seam is added to break it. The inherited `current archive time` and `strict cutoff` records cover the locked query's time predicates, so section 4.2 does not duplicate them.
## 5. Validation and landing
- [x] 5.1 Phase 3: reset the worktree test databases after the migration using the documented script, then run the store and workflows packages separately with `-count=1 -p 1`, twice without another reset. Run workflows with `-race` for the rewritten lock tests. Prepare and vet section 4's breaks; leave full-suite and mutation runs to the caller.
- [x] 5.2 `openspec validate enforce-archive-time-in-database --strict` and `make lint` pass; no notebook citation. Check migration numbering against the FedWiki stream at landing.
- [x] 5.3 Caller: run `make test` with the stack up, then the external break list; record results and limits in the commit message. Hand captures only if a page changes. Commit without archiving either change until requested; when archiving, apply #245 before this delta so its retention protections survive.