Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions OPERATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ The app keeps its data in three places:

From a laptop, the same steps are `node ui/scripts/production.mjs preflight|deploy|smoke`, with the environment the workflow sets.

### Resetting a project store

The release with binary Git SHAs and decision batch UUIDs changes both D1 and the CLI's SQLite schema. To keep published batches and approvals, use a machine that still has their local store and the previous CLI.

1. Take the old Worker offline. Run the SQL below on each project D1 database, then deploy the new Worker. The reset discards batch history, decisions, and image and built Storybook metadata. The Worker recreates the tables on first use. The image and Storybook bytes in R2 can be reused.
2. Once the new Worker is serving, use the **previous CLI and its intact local store** to run `selfie push <batch-id>` for each finished, clean batch you want to republish. Use credentials with decision permission to include approvals and rejections. A push made before the D1 reset would be erased by step 1.
3. After those pushes succeed, stop local Selfie processes and run the same SQL on each affected `$SELFIE_HOME/<slug>/store.db` (default `~/.config/selfie/<slug>/store.db`). Then use the upgraded CLI. To restore published decisions reachable from HEAD, run `selfie pull`. Run `selfie review` to record a fresh local batch. If all old batch history is disposable, skip step 2.

```sql
DROP TABLE IF EXISTS decisions;
DROP TABLE IF EXISTS renders;
DROP TABLE IF EXISTS commit_parents;
DROP TABLE IF EXISTS render_batches;
DROP TABLE IF EXISTS commits;
DROP TABLE IF EXISTS images;
DROP TABLE IF EXISTS built_storybooks;
```

The table reset preserves local notes and `render_keys`. Their old batch IDs may refer to discarded batches. Deleting the whole `store.db` also deletes the notes and render keys.

## Logs

`selfie-app` keeps what it logs in Workers Logs (`[observability]` in `ui/wrangler.toml`), for 3 days on the Workers Free plan and 7 on Paid: in the dashboard, **Workers & Pages → selfie-app → Logs**. It doesn't keep a line for every request, only what the code logs, which is mostly errors.
Expand Down
2 changes: 1 addition & 1 deletion cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ async function withProject(context: Context, work: (project: Project) => Promise
} catch (error) {
// Nothing migrates a store yet: docs/specs/store.md -> Open questions.
if (/no such column|has no column named/.test((error as Error).message)) {
throw new SelfieError("old-store", `This project's store.db under ${selfieHome(context.env)} is from an older Selfie, and nothing migrates it yet, so delete it and render again. (${(error as Error).message})`);
throw new SelfieError("old-store", `This project's store.db under ${selfieHome(context.env)} is from an older Selfie. For the binary Git SHA upgrade, follow https://github.com/diffplug/selfie/blob/main/OPERATIONS.md#resetting-a-project-store to preserve local notes and republish batches. If all local data is disposable, delete it and render again. (${(error as Error).message})`);
}
throw error;
}
Expand Down
12 changes: 12 additions & 0 deletions cli/test/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,18 @@ describe("a store from before a schema change", () => {
expect(json.error).toMatchObject({ code: "old-store", message: expect.stringContaining("delete it") });
});

test("points a text-SHA store to the reset that preserves local notes", async () => {
h.stories = [{ id: "a--one" }];
await review();
const [slug] = await readdir(h.home);
const db = new DatabaseSync(path.join(h.home, slug!, "store.db"));
db.exec("DROP INDEX commits_by_sha; ALTER TABLE commits RENAME COLUMN sha_bytes TO sha");
db.close();
const { code, json } = await review();
expect(code).toBe(2);
expect(json.error).toMatchObject({ code: "old-store", message: expect.stringContaining("OPERATIONS.md#resetting-a-project-store") });
});

