fix: write API posts in one transaction and fire RecordSaved after its commit
POST and PUT /_journal/api/v1/posts saved the row, then the categories, tags and translations, and fired RecordSaved only when all succeeded. A failure part-way left a committed row (possibly a new slug) with no event, so subscribers such as an SEO plugin never created the redirect. The write now runs in one lagoon transaction with the event emitted inside it.
This commit is contained in:
@@ -239,6 +239,24 @@ func TestAPIRecordEvents(t *testing.T) {
|
||||
t.Fatalf("PUT described URL = %q", got[0].url)
|
||||
}
|
||||
|
||||
// A write that fails after the row is saved rolls the row back and
|
||||
// fires nothing (WR-04).
|
||||
l.reset()
|
||||
failed := env.call(t, http.MethodPut, "/_journal/api/v1/posts/"+id, token, map[string]any{
|
||||
"slug": "api-lost",
|
||||
"tags": "not a list",
|
||||
})
|
||||
if failed.Code != http.StatusInternalServerError {
|
||||
t.Fatalf("failing PUT = %d, want 500: %s", failed.Code, failed.Body.String())
|
||||
}
|
||||
if saved := l.savedEvents(); len(saved) != 0 {
|
||||
t.Fatalf("failing PUT fired RecordSaved: %+v", saved)
|
||||
}
|
||||
var slug string
|
||||
if err := env.gdb.Raw("SELECT slug FROM golem15_journal_posts WHERE id = ?", id).Scan(&slug).Error; err != nil || slug != "api-new" {
|
||||
t.Fatalf("slug after failing PUT = %q (err %v), want api-new", slug, err)
|
||||
}
|
||||
|
||||
l.reset()
|
||||
env.expect(t, http.StatusOK, http.MethodDelete, "/_journal/api/v1/posts/"+id, token, nil)
|
||||
if saved := l.savedEvents(); len(saved) != 0 {
|
||||
|
||||
Reference in New Issue
Block a user