diff --git a/docs/operations.md b/docs/operations.md index 5cf5d46..43cec68 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -19,7 +19,7 @@ ## Data -State lives in the directory given by `byre serve --data`, by default the `byre` directory under Go's `os.UserConfigDir()` (on macOS, `~/Library/Application Support/byre`): `byre.db`, `worktrees//`, `worktrees//-media` (the task's media folder, see "Screenshots from implementers"), `leads/` (each lead agent's working directory, the login path-escaped), `hands//byre-hands` (each running implementer session's copy of `byre-hands`, see "Screen and app control"), `blobs/` (uploaded images; the `blobs` table maps each id to its content type and its path relative to this directory). Task branches are `byre/` in the project's repo. +State lives in the directory given by `byre serve --data`, by default the `byre` directory under Go's `os.UserConfigDir()` (on macOS, `~/Library/Application Support/byre`): `byre.db`, `worktrees//`, `worktrees//-media` (the task's media folder, see "Screenshots from implementers"), `leads/` (each lead agent's working directory, the login path-escaped), `hands//byre-hands` (each running implementer session's copy of `byre-hands`, see "Screen and app control"), `blobs/` (uploaded images; the `blobs` table stores image metadata; `Store.blobPath` derives the file path from the id, with no separate path column). Task branches are `byre/` in the project's repo. Schema changes are the files in `internal/store/migrations/`, embedded in the binary, each named `NNN-name.sql`: a three-digit number, then a short kebab-case name saying what it does. A data migration SQL can't express is a Go function in `goMigrations` (`internal/store/store.go`), keyed `NNN-name` the same way and ordered among the files; `071-workflow-steps` (`internal/store/migrateworkflows.go`) is one. On open, byre applies, in file name order, every file whose name (without the number) isn't in the `migrations` table, each in its own transaction that also records the name. The number only orders: a migration that needs another's tables or columns takes a higher number, and migrations with the same number must not depend on each other. Each runs with foreign keys off, so a migration can rebuild a table other tables reference, and `PRAGMA foreign_key_check` must come back empty before it commits. A migration's SQL is fixed once applied anywhere: write it out in full rather than sharing Go constants that later change. byre is deployed from task branches that each add their own migrations, so after a merge a migration can land before others the database has already applied, and it still runs. Give a new migration a name no other branch will use, and make any data migration safe to run twice. `sqlite3 byre.db 'SELECT * FROM migrations'` lists what has been applied. diff --git a/internal/store/blob.go b/internal/store/blob.go index 262ff49..e4d2658 100644 --- a/internal/store/blob.go +++ b/internal/store/blob.go @@ -27,28 +27,27 @@ func (s *Store) CreateBlob(ctx context.Context, contentType string, data []byte, CreatedBy: actor, CreatedAt: time.Now().UTC(), } - rel := filepath.Join("blobs", b.Id) + path := s.blobPath(b.Id) - err := os.MkdirAll(filepath.Join(s.dataDir(), "blobs"), 0o755) + err := os.MkdirAll(filepath.Dir(path), 0o755) if err != nil { return b, err } - err = os.WriteFile(filepath.Join(s.dataDir(), rel), data, 0o644) + err = os.WriteFile(path, data, 0o644) if err != nil { return b, err } - _, err = s.db.ExecContext(ctx, `INSERT INTO blobs (id, content_type, path, created_by, created_at) VALUES (?, ?, ?, ?, ?)`, - b.Id, b.ContentType, rel, b.CreatedBy, formatTime(b.CreatedAt)) + _, err = s.db.ExecContext(ctx, `INSERT INTO blobs (id, content_type, created_by, created_at) VALUES (?, ?, ?, ?)`, + b.Id, b.ContentType, b.CreatedBy, formatTime(b.CreatedAt)) return b, err } func (s *Store) GetBlob(ctx context.Context, id string) (api.Blob, string, error) { b := api.Blob{} - rel := "" createdAt := "" - err := s.db.QueryRowContext(ctx, `SELECT id, content_type, path, created_by, created_at FROM blobs WHERE id = ?`, id). - Scan(&b.Id, &b.ContentType, &rel, &b.CreatedBy, &createdAt) + err := s.db.QueryRowContext(ctx, `SELECT id, content_type, created_by, created_at FROM blobs WHERE id = ?`, id). + Scan(&b.Id, &b.ContentType, &b.CreatedBy, &createdAt) if errors.Is(err, sql.ErrNoRows) { return b, "", errors.Join(ErrNotFound, errors.New("no blob "+id)) } @@ -56,7 +55,7 @@ func (s *Store) GetBlob(ctx context.Context, id string) (api.Blob, string, error return b, "", err } b.CreatedAt, err = parseTime(createdAt) - return b, filepath.Join(s.dataDir(), rel), err + return b, s.blobPath(b.Id), err } func (s *Store) ReadBlob(ctx context.Context, id string) (api.Blob, []byte, error) { @@ -68,6 +67,6 @@ func (s *Store) ReadBlob(ctx context.Context, id string) (api.Blob, []byte, erro return b, data, err } -func (s *Store) dataDir() string { - return filepath.Dir(s.path) +func (s *Store) blobPath(id string) string { + return filepath.Join(filepath.Dir(s.path), "blobs", id) } diff --git a/internal/store/migrations/081-drop-derived-blob-path.sql b/internal/store/migrations/081-drop-derived-blob-path.sql new file mode 100644 index 0000000..1bec386 --- /dev/null +++ b/internal/store/migrations/081-drop-derived-blob-path.sql @@ -0,0 +1 @@ +ALTER TABLE blobs DROP COLUMN path;