test("says so on opening one from before integer keys, which fails at making an index", async () => {
h.stories = [{ id: "a--one" }];
await review();
Expand Down
1 change: 1 addition & 0 deletions docs/specs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Source of truth: `cli/src/paths.ts`, `cli/src/project.ts`; tests in `cli/test/un
render/<uuid>/ One render's output, deleted when recording ends
```

- **Must report an incompatible store with a missing column as `old-store` and point to the reset instructions** (`OPERATIONS.md` → Resetting a project store).
- **Never read `<home>/d1.env`**, or anything else in `<home>` that isn't under a project's slug.
- **Must make a slug of only `[a-z0-9-]`**, so it holds no separator or dot, can't climb out of `<home>`, and can't be a file there such as `d1.env`. It is the readable origin, cut to 80 characters, then 8 hex digits of the normalized origin's SHA256, so `a/b-c` and `a-b/c` stay apart.
- **Must normalize an origin to its lowercased host and path, with no scheme, credentials, port, trailing slash, or `.git`**, so an SSH and an HTTPS clone share a store, and a token in a URL reaches no path, document, or log. The origin goes no further than the slug.
Expand Down
16 changes: 10 additions & 6 deletions docs/specs/renders.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ Source of truth: `RenderBatches.decide` in `store/src/core/render-batches.ts`.

A decision is an approval or a rejection, by an actor, at the time it is recorded.

- **Must record a decision against a render's name, an image hash or a removal, and the commit and batch it was made on.** A removal is a decision with no image, stored as an image key of 0, which [no image is ever given](store.md#image-keys).
- **Must record a decision against a render's name, an image hash or a removal, and the commit and batch it was made on.** A removal is a decision with no image, stored as an image key of 0, which [no image is ever given](store.md#image-keys). **Must store its originating batch UUID as 16 raw bytes and exchange it as a lowercase, hyphenated UUID** (rationale).
- **Must keep one decision per name, hash, and commit, the one most recently recorded.** There is no history of replaced decisions.
- **Must merge a decision from another store by its own time, latest first, then `rejected` before `approved`, and never by who made it**, so two stores reach one verdict whichever order they hear in, a person changing their mind still works, and sending a decision twice changes nothing. It keeps its commit and batch there, so history reaches it as it would here.
- **Must merge decisions for the same name, image hash or removal, and commit by decision time, with rejection winning an equal-time tie.** When the incoming time and verdict both match, the stored decision keeps its actor and batch. Replaying a decision changes nothing (rationale).
- **Must refuse every decision in a request if any names an image this store doesn't hold**, with `unknown-image`, as a render already must, so a decision can hold the image's key (rationale). Sync sends a batch's images before its decisions (`docs/specs/sync.md` → Pushing and pulling).

## Approvals
Expand Down Expand Up @@ -129,22 +129,24 @@ Source of truth: `CREATE_BATCH_TABLES` in `store/src/core/render-batches.ts`, `i
```sql
environments(key PK, id UNIQUE)
stories(key PK, id UNIQUE)
commits(key PK, sha UNIQUE)
commits(key PK, sha_bytes BLOB)
UNIQUE INDEX commits_by_sha(sha_bytes)
actors(key PK, name UNIQUE)
render_batches(key PK, id UNIQUE, commit_key → commits.key, environment_key → environments.key, started_at, finished_at, failed_count, dirty, story_filter, reused_from_batch, scale, viewport_width, viewport_height, branch, storybook_key → built_storybooks.key)
INDEX batches_by_commit(commit_key, environment_key, started_at)
INDEX batches_by_branch(branch, environment_key, started_at)
renders(batch_key → render_batches.key, story_key → stories.key, image_key → images.key, failure, viewport_width, viewport_height) PK (batch_key, story_key) WITHOUT ROWID
decisions(environment_key → environments.key, commit_key → commits.key, story_key → stories.key, image_key, rejected, decided_at, actor_key → actors.key, batch_id) PK (environment_key, commit_key, story_key, image_key) WITHOUT ROWID
decisions(environment_key → environments.key, commit_key → commits.key, story_key → stories.key, image_key, rejected, decided_at, actor_key → actors.key, batch_uuid BLOB) PK (environment_key, commit_key, story_key, image_key) WITHOUT ROWID
INDEX decisions_by_image(environment_key, story_key, image_key)
commit_parents(commit_key → commits.key, parent_key → commits.key) PK (commit_key, parent_key) WITHOUT ROWID
INDEX commit_parents_by_parent(parent_key, commit_key)
```

- **Must leave out the CLI's local table** (`docs/specs/cli.md` → Recording only if necessary): it's in a local `store.db`, not this schema, and D1 doesn't have it.
- **Must only add what is missing:** `initStoreSchema` is one request of `CREATE ... IF NOT EXISTS`, so running it upgrades a store holding only images. It changes no existing table, and there is no version number. Any other change needs `docs/specs/store.md` → Open questions decided first. `render_batches.reused_from_batch`, the rename of its tables and columns (`docs/specs/cli.rationale.md`), and its foreign keys are the exceptions, made before any project database existed (rationale): `CREATE TABLE IF NOT EXISTS` doesn't add it to an older store, whose reviews and accepts fail with exit 2 until the store is recreated.
- **Must only add missing tables and indexes during setup**, in one request. A store holding only images can be opened. A store with the old text `commits.sha` column must fail setup and be recreated (rationale).
- **Must store a Git commit SHA as its 20 SHA-1 bytes or 32 SHA-256 bytes, and exchange it as full lowercase hex.** (rationale)
- **Must declare a foreign key wherever a row's parent is always written first and held here**, as `→` shows above, so a store refuses an orphan instead of holding one (rationale).
- **Never declare one on `decisions.image_key`, `decisions.batch_id`, or `reused_from_batch`**, since a key of 0 is a removal, a decision may name a batch this store doesn't hold, and sync may move a batch without the one it was reused from.
- **Never declare one on `decisions.image_key`, `decisions.batch_uuid`, or `reused_from_batch`**, since a key of 0 is a removal, a decision may name a batch this store doesn't hold, and sync may move a batch without the one it was reused from.
- **Must say in `render_batches.viewport_width` and `viewport_height` the window a batch's stories lay out in, in CSS pixels, and in those columns of `renders` only a story's own, if it differs**, both or neither, so a story with neither laid out in its batch's (`docs/specs/render.md` → A story's viewport). They only size a story running live, so a comparison never reads them (rationale). **A batch reusing another's images must hold the same sizes.**
- **Must hold what repeats across a great many rows once, by an integer key, and never let a key leave the store**, as with image keys (rationale). A batch's `id`, a story's ID, a commit's SHA, an environment's ID, an actor's name, and an image's hash are what leave it. **Must give a name its key in the same request, before the first row that uses it.**
- **May list a batch's pages in any order**, and `next` only says where the next one starts. A comparison lists stories by name.
Expand All @@ -167,6 +169,8 @@ Open:

- **Squash and rebase merges.** Out of scope for now. Supporting them would need a commit's history to also reach the commits of any pull request merged at one of its ancestors, and a pull request tied to its merge commit when it merges.
- **Known limit: history stops at 1,000 commits.** A story whose newest approval is further back has no reference, and reads as added. It's accepted for now, and a cache will replace it. Only approvals count, so this starts to matter 1,000 commits after a project's first approval, however long its history was before that.
- **Equal-time approvals.** Two approvals of the same image at the same commit in the same millisecond can leave stores with different originating batch IDs. A batch excludes its own approval, so this can change its reference. When approvals of different images tie in reachable history, commit order can also change the reference. A portable tie-break is deferred.
- **Decision history size on D1.** `decisionsAt` and `compareReached` read reachable decisions before reducing them. When one query result exceeds D1's 50 MB limit, sync or comparison can fail. Bounding those queries is deferred.
- Whether the reference needs a derived table for the cloud. Locally it doesn't (rationale). If it does, that table can always be rebuilt, and is never a source of truth.
- A shallow clone can't walk its history, so CI needs a full clone.
- An approval on a branch with no commits of its own lands on a commit the default branch also has, so it counts there too.
Expand Down
Loading
Loading