Implement complete collection persistence with database-backed survival across server restarts

• Add full multi-table schema for collections with normalized design (collections, collection_templates, collection_items, collection_item_versions)
• Implement collection detection and processing in enhancement pipeline for .insertr-add elements
• Add template extraction and storage from existing HTML children with multi-variant support
• Enable collection reconstruction from database on server restart with proper DOM rebuilding
• Extend ContentClient interface with collection operations and full database integration
• Update enhance command to use engine.DatabaseClient for collection persistence support
This commit is contained in:
2025-09-22 18:29:58 +02:00
parent b25663f76b
commit 2315ba4750
36 changed files with 4356 additions and 46 deletions

View File

@@ -9,6 +9,80 @@ import (
"context"
)
const createCollectionItemVersionsLookupIndex = `-- name: CreateCollectionItemVersionsLookupIndex :exec
CREATE INDEX IF NOT EXISTS idx_collection_item_versions_lookup ON collection_item_versions(item_id, collection_id, site_id, created_at DESC)
`
func (q *Queries) CreateCollectionItemVersionsLookupIndex(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, createCollectionItemVersionsLookupIndex)
return err
}
const createCollectionItemsLookupIndex = `-- name: CreateCollectionItemsLookupIndex :exec
CREATE INDEX IF NOT EXISTS idx_collection_items_lookup ON collection_items(collection_id, site_id, position)
`
func (q *Queries) CreateCollectionItemsLookupIndex(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, createCollectionItemsLookupIndex)
return err
}
const createCollectionItemsTemplateIndex = `-- name: CreateCollectionItemsTemplateIndex :exec
CREATE INDEX IF NOT EXISTS idx_collection_items_template ON collection_items(template_id)
`
func (q *Queries) CreateCollectionItemsTemplateIndex(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, createCollectionItemsTemplateIndex)
return err
}
const createCollectionTemplatesDefaultIndex = `-- name: CreateCollectionTemplatesDefaultIndex :exec
CREATE INDEX IF NOT EXISTS idx_collection_templates_default ON collection_templates(collection_id, site_id, is_default DESC)
`
func (q *Queries) CreateCollectionTemplatesDefaultIndex(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, createCollectionTemplatesDefaultIndex)
return err
}
const createCollectionTemplatesLookupIndex = `-- name: CreateCollectionTemplatesLookupIndex :exec
CREATE INDEX IF NOT EXISTS idx_collection_templates_lookup ON collection_templates(collection_id, site_id)
`
func (q *Queries) CreateCollectionTemplatesLookupIndex(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, createCollectionTemplatesLookupIndex)
return err
}
const createCollectionTemplatesOneDefaultIndex = `-- name: CreateCollectionTemplatesOneDefaultIndex :exec
CREATE UNIQUE INDEX IF NOT EXISTS idx_collection_templates_one_default
ON collection_templates(collection_id, site_id)
WHERE is_default = TRUE
`
func (q *Queries) CreateCollectionTemplatesOneDefaultIndex(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, createCollectionTemplatesOneDefaultIndex)
return err
}
const createCollectionsSiteIndex = `-- name: CreateCollectionsSiteIndex :exec
CREATE INDEX IF NOT EXISTS idx_collections_site_id ON collections(site_id)
`
func (q *Queries) CreateCollectionsSiteIndex(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, createCollectionsSiteIndex)
return err
}
const createCollectionsUpdatedAtIndex = `-- name: CreateCollectionsUpdatedAtIndex :exec
CREATE INDEX IF NOT EXISTS idx_collections_updated_at ON collections(updated_at)
`
func (q *Queries) CreateCollectionsUpdatedAtIndex(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, createCollectionsUpdatedAtIndex)
return err
}
const createContentSiteIndex = `-- name: CreateContentSiteIndex :exec
CREATE INDEX IF NOT EXISTS idx_content_site_id ON content(site_id)
`
@@ -51,6 +125,83 @@ func (q *Queries) CreateVersionsLookupIndex(ctx context.Context) error {
return err
}
const initializeCollectionItemVersionsTable = `-- name: InitializeCollectionItemVersionsTable :exec
CREATE TABLE IF NOT EXISTS collection_item_versions (
version_id SERIAL PRIMARY KEY,
item_id TEXT NOT NULL,
collection_id TEXT NOT NULL,
site_id TEXT NOT NULL,
html_content TEXT NOT NULL,
template_id INTEGER NOT NULL,
position INTEGER NOT NULL,
created_at BIGINT DEFAULT (EXTRACT(EPOCH FROM NOW())) NOT NULL,
created_by TEXT DEFAULT 'system' NOT NULL,
FOREIGN KEY (item_id, collection_id, site_id) REFERENCES collection_items(item_id, collection_id, site_id) ON DELETE CASCADE
)
`
func (q *Queries) InitializeCollectionItemVersionsTable(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, initializeCollectionItemVersionsTable)
return err
}
const initializeCollectionItemsTable = `-- name: InitializeCollectionItemsTable :exec
CREATE TABLE IF NOT EXISTS collection_items (
item_id TEXT NOT NULL,
collection_id TEXT NOT NULL,
site_id TEXT NOT NULL,
template_id INTEGER NOT NULL,
html_content TEXT NOT NULL,
position INTEGER NOT NULL,
created_at BIGINT DEFAULT (EXTRACT(EPOCH FROM NOW())) NOT NULL,
updated_at BIGINT DEFAULT (EXTRACT(EPOCH FROM NOW())) NOT NULL,
last_edited_by TEXT DEFAULT 'system' NOT NULL,
PRIMARY KEY (item_id, collection_id, site_id),
FOREIGN KEY (collection_id, site_id) REFERENCES collections(id, site_id) ON DELETE CASCADE,
FOREIGN KEY (template_id) REFERENCES collection_templates(template_id) ON DELETE RESTRICT
)
`
func (q *Queries) InitializeCollectionItemsTable(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, initializeCollectionItemsTable)
return err
}
const initializeCollectionTemplatesTable = `-- name: InitializeCollectionTemplatesTable :exec
CREATE TABLE IF NOT EXISTS collection_templates (
template_id SERIAL PRIMARY KEY,
collection_id TEXT NOT NULL,
site_id TEXT NOT NULL,
name TEXT NOT NULL,
html_template TEXT NOT NULL,
is_default BOOLEAN DEFAULT FALSE NOT NULL,
created_at BIGINT DEFAULT (EXTRACT(EPOCH FROM NOW())) NOT NULL,
FOREIGN KEY (collection_id, site_id) REFERENCES collections(id, site_id) ON DELETE CASCADE
)
`
func (q *Queries) InitializeCollectionTemplatesTable(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, initializeCollectionTemplatesTable)
return err
}
const initializeCollectionsTable = `-- name: InitializeCollectionsTable :exec
CREATE TABLE IF NOT EXISTS collections (
id TEXT NOT NULL,
site_id TEXT NOT NULL,
container_html TEXT NOT NULL,
created_at BIGINT DEFAULT (EXTRACT(EPOCH FROM NOW())) NOT NULL,
updated_at BIGINT DEFAULT (EXTRACT(EPOCH FROM NOW())) NOT NULL,
last_edited_by TEXT DEFAULT 'system' NOT NULL,
PRIMARY KEY (id, site_id)
)
`
func (q *Queries) InitializeCollectionsTable(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, initializeCollectionsTable)
return err
}
const initializeSchema = `-- name: InitializeSchema :exec
CREATE TABLE IF NOT EXISTS content (
id TEXT NOT NULL,