feat(postcard): sample values, file hashes, index sync and the draft editor

- sample.<path> header keys and a parse-tree walker that fills every other
  variable with a visible [path] placeholder, nested fields and range
  targets included
- every file entry carries the sha256 of its CRLF-normalized text; the
  catalog lists its files (Templates, Layouts, Partials) for admin code
- SyncIndex upserts one index row per file and never touches the override
  columns of a customized row; ResetOverride returns a row to its file
- Editor validates, previews and test-sends a template, layout or partial
  draft through the render path of Send and reports field and rule
- pact.RowStateCustomized and pact.RowStateOutdated

Plan 35-01, task 4.
This commit is contained in:
Jakub Zych
2026-11-13 10:57:05 +01:00
parent 5df1ec27cf
commit 495319ee21
11 changed files with 2128 additions and 42 deletions

View File

@@ -264,3 +264,162 @@ WHERE t.code = ? AND t.is_custom`, name).Scan(&rows).Error
}
return out, nil
}
// syncLockKey names the advisory lock that serializes SyncIndex runs.
const syncLockKey = "summercms.postcard.sync"
// SyncIndex gives every template, layout and partial of cat its index row,
// keyed by code, so that admin lists, forms and relations can work on rows.
// It runs layouts first, then templates (whose layout_id points at the
// layout row of the file's layout), then partials.
//
// For a row that is not customized the content columns mirror the file. For
// a customized row they are left alone: only source, description, locale,
// name and file_hash follow the file, which is what makes a changed file
// show as a difference between base_hash and file_hash. No row is deleted,
// database-only layouts are not touched, and a row that already matches is
// not written. locales are the application's locales, for TemplateLocale.
//
// It is safe to call from several processes at once. It fails when the
// tables are not migrated yet; the caller decides when to try again.
func SyncIndex(ctx context.Context, db *gorm.DB, cat *Catalog, locales []string) error {
if db == nil {
return fmt.Errorf("postcard: sync index: database is nil")
}
if cat == nil {
return fmt.Errorf("postcard: sync index: catalog is nil")
}
layouts, templates, partials := cat.Layouts(), cat.Templates(), cat.Partials()
err := db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Exec(`SELECT pg_advisory_xact_lock(hashtext(?))`, syncLockKey).Error; err != nil {
return err
}
for _, l := range layouts {
if err := tx.Exec(`INSERT INTO `+LayoutsTable+` AS t (code, source, name, content_html, content_text, content_css, file_hash)
VALUES (?, ?, ?, ?, ?, '', ?)
ON CONFLICT (code) DO UPDATE SET
source = EXCLUDED.source,
name = EXCLUDED.name,
file_hash = EXCLUDED.file_hash,
content_html = CASE WHEN t.is_custom THEN t.content_html ELSE EXCLUDED.content_html END,
content_text = CASE WHEN t.is_custom THEN t.content_text ELSE EXCLUDED.content_text END,
content_css = CASE WHEN t.is_custom THEN t.content_css ELSE EXCLUDED.content_css END,
updated_at = NOW()
WHERE t.source IS DISTINCT FROM EXCLUDED.source
OR t.name IS DISTINCT FROM EXCLUDED.name
OR t.file_hash IS DISTINCT FROM EXCLUDED.file_hash
OR (NOT t.is_custom AND (t.content_html, t.content_text, t.content_css)
IS DISTINCT FROM (EXCLUDED.content_html, EXCLUDED.content_text, EXCLUDED.content_css))`,
l.Name, l.PluginID, rowName(l.Title, l.Name), l.HTML, l.Text, l.Hash).Error; err != nil {
return err
}
}
for _, f := range templates {
if err := tx.Exec(`INSERT INTO `+TemplatesTable+` AS t (code, source, locale, description, subject, body, layout_id, file_hash)
VALUES (?, ?, NULLIF(?, ''), ?, ?, ?, (SELECT id FROM `+LayoutsTable+` WHERE code = ?), ?)
ON CONFLICT (code) DO UPDATE SET
source = EXCLUDED.source,
locale = EXCLUDED.locale,
description = EXCLUDED.description,
file_hash = EXCLUDED.file_hash,
subject = CASE WHEN t.is_custom THEN t.subject ELSE EXCLUDED.subject END,
body = CASE WHEN t.is_custom THEN t.body ELSE EXCLUDED.body END,
layout_id = CASE WHEN t.is_custom THEN t.layout_id ELSE EXCLUDED.layout_id END,
updated_at = NOW()
WHERE t.source IS DISTINCT FROM EXCLUDED.source
OR t.locale IS DISTINCT FROM EXCLUDED.locale
OR t.description IS DISTINCT FROM EXCLUDED.description
OR t.file_hash IS DISTINCT FROM EXCLUDED.file_hash
OR (NOT t.is_custom AND (t.subject, t.body, t.layout_id)
IS DISTINCT FROM (EXCLUDED.subject, EXCLUDED.body, EXCLUDED.layout_id))`,
f.Name, f.PluginID, TemplateLocale(f.Name, locales), f.Description, f.Subject, f.Body, f.Layout, f.Hash).Error; err != nil {
return err
}
}
for _, p := range partials {
if err := tx.Exec(`INSERT INTO `+PartialsTable+` AS t (code, source, name, content_html, content_text, file_hash)
VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT (code) DO UPDATE SET
source = EXCLUDED.source,
name = EXCLUDED.name,
file_hash = EXCLUDED.file_hash,
content_html = CASE WHEN t.is_custom THEN t.content_html ELSE EXCLUDED.content_html END,
content_text = CASE WHEN t.is_custom THEN t.content_text ELSE EXCLUDED.content_text END,
updated_at = NOW()
WHERE t.source IS DISTINCT FROM EXCLUDED.source
OR t.name IS DISTINCT FROM EXCLUDED.name
OR t.file_hash IS DISTINCT FROM EXCLUDED.file_hash
OR (NOT t.is_custom AND (t.content_html, t.content_text)
IS DISTINCT FROM (EXCLUDED.content_html, EXCLUDED.content_text))`,
p.Name, p.PluginID, rowName(p.Title, p.Name), p.HTML, p.Text, p.Hash).Error; err != nil {
return err
}
}
return nil
})
if err != nil {
return fmt.Errorf("postcard: sync index: %w", err)
}
return nil
}
// rowName is the name column of a layout or partial row: the name in the
// file's header, or the full name when the header has none.
func rowName(title, full string) string {
if title != "" {
return title
}
return full
}
// ResetOverride returns one file-backed row to its file version: the content
// columns mirror the file again and is_custom, base_hash and customized_at
// are cleared. kind is KindTemplate, KindLayout or KindPartial and code the
// row's code. A database-only layout has no file version: it is refused, and
// so is a code cat does not hold.
func ResetOverride(ctx context.Context, db *gorm.DB, cat *Catalog, kind Kind, code string) error {
if db == nil {
return fmt.Errorf("postcard: reset %s: database is nil", code)
}
if cat == nil {
return fmt.Errorf("postcard: reset %s: catalog is nil", code)
}
db = db.WithContext(ctx)
var res *gorm.DB
switch kind {
case KindTemplate:
f, ok := cat.Template(code)
if !ok {
return fmt.Errorf("postcard: reset %s: no file version", code)
}
res = db.Exec(`UPDATE `+TemplatesTable+` SET subject = ?, body = ?,
layout_id = (SELECT id FROM `+LayoutsTable+` WHERE code = ?),
file_hash = ?, is_custom = FALSE, base_hash = NULL, customized_at = NULL, updated_at = NOW()
WHERE code = ? AND source IS NOT NULL`, f.Subject, f.Body, f.Layout, f.Hash, code)
case KindLayout:
f, ok := cat.Layout(code)
if !ok {
return fmt.Errorf("postcard: reset %s: no file version", code)
}
res = db.Exec(`UPDATE `+LayoutsTable+` SET name = ?, content_html = ?, content_text = ?, content_css = '',
file_hash = ?, is_custom = FALSE, base_hash = NULL, customized_at = NULL, updated_at = NOW()
WHERE code = ? AND source IS NOT NULL`, rowName(f.Title, f.Name), f.HTML, f.Text, f.Hash, code)
case KindPartial:
f, ok := cat.Partial(code)
if !ok {
return fmt.Errorf("postcard: reset %s: no file version", code)
}
res = db.Exec(`UPDATE `+PartialsTable+` SET name = ?, content_html = ?, content_text = ?,
file_hash = ?, is_custom = FALSE, base_hash = NULL, customized_at = NULL, updated_at = NOW()
WHERE code = ? AND source IS NOT NULL`, rowName(f.Title, f.Name), f.HTML, f.Text, f.Hash, code)
default:
return fmt.Errorf("postcard: reset %s: kind %q has no file version", code, kind)
}
if res.Error != nil {
return fmt.Errorf("postcard: reset %s: %w", code, res.Error)
}
if res.RowsAffected == 0 {
return fmt.Errorf("postcard: reset %s: no index row", code)
}
return nil
}