Files
pad/internal/store/workspaces.go
T
xarmian 7cda0d7896 feat: rebrand to Perpetual Software + new tagline (IDEA-832) (#273)
Migrates from xarmian/pad to PerpetualSoftware/pad across the entire
repo and updates the product subtitle to "Collaborate with your AI
agents".

Go module rename
- go.mod: github.com/xarmian/pad → github.com/PerpetualSoftware/pad
- All Go imports updated across cmd/pad, internal/{cli,server,store,
  models,collections,items,events,metrics,webhooks} (~130 files)
- Test fixtures with the literal repo slug ("xarmian/pad" in JSON
  shapes, SSH/HTTPS git URL strings, workspace_context fixtures)
  also updated, including the secondary repo entry
  (xarmian/pad-web → PerpetualSoftware/pad-web — pad-web was also
  moved to the org per branch context)

Docs / config
- README badges, install instructions, brew tap, Docker image, source
  build path, sponsor link (sponsor link kept as personal @xarmian)
- Subtitle: "Project management for developers and AI agents." →
  "Collaborate with your AI agents." (README, manifests, web layout
  meta, .goreleaser homebrew description)
- CONTRIBUTING.md, SECURITY.md, skills/INSTALL.md
- .goreleaser.yaml: homebrew_casks owner, GHCR image, release github
  owner, cosign cert-identity regex, comments
- .github/workflows/release.yml: tap/release comments
- deploy/k8s/deployment.yaml: container image
- docs/deployment.md: clone URL
- web/static/{site.webmanifest,manifest.json}: description
- web/src/routes/+layout.svelte: meta description + og:description

Brew tap path is PerpetualSoftware/tap/pad (CamelCase, matches
GitHub user case). GHCR image is ghcr.io/perpetualsoftware/pad
(lowercased per GHCR's URL normalization). CODEOWNERS @xarmian and
FUNDING.yml github: xarmian intentionally retained — those are the
personal maintainer / sponsor account, separate from the org repo.

Verification: go build ./..., go test ./... (all pkgs pass), web
build, and make install all clean (TASK-844, TASK-845).
2026-04-28 12:26:39 -04:00

262 lines
7.7 KiB
Go

package store
import (
"database/sql"
"fmt"
"github.com/PerpetualSoftware/pad/internal/models"
)
func (s *Store) ListWorkspaces() ([]models.Workspace, error) {
return s.ListWorkspacesForUser("")
}
// ListWorkspacesForUser returns all workspaces (admin view), with per-user
// sort_order from workspace_members when userID is provided.
func (s *Store) ListWorkspacesForUser(userID string) ([]models.Workspace, error) {
var rows *sql.Rows
var err error
if userID != "" {
rows, err = s.db.Query(s.q(`
SELECT w.id, w.name, w.slug, w.owner_id, COALESCE(ou.username, ''), w.description, w.settings, w.created_at, w.updated_at,
COALESCE(wm.sort_order, 0)
FROM workspaces w
LEFT JOIN workspace_members wm ON wm.workspace_id = w.id AND wm.user_id = ?
LEFT JOIN users ou ON ou.id = w.owner_id
WHERE w.deleted_at IS NULL
ORDER BY COALESCE(wm.sort_order, 0) ASC, w.name ASC
`), userID)
} else {
rows, err = s.db.Query(s.q(`
SELECT w.id, w.name, w.slug, w.owner_id, COALESCE(ou.username, ''), w.description, w.settings, w.created_at, w.updated_at,
0 as sort_order
FROM workspaces w
LEFT JOIN users ou ON ou.id = w.owner_id
WHERE w.deleted_at IS NULL
ORDER BY w.name ASC
`))
}
if err != nil {
return nil, err
}
defer rows.Close()
var workspaces []models.Workspace
for rows.Next() {
var w models.Workspace
var createdAt, updatedAt string
if err := rows.Scan(&w.ID, &w.Name, &w.Slug, &w.OwnerID, &w.OwnerUsername, &w.Description, &w.Settings, &createdAt, &updatedAt, &w.SortOrder); err != nil {
return nil, err
}
w.CreatedAt = parseTime(createdAt)
w.UpdatedAt = parseTime(updatedAt)
w.HydrateDerivedFields()
workspaces = append(workspaces, w)
}
return workspaces, rows.Err()
}
func (s *Store) CreateWorkspace(input models.WorkspaceCreate) (*models.Workspace, error) {
id := newID()
ts := now()
slug := input.Slug
if slug == "" {
slug = slugify(input.Name)
}
// Workspace slugs are globally unique (not scoped to a workspace
// like collection/item slugs), so we use a workspace-specific
// uniqueness check rather than the generic uniqueSlug helper.
finalSlug, err := s.uniqueWorkspaceSlug(slug)
if err != nil {
return nil, err
}
settings := input.Settings
if settings == "" {
settings = "{}"
}
settings, err = models.NormalizeWorkspaceSettings(settings)
if err != nil {
return nil, fmt.Errorf("normalize workspace settings: %w", err)
}
if input.Context != nil {
settings, err = models.ApplyWorkspaceContext(settings, input.Context)
if err != nil {
return nil, fmt.Errorf("apply workspace context: %w", err)
}
}
_, err = s.db.Exec(s.q(`
INSERT INTO workspaces (id, name, slug, owner_id, description, settings, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
`), id, input.Name, finalSlug, input.OwnerID, input.Description, settings, ts, ts)
if err != nil {
return nil, fmt.Errorf("insert workspace: %w", err)
}
return s.GetWorkspaceBySlug(finalSlug)
}
func (s *Store) uniqueWorkspaceSlug(baseSlug string) (string, error) {
slug := baseSlug
for i := 2; ; i++ {
var count int
err := s.db.QueryRow(s.q("SELECT COUNT(*) FROM workspaces WHERE slug = ? AND deleted_at IS NULL"), slug).Scan(&count)
if err != nil {
return "", err
}
if count == 0 {
return slug, nil
}
slug = fmt.Sprintf("%s-%d", baseSlug, i)
}
}
func (s *Store) GetWorkspaceBySlug(slug string) (*models.Workspace, error) {
var w models.Workspace
var createdAt, updatedAt string
var deletedAt *string
err := s.db.QueryRow(s.q(`
SELECT w.id, w.name, w.slug, w.owner_id, COALESCE(ou.username, ''), w.description, w.settings, w.created_at, w.updated_at, w.deleted_at
FROM workspaces w
LEFT JOIN users ou ON ou.id = w.owner_id
WHERE w.slug = ? AND w.deleted_at IS NULL
`), slug).Scan(&w.ID, &w.Name, &w.Slug, &w.OwnerID, &w.OwnerUsername, &w.Description, &w.Settings, &createdAt, &updatedAt, &deletedAt)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
w.CreatedAt = parseTime(createdAt)
w.UpdatedAt = parseTime(updatedAt)
w.DeletedAt = parseTimePtr(deletedAt)
w.HydrateDerivedFields()
return &w, nil
}
func (s *Store) GetWorkspaceByID(id string) (*models.Workspace, error) {
var w models.Workspace
var createdAt, updatedAt string
var deletedAt *string
err := s.db.QueryRow(s.q(`
SELECT w.id, w.name, w.slug, w.owner_id, COALESCE(ou.username, ''), w.description, w.settings, w.created_at, w.updated_at, w.deleted_at
FROM workspaces w
LEFT JOIN users ou ON ou.id = w.owner_id
WHERE w.id = ? AND w.deleted_at IS NULL
`), id).Scan(&w.ID, &w.Name, &w.Slug, &w.OwnerID, &w.OwnerUsername, &w.Description, &w.Settings, &createdAt, &updatedAt, &deletedAt)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
w.CreatedAt = parseTime(createdAt)
w.UpdatedAt = parseTime(updatedAt)
w.DeletedAt = parseTimePtr(deletedAt)
w.HydrateDerivedFields()
return &w, nil
}
// GetWorkspacesBySlugForUser finds workspaces matching a slug that are accessible
// to the given user (owned, member, or guest with grants).
func (s *Store) GetWorkspacesBySlugForUser(slug, userID string) ([]models.Workspace, error) {
rows, err := s.db.Query(s.q(`
SELECT DISTINCT w.id, w.name, w.slug, w.owner_id, COALESCE(ou.username, ''), w.description, w.settings, w.created_at, w.updated_at
FROM workspaces w
LEFT JOIN workspace_members wm ON wm.workspace_id = w.id AND wm.user_id = ?
LEFT JOIN collection_grants cg ON cg.workspace_id = w.id AND cg.user_id = ?
LEFT JOIN item_grants ig ON ig.workspace_id = w.id AND ig.user_id = ?
LEFT JOIN users ou ON ou.id = w.owner_id
WHERE w.slug = ? AND w.deleted_at IS NULL
AND (w.owner_id = ? OR wm.user_id IS NOT NULL OR cg.user_id IS NOT NULL OR ig.user_id IS NOT NULL)
`), userID, userID, userID, slug, userID)
if err != nil {
return nil, fmt.Errorf("get workspaces by slug for user: %w", err)
}
defer rows.Close()
var result []models.Workspace
for rows.Next() {
var w models.Workspace
var createdAt, updatedAt string
if err := rows.Scan(&w.ID, &w.Name, &w.Slug, &w.OwnerID, &w.OwnerUsername, &w.Description, &w.Settings, &createdAt, &updatedAt); err != nil {
return nil, err
}
w.CreatedAt = parseTime(createdAt)
w.UpdatedAt = parseTime(updatedAt)
w.HydrateDerivedFields()
result = append(result, w)
}
return result, rows.Err()
}
func (s *Store) UpdateWorkspace(slug string, input models.WorkspaceUpdate) (*models.Workspace, error) {
w, err := s.GetWorkspaceBySlug(slug)
if err != nil {
return nil, err
}
if w == nil {
return nil, nil
}
ts := now()
if input.Name != nil {
w.Name = *input.Name
}
if input.Description != nil {
w.Description = *input.Description
}
if input.Settings != nil {
w.Settings = *input.Settings
}
if input.Context != nil {
settings, err := models.ApplyWorkspaceContext(w.Settings, input.Context)
if err != nil {
return nil, fmt.Errorf("apply workspace context: %w", err)
}
w.Settings = settings
}
if w.Settings != "" {
settings, err := models.NormalizeWorkspaceSettings(w.Settings)
if err != nil {
return nil, fmt.Errorf("normalize workspace settings: %w", err)
}
w.Settings = settings
}
_, err = s.db.Exec(s.q(`
UPDATE workspaces SET name = ?, description = ?, settings = ?, updated_at = ?
WHERE id = ?
`), w.Name, w.Description, w.Settings, ts, w.ID)
if err != nil {
return nil, err
}
return s.GetWorkspaceBySlug(slug)
}
func (s *Store) DeleteWorkspace(slug string) error {
ts := now()
result, err := s.db.Exec(s.q(`
UPDATE workspaces SET deleted_at = ?, updated_at = ?
WHERE slug = ? AND deleted_at IS NULL
`), ts, ts, slug)
if err != nil {
return err
}
rows, _ := result.RowsAffected()
if rows == 0 {
return sql.ErrNoRows
}
return nil
}