Files
tiny-forge/internal/store/apps.go
T
alexei.dolgolyov cba2149aa9 refactor(workload): finalize containers index + post-review hardening
Wraps up the workload refactor with the fixes that came out of the multi-agent
code review (see docs/plans/workload-refactor.md "What actually shipped").

Backend:
- store.ReconcileContainer: separate write path so the 30s reconciler tick no
  longer overwrites deployer-owned fields (subdomain, proxy_route_id,
  npm_proxy_id, image_tag).
- Container.stage_id column + index; ListProxyRoutes / ListContainersByStageID
  join via stage_id (survives stage rename), with legacy fallback to
  (project_id, role=stage_name).
- Reconciler: workload-existence check (rejects forged tinyforge.workload.id
  labels), skips inventing project-kind rows, child-context cancel before
  wg.Wait() on shutdown.
- Transactional CRUD across projects / stacks / static_sites: parent UPDATE
  and workload sync land in one transaction so secret rotations are durable.
- Webhook routing reads exclusively through workloads.webhook_secret; legacy
  GetProjectByWebhookSecret / GetStaticSiteByWebhookSecret fallback removed.
- store.GetStackByComposeProjectName + indexed lookup (no more full-table
  stack scan per compose container per tick).
- store.ListMissingSweepRows: filtered query for the missing-sweep.
- /api/instances/* handlers verify (workload_id, role) match URL
  (project_id, stage_name) before mutating — closes the cross-project
  hijack the security review flagged.
- extra_json no longer referenced from Go (column kept on disk for now).

Frontend:
- WorkloadContainers.svelte: generic detail-page panel reusable by stack and
  site detail pages.
- Containers page polish: client-side kind/state filters over an unfiltered
  fetch, URL-synced filters, race-safe loads via sequence number, EN+RU i18n,
  sidebar counter via navCounts.containers.

Misc:
- scripts/dev-server.sh: tolerate empty netstat grep result.
- .gitignore: ignore docker-watcher binaries, .claude/worktrees/, .facts-sync.json.
2026-05-09 15:44:41 +03:00

117 lines
2.8 KiB
Go

package store
import (
"database/sql"
"errors"
"fmt"
"github.com/google/uuid"
)
const appColumns = `id, name, description, created_at, updated_at`
func scanApp(scanner interface{ Scan(...any) error }) (App, error) {
var a App
err := scanner.Scan(&a.ID, &a.Name, &a.Description, &a.CreatedAt, &a.UpdatedAt)
return a, err
}
// CreateApp inserts a new app row. Names must be unique.
func (s *Store) CreateApp(a App) (App, error) {
if a.ID == "" {
a.ID = uuid.New().String()
}
a.CreatedAt = Now()
a.UpdatedAt = a.CreatedAt
_, err := s.db.Exec(
`INSERT INTO apps (`+appColumns+`) VALUES (?, ?, ?, ?, ?)`,
a.ID, a.Name, a.Description, a.CreatedAt, a.UpdatedAt,
)
if err != nil {
return App{}, fmt.Errorf("insert app: %w", err)
}
return a, nil
}
// GetAppByID returns an app by ID.
func (s *Store) GetAppByID(id string) (App, error) {
a, err := scanApp(s.db.QueryRow(
`SELECT `+appColumns+` FROM apps WHERE id = ?`, id,
))
if errors.Is(err, sql.ErrNoRows) {
return App{}, fmt.Errorf("app %s: %w", id, ErrNotFound)
}
if err != nil {
return App{}, fmt.Errorf("query app: %w", err)
}
return a, nil
}
// ListApps returns all apps, ordered by name.
func (s *Store) ListApps() ([]App, error) {
rows, err := s.db.Query(
`SELECT ` + appColumns + ` FROM apps ORDER BY name`,
)
if err != nil {
return nil, fmt.Errorf("query apps: %w", err)
}
defer rows.Close()
out := []App{}
for rows.Next() {
a, err := scanApp(rows)
if err != nil {
return nil, fmt.Errorf("scan app: %w", err)
}
out = append(out, a)
}
return out, rows.Err()
}
// UpdateApp updates the mutable fields (name, description) of an app row.
func (s *Store) UpdateApp(a App) error {
a.UpdatedAt = Now()
result, err := s.db.Exec(
`UPDATE apps SET name=?, description=?, updated_at=? WHERE id=?`,
a.Name, a.Description, a.UpdatedAt, a.ID,
)
if err != nil {
return fmt.Errorf("update app: %w", err)
}
n, err := result.RowsAffected()
if err != nil {
return fmt.Errorf("rows affected: %w", err)
}
if n == 0 {
return fmt.Errorf("app %s: %w", a.ID, ErrNotFound)
}
return nil
}
// DeleteApp removes an app and clears its app_id from any workloads.
// Workloads survive — they just become unassigned.
func (s *Store) DeleteApp(id string) error {
tx, err := s.db.Begin()
if err != nil {
return fmt.Errorf("begin delete app: %w", err)
}
defer tx.Rollback()
if _, err := tx.Exec(`UPDATE workloads SET app_id = '' WHERE app_id = ?`, id); err != nil {
return fmt.Errorf("clear app_id on workloads: %w", err)
}
result, err := tx.Exec(`DELETE FROM apps WHERE id = ?`, id)
if err != nil {
return fmt.Errorf("delete app: %w", err)
}
n, err := result.RowsAffected()
if err != nil {
return fmt.Errorf("rows affected: %w", err)
}
if n == 0 {
return fmt.Errorf("app %s: %w", id, ErrNotFound)
}
return tx.Commit()
}