Files
archie/internal/infrastructure/postgres/package_contributions.go
T
sam 8c745481d2 feat(store): project installed package contributions into org resources
An install that stayed in installed_packages left every contribution
unprojected: nothing made a package's workflow a usable org resource, and
nothing stopped it on removal. This lands projection for the workflow family,
the one kind whose org resource, validation and live read path all exist:

- storepkg.Service gains Projections (family -> projector): install resolves
  each projected family's contributed names against the fetched
  layer (FilesFromLayer), applies them, and on failure withdraws and removes
  the half installation; remove withdraws before the record goes, so a failed
  withdrawal keeps the package installed and usable.
- controlplane.WorkflowProjector merges the package's entries into the org's
  workflow-definitions document — the same document the daemon's
  WorkflowDefinitions client reads per dispatch, so a projected workflow is
  usable at the next dispatch and a withdrawn one stops dispatching, without
  restarts. A contributed id displacing an operator's own workflow is refused
  (storepkg.ErrContributionCollision), never silently replaced.
- installed_package_contributions (migration 0023) records the entries a
  package projected, so removal takes exactly its own contributions out.
- stateStoreDeps wires the projector set on the State Store's pool; the
  composition is pinned by the live round-trip test over a real gRPC
  connection.

Prompts, skills, MCP server definitions and default settings remain
unprojected: their contribution formats are not settled by docs/prds/store.md
and are left for their own slices rather than half-wired here.
2026-09-30 08:05:22 +10:00

65 lines
2.3 KiB
Go

package postgres
import (
"context"
"errors"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/samcharles93/archie-core/internal/domain/storepkg"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
)
// PackageContributions records the control-plane resource entries an org's
// installed package projected. Removal consults this ledger to take exactly
// the package's own contributions back out, so a remove that fails midway
// keeps the ledger for the next attempt and another package's entries stay.
type PackageContributions struct{ pool *pgxpool.Pool }
var _ storepkg.ProjectionLedger = (*PackageContributions)(nil)
func NewPackageContributions(pool *pgxpool.Pool) *PackageContributions {
return &PackageContributions{pool: pool}
}
func (s *PackageContributions) Record(ctx context.Context, orgID, name string, entries []storepkg.ProjectionEntry) error {
if orgID == "" || name == "" {
return errors.New("org and package name are required")
}
tx, err := s.pool.Begin(ctx)
if err != nil {
return err
}
defer func() { _ = tx.Rollback(ctx) }()
queries := postgresdb.New(tx)
if err := queries.LockInstalledPackageContributions(ctx, postgresdb.LockInstalledPackageContributionsParams{Column1: orgID, Column2: name}); err != nil {
return err
}
for _, entry := range entries {
if err := queries.InsertInstalledPackageContribution(ctx, postgresdb.InsertInstalledPackageContributionParams{
OrgID: orgID, PackageName: name, Family: entry.Family, EntryID: entry.EntryID,
}); err != nil {
return err
}
}
return tx.Commit(ctx)
}
func (s *PackageContributions) Entries(ctx context.Context, orgID, name string) ([]storepkg.ProjectionEntry, error) {
rows, err := postgresdb.New(s.pool).ListInstalledPackageContributions(ctx, postgresdb.ListInstalledPackageContributionsParams{OrgID: orgID, PackageName: name})
if err != nil {
return nil, err
}
entries := make([]storepkg.ProjectionEntry, 0, len(rows))
for _, row := range rows {
entries = append(entries, storepkg.ProjectionEntry{Family: row.Family, EntryID: row.EntryID})
}
return entries, nil
}
func (s *PackageContributions) Forget(ctx context.Context, orgID, name, family string) error {
return postgresdb.New(s.pool).DeleteInstalledPackageContributions(ctx, postgresdb.DeleteInstalledPackageContributionsParams{
OrgID: orgID, PackageName: name, Family: family,
})
}