d63c831d15
Initialize Go module, directory structure, and full SQLite store layer: - 7-table schema (projects, stages, registries, settings, instances, deploys, deploy_logs) with auto-migration - CRUD operations for all entities with proper error handling - ErrNotFound sentinel for distinguishing 404 from 500 in handlers - WAL mode, foreign keys, busy timeout pragmas
96 lines
2.5 KiB
Go
96 lines
2.5 KiB
Go
package store
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// CreateRegistry inserts a new registry.
|
|
func (s *Store) CreateRegistry(r Registry) (Registry, error) {
|
|
r.ID = uuid.New().String()
|
|
r.CreatedAt = now()
|
|
r.UpdatedAt = r.CreatedAt
|
|
|
|
_, err := s.db.Exec(
|
|
`INSERT INTO registries (id, name, url, type, token, created_at, updated_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
|
r.ID, r.Name, r.URL, r.Type, r.Token, r.CreatedAt, r.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
return Registry{}, fmt.Errorf("insert registry: %w", err)
|
|
}
|
|
return r, nil
|
|
}
|
|
|
|
// GetRegistryByID returns a single registry by its ID.
|
|
func (s *Store) GetRegistryByID(id string) (Registry, error) {
|
|
var r Registry
|
|
err := s.db.QueryRow(
|
|
`SELECT id, name, url, type, token, created_at, updated_at
|
|
FROM registries WHERE id = ?`, id,
|
|
).Scan(&r.ID, &r.Name, &r.URL, &r.Type, &r.Token, &r.CreatedAt, &r.UpdatedAt)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return Registry{}, fmt.Errorf("registry %s: %w", id, ErrNotFound)
|
|
}
|
|
if err != nil {
|
|
return Registry{}, fmt.Errorf("query registry: %w", err)
|
|
}
|
|
return r, nil
|
|
}
|
|
|
|
// GetAllRegistries returns every registry ordered by name.
|
|
func (s *Store) GetAllRegistries() ([]Registry, error) {
|
|
rows, err := s.db.Query(
|
|
`SELECT id, name, url, type, token, created_at, updated_at
|
|
FROM registries ORDER BY name`,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query registries: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var registries []Registry
|
|
for rows.Next() {
|
|
var r Registry
|
|
if err := rows.Scan(&r.ID, &r.Name, &r.URL, &r.Type, &r.Token, &r.CreatedAt, &r.UpdatedAt); err != nil {
|
|
return nil, fmt.Errorf("scan registry: %w", err)
|
|
}
|
|
registries = append(registries, r)
|
|
}
|
|
return registries, rows.Err()
|
|
}
|
|
|
|
// UpdateRegistry updates an existing registry's mutable fields.
|
|
func (s *Store) UpdateRegistry(r Registry) error {
|
|
r.UpdatedAt = now()
|
|
result, err := s.db.Exec(
|
|
`UPDATE registries SET name=?, url=?, type=?, token=?, updated_at=?
|
|
WHERE id=?`,
|
|
r.Name, r.URL, r.Type, r.Token, r.UpdatedAt, r.ID,
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("update registry: %w", err)
|
|
}
|
|
n, _ := result.RowsAffected()
|
|
if n == 0 {
|
|
return fmt.Errorf("registry %s: %w", r.ID, ErrNotFound)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteRegistry removes a registry by ID.
|
|
func (s *Store) DeleteRegistry(id string) error {
|
|
result, err := s.db.Exec(`DELETE FROM registries WHERE id = ?`, id)
|
|
if err != nil {
|
|
return fmt.Errorf("delete registry: %w", err)
|
|
}
|
|
n, _ := result.RowsAffected()
|
|
if n == 0 {
|
|
return fmt.Errorf("registry %s: %w", id, ErrNotFound)
|
|
}
|
|
return nil
|
|
}
|