d516462750
The Proxies page consumer (and the secondary callers in internal/api/health.go and internal/api/settings.go) now read from the normalized containers index instead of the instances table. Stage ID is recovered through a (project_id, role=stage_name) join — uniquely-indexed via the existing UNIQUE(project_id, name) constraint on stages. Source field stays "instance" for back-compat with the Proxies page filter (the frontend keys off the literal string). Three new tests pin the join shape, verify the npm_proxy_id-only WHERE branch survives, and check that an orphan-role row falls out of the join cleanly (catches a regression to LEFT JOIN).
252 lines
8.0 KiB
Go
252 lines
8.0 KiB
Go
package store
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// instanceColumns is the canonical column list for instance queries.
|
|
const instanceColumns = `id, stage_id, project_id, container_id, image_tag, subdomain, npm_proxy_id, proxy_route_id, status, port, last_alive_at, created_at, updated_at`
|
|
|
|
// scanInstance scans a row into an Instance struct using the canonical column order.
|
|
func scanInstance(scanner interface{ Scan(...any) error }) (Instance, error) {
|
|
var inst Instance
|
|
err := scanner.Scan(
|
|
&inst.ID, &inst.StageID, &inst.ProjectID, &inst.ContainerID, &inst.ImageTag,
|
|
&inst.Subdomain, &inst.NpmProxyID, &inst.ProxyRouteID, &inst.Status, &inst.Port,
|
|
&inst.LastAliveAt, &inst.CreatedAt, &inst.UpdatedAt,
|
|
)
|
|
return inst, err
|
|
}
|
|
|
|
// CreateInstance inserts a new instance record.
|
|
func (s *Store) CreateInstance(inst Instance) (Instance, error) {
|
|
inst.ID = uuid.New().String()
|
|
inst.CreatedAt = Now()
|
|
inst.UpdatedAt = inst.CreatedAt
|
|
|
|
_, err := s.db.Exec(
|
|
`INSERT INTO instances (`+instanceColumns+`)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
inst.ID, inst.StageID, inst.ProjectID, inst.ContainerID, inst.ImageTag,
|
|
inst.Subdomain, inst.NpmProxyID, inst.ProxyRouteID, inst.Status, inst.Port,
|
|
inst.LastAliveAt, inst.CreatedAt, inst.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
return Instance{}, fmt.Errorf("insert instance: %w", err)
|
|
}
|
|
return inst, nil
|
|
}
|
|
|
|
// CreateInstanceWithID inserts a new instance using a pre-generated ID.
|
|
// Use this when the ID must be known before creation (e.g., for container labels).
|
|
func (s *Store) CreateInstanceWithID(inst Instance) (Instance, error) {
|
|
if inst.ID == "" {
|
|
return Instance{}, fmt.Errorf("instance ID is required")
|
|
}
|
|
inst.CreatedAt = Now()
|
|
inst.UpdatedAt = inst.CreatedAt
|
|
|
|
_, err := s.db.Exec(
|
|
`INSERT INTO instances (`+instanceColumns+`)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
inst.ID, inst.StageID, inst.ProjectID, inst.ContainerID, inst.ImageTag,
|
|
inst.Subdomain, inst.NpmProxyID, inst.ProxyRouteID, inst.Status, inst.Port,
|
|
inst.LastAliveAt, inst.CreatedAt, inst.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
return Instance{}, fmt.Errorf("insert instance: %w", err)
|
|
}
|
|
return inst, nil
|
|
}
|
|
|
|
// GetInstanceByID returns a single instance by its ID.
|
|
func (s *Store) GetInstanceByID(id string) (Instance, error) {
|
|
inst, err := scanInstance(s.db.QueryRow(
|
|
`SELECT `+instanceColumns+` FROM instances WHERE id = ?`, id,
|
|
))
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return Instance{}, fmt.Errorf("instance %s: %w", id, ErrNotFound)
|
|
}
|
|
if err != nil {
|
|
return Instance{}, fmt.Errorf("query instance: %w", err)
|
|
}
|
|
return inst, nil
|
|
}
|
|
|
|
// GetInstancesByStageID returns all instances for a given stage.
|
|
func (s *Store) GetInstancesByStageID(stageID string) ([]Instance, error) {
|
|
rows, err := s.db.Query(
|
|
`SELECT `+instanceColumns+` FROM instances WHERE stage_id = ? ORDER BY created_at DESC`, stageID,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query instances: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
instances := []Instance{}
|
|
for rows.Next() {
|
|
inst, err := scanInstance(rows)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("scan instance: %w", err)
|
|
}
|
|
instances = append(instances, inst)
|
|
}
|
|
return instances, rows.Err()
|
|
}
|
|
|
|
// ListAllInstances returns all instances across all stages.
|
|
func (s *Store) ListAllInstances() ([]Instance, error) {
|
|
rows, err := s.db.Query(
|
|
`SELECT ` + instanceColumns + ` FROM instances ORDER BY created_at DESC`,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query all instances: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
instances := []Instance{}
|
|
for rows.Next() {
|
|
inst, err := scanInstance(rows)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("scan instance: %w", err)
|
|
}
|
|
instances = append(instances, inst)
|
|
}
|
|
return instances, rows.Err()
|
|
}
|
|
|
|
// ProxyRoute represents a proxy-enabled resource (Docker instance or static site)
|
|
// joined with the human-readable names needed to render the Proxies page.
|
|
type ProxyRoute struct {
|
|
Source string `json:"source"` // "instance" or "static_site"
|
|
InstanceID string `json:"instance_id"`
|
|
ProjectID string `json:"project_id"`
|
|
ProjectName string `json:"project_name"`
|
|
StageID string `json:"stage_id"`
|
|
StageName string `json:"stage_name"`
|
|
ImageTag string `json:"image_tag"`
|
|
Subdomain string `json:"subdomain"`
|
|
Domain string `json:"domain"`
|
|
ContainerID string `json:"container_id"`
|
|
Port int `json:"port"`
|
|
ProxyRouteID string `json:"proxy_route_id"`
|
|
NpmProxyID int `json:"npm_proxy_id"`
|
|
Status string `json:"status"`
|
|
CreatedAt string `json:"created_at"`
|
|
}
|
|
|
|
// ListProxyRoutes returns proxy-enabled project containers joined with
|
|
// project + stage names. Reads from the normalized containers index — the
|
|
// instances table is no longer queried. Stage ID is resolved through a
|
|
// (project_id, role=stage_name) join, which is uniquely indexed.
|
|
//
|
|
// Source is reported as "instance" for back-compat with the Proxies page
|
|
// filter (which keys off the literal string).
|
|
func (s *Store) ListProxyRoutes(domain string) ([]ProxyRoute, error) {
|
|
rows, err := s.db.Query(`
|
|
SELECT c.id, p.id, p.name, s.id, s.name,
|
|
c.image_tag, c.subdomain, c.container_id, c.port,
|
|
c.proxy_route_id, c.npm_proxy_id, c.state, c.created_at
|
|
FROM containers c
|
|
JOIN workloads w ON w.id = c.workload_id AND w.kind = 'project'
|
|
JOIN projects p ON p.id = w.ref_id
|
|
JOIN stages s ON s.project_id = p.id AND s.name = c.role
|
|
WHERE c.subdomain != '' AND (c.proxy_route_id != '' OR c.npm_proxy_id > 0)
|
|
ORDER BY p.name, s.name, c.created_at DESC`,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query proxy routes: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
routes := []ProxyRoute{}
|
|
for rows.Next() {
|
|
var r ProxyRoute
|
|
if err := rows.Scan(
|
|
&r.InstanceID, &r.ProjectID, &r.ProjectName, &r.StageID, &r.StageName,
|
|
&r.ImageTag, &r.Subdomain, &r.ContainerID, &r.Port,
|
|
&r.ProxyRouteID, &r.NpmProxyID, &r.Status, &r.CreatedAt,
|
|
); err != nil {
|
|
return nil, fmt.Errorf("scan proxy route: %w", err)
|
|
}
|
|
r.Source = "instance"
|
|
if domain != "" && r.Subdomain != "" {
|
|
r.Domain = r.Subdomain + "." + domain
|
|
}
|
|
routes = append(routes, r)
|
|
}
|
|
return routes, rows.Err()
|
|
}
|
|
|
|
// UpdateInstance updates an existing instance's mutable fields.
|
|
func (s *Store) UpdateInstance(inst Instance) error {
|
|
inst.UpdatedAt = Now()
|
|
result, err := s.db.Exec(
|
|
`UPDATE instances SET stage_id=?, project_id=?, container_id=?, image_tag=?, subdomain=?, npm_proxy_id=?, proxy_route_id=?, status=?, port=?, last_alive_at=?, updated_at=?
|
|
WHERE id=?`,
|
|
inst.StageID, inst.ProjectID, inst.ContainerID, inst.ImageTag,
|
|
inst.Subdomain, inst.NpmProxyID, inst.ProxyRouteID, inst.Status, inst.Port,
|
|
inst.LastAliveAt, inst.UpdatedAt, inst.ID,
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("update instance: %w", err)
|
|
}
|
|
n, _ := result.RowsAffected()
|
|
if n == 0 {
|
|
return fmt.Errorf("instance %s: %w", inst.ID, ErrNotFound)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// UpdateInstanceStatus sets only the status field on an instance.
|
|
func (s *Store) UpdateInstanceStatus(id string, status string) error {
|
|
ts := Now()
|
|
result, err := s.db.Exec(
|
|
`UPDATE instances SET status=?, updated_at=? WHERE id=?`,
|
|
status, ts, id,
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("update instance status: %w", err)
|
|
}
|
|
n, _ := result.RowsAffected()
|
|
if n == 0 {
|
|
return fmt.Errorf("instance %s: %w", id, ErrNotFound)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// UpdateLastAliveAt sets the last_alive_at timestamp for an instance.
|
|
// Called when an instance is seen running.
|
|
func (s *Store) UpdateLastAliveAt(id string) error {
|
|
ts := Now()
|
|
result, err := s.db.Exec(
|
|
`UPDATE instances SET last_alive_at=?, updated_at=? WHERE id=?`,
|
|
ts, ts, id,
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("update last_alive_at: %w", err)
|
|
}
|
|
n, _ := result.RowsAffected()
|
|
if n == 0 {
|
|
return fmt.Errorf("instance %s: %w", id, ErrNotFound)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteInstance removes an instance by ID.
|
|
func (s *Store) DeleteInstance(id string) error {
|
|
result, err := s.db.Exec(`DELETE FROM instances WHERE id = ?`, id)
|
|
if err != nil {
|
|
return fmt.Errorf("delete instance: %w", err)
|
|
}
|
|
n, _ := result.RowsAffected()
|
|
if n == 0 {
|
|
return fmt.Errorf("instance %s: %w", id, ErrNotFound)
|
|
}
|
|
return nil
|
|
}
|