From c66fd427b249ff66248d5e1aaa55b9cba590126c Mon Sep 17 00:00:00 2001 From: tgiachi Date: Tue, 10 Nov 2026 15:07:11 +0100 Subject: [PATCH] fix(store): add the outbox post_id and priority columns separately The two columns were added in one non-transactional Exec guarded by post_id alone. A crash between the two statements left a database with post_id but no priority, and every later start failed at CREATE INDEX outbox_due_prio. Each column is now checked and added on its own, as the members columns are. --- internal/store/m2_test.go | 22 ++++++++++++++++++++++ internal/store/store.go | 8 ++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/internal/store/m2_test.go b/internal/store/m2_test.go index 79765bf..1cfc525 100644 --- a/internal/store/m2_test.go +++ b/internal/store/m2_test.go @@ -717,3 +717,25 @@ func TestConfirmPendingIsSingleUseUnderConcurrency(t *testing.T) { } } } + +func TestMigrationAddsTheOutboxPriorityAloneAfterAHalfDoneUpgrade(t *testing.T) { + // A crash between adding post_id and priority leaves outbox with the first and not the second. + path := t.TempDir() + "/old.db" + s, _ := Open(path) + if _, err := s.db.Exec(`DROP INDEX outbox_due_prio`); err != nil { + t.Fatal(err) + } + if _, err := s.db.Exec(`ALTER TABLE outbox DROP COLUMN priority`); err != nil { + t.Fatal(err) + } + s.Close() + + s2, err := Open(path) + if err != nil { + t.Fatalf("migrate: %v", err) + } + defer s2.Close() + if ok, err := hasColumn(s2.db, "outbox", "priority"); err != nil || !ok { + t.Fatalf("priority is added back: %v %v", ok, err) + } +} diff --git a/internal/store/store.go b/internal/store/store.go index bfe150e..6f74622 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -325,8 +325,12 @@ func migrate(db *sql.DB) error { } } if has, _ := hasColumn(db, "outbox", "post_id"); !has { - if _, err := db.Exec(`ALTER TABLE outbox ADD COLUMN post_id INTEGER NOT NULL DEFAULT 0; - ALTER TABLE outbox ADD COLUMN priority INTEGER NOT NULL DEFAULT 0;`); err != nil { + if _, err := db.Exec(`ALTER TABLE outbox ADD COLUMN post_id INTEGER NOT NULL DEFAULT 0`); err != nil { + return err + } + } + if has, _ := hasColumn(db, "outbox", "priority"); !has { + if _, err := db.Exec(`ALTER TABLE outbox ADD COLUMN priority INTEGER NOT NULL DEFAULT 0`); err != nil { return err } }