oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1package docs
2
3import (
4 "embed"
5 "time"
6
7 "amadan.net/rastrillo/rastrillo/migrate"
8)
9
10// migrationFS embeds this app's own numbered migrations — per
11// rastrillo SKILL.md's app shape ("Two *migrate.Set values live in
12// internal/<app>/migrations.go, beside models.go"; kept in this file
13// instead of a separate migrations.go because this task's file list
14// is models.go, store.go, store_test.go and a fourth file would only
15// split one small declaration across two places for no benefit).
16//
17//go:embed migrations/*.sql
18var migrationFS embed.FS
19
20// Schema is this app's own migrations — spec §4's documents and
21// doc_updates tables, only what this plan needs. It is idempotent
22// (CREATE TABLE IF NOT EXISTS today; later plans add further numbered
23// files, additive ALTERs, never edits to 0001) per rastrillo's
24// migration pattern.
25var Schema = migrate.MustFromFS(migrationFS, "docs")
26
27// Document is a document's row. Its text is deliberately not a
28// column: GUID and ClientID are what let Store open the real CRDT
29// core (internal/core) and replay doc_updates into it, which is where
30// the text actually comes from — see Store.Text.
31//
32// GUID and ClientID are assigned by the app (Store.Create) and stored
33// here; they are never drawn from yrs itself — on wasm the CRDT
34// library's entropy is a constant seed, so a value it generates is
35// identical in every process, and a repeated client id or GUID
36// corrupts the document. This is the Go side of the rule clippy.toml
37// enforces in Rust (disallowed-methods on yrs::Doc::new /
38// Doc::with_client_id).
39//
40// OwnerID is the document's owner — Person.ID, never taken from a
41// request body (Store.Create's owner parameter comes from the
42// signed-in person; see handlers.go's owner helper and rastrillo
43// SKILL.md §3/§4's mass-assignment rule). Every read and write is
44// scoped by it through store.go's own owned function, deliberately
45// not rastrillo's scope.Owned — see that function's doc comment and
46// spec §4.2.
47type Document struct {
48 ID int64 `gorm:"primaryKey"`
49 Sqid string `gorm:"column:sqid;uniqueIndex;not null"`
50 Title string `gorm:"column:title;not null"`
51
52 OwnerID int64 `gorm:"column:owner_id;not null;index;uniqueIndex:documents_creation,priority:1"`
53 CreationToken *string `gorm:"column:creation_token;uniqueIndex:documents_creation,priority:2"`
54
55 GUID string `gorm:"column:guid;not null"`
56 ClientID uint64 `gorm:"column:client_id;not null"`
57
58 // Visibility is the document's rung beside grants and links:
59 // VisibilityPrivate or VisibilityTeam (spec §8.2.1). createDocument
60 // always writes it; the default tag only means a row built some other way
61 // (a test inserting a Document directly) is private, as 0011 makes every
62 // existing row, rather than an empty string the CHECK refuses.
63 Visibility Visibility `gorm:"column:visibility;not null;default:private"`
64 // Revision is replaced on every change a suite consumer must re-read:
65 // every applied update, rename, visibility change and deletion, in the
66 // same transaction (bumpRevision, suite_feed.go). Each value is a fresh
67 // random positive int63, never one the document's suite_feed already
68 // holds (suite revision 11 §6.6), so a restore never reissues one; it
69 // has no order, and S2 reads STALE from suite_feed.
70 Revision int64 `gorm:"column:revision;not null;default:1"`
71
72 CreatedAt time.Time `gorm:"column:created_at;not null"`
73 UpdatedAt time.Time `gorm:"column:updated_at;not null"`
74 DeletedAt *time.Time `gorm:"column:deleted_at"`
75}
76
77// Visibility is the team rung's switch (spec §8.2.1, Paul 2026-11-01: "New
78// docs Team, old private"). A link is not a rung and a grant is its own
79// table; this says only whether every admitted member may read it.
80type Visibility string
81
82const (
83 // VisibilityPrivate: the owner and the people it is granted to.
84 VisibilityPrivate Visibility = "private"
85 // VisibilityTeam: also every admitted member, for reading only.
86 VisibilityTeam Visibility = "team"
87)
88
89func (v Visibility) valid() bool { return v == VisibilityPrivate || v == VisibilityTeam }
90
91// SuiteFeedRow is one row of the append-only log suite S1 pages through
92// (0011_suite.sql): which document, at which revision, whether that bump
93// deleted it, and when. Written only by bumpRevision and CreateDraft, inside
94// the transaction that made the change.
95type SuiteFeedRow struct {
96 ID int64 `gorm:"column:id;primaryKey"`
97 DocumentID int64 `gorm:"column:document_id;not null"`
98 Revision int64 `gorm:"column:revision;not null"`
99 Gone bool `gorm:"column:gone;not null"`
100 TouchedAt time.Time `gorm:"column:touched_at;not null"`
101 // Stamp is a random nonce written once with the row; the S1 cursor
102 // carries it so a cursor from before a restore is refused rather than
103 // read as a position in a log that has since been rewritten.
104 Stamp int64 `gorm:"column:stamp;not null"`
105}
106
107// TableName pins the table rather than leaving it to GORM's pluralising.
108func (SuiteFeedRow) TableName() string { return "suite_feed" }
109
110// Person is this app's own record of someone who has signed in —
111// deliberately separate from rastrillo/auth's session Subject, which
112// under the magic-link plugin is the verified email address itself,
113// not a numeric id (rastrillo SKILL.md §5: "Under auth, never
114// sessions.UserID: the Subject is the verified email... map the
115// address to your user row's id before scoping"). Store.EnsurePerson
116// is the one place that mapping happens, get-or-create, keyed on
117// Email. A document's OwnerID is a Person.ID, never an email — so an
118// email can change (a future feature) without rewriting every
119// document row it ever owned.
120type Person struct {
121 ID int64 `gorm:"primaryKey"`
122 Email string `gorm:"column:email;uniqueIndex;not null"`
123
124 CreatedAt time.Time `gorm:"column:created_at;not null"`
125}
126
127// DocUpdate is one Yjs update in a document's log, applied in Seq
128// order to rebuild the document's CRDT state (Store.replay).
129type DocUpdate struct {
130 ID int64 `gorm:"primaryKey"`
131 DocID int64 `gorm:"column:doc_id;not null;index"`
132 Seq int64 `gorm:"column:seq;not null"`
133
134 UpdateBin []byte `gorm:"column:update_bin;not null"`
135 ContentEncoding int `gorm:"column:content_encoding;not null"`
136
137 CreatedAt time.Time `gorm:"column:created_at;not null"`
138}
139
140// DocSnapshot is one document's entire state encoded as a single Yjs
141// update, as of Seq. It is an accelerator for replay and for the update
142// endpoint, never the source of truth — doc_updates keeps every row, and
143// a snapshot that cannot be read is discarded in favour of replaying
144// them (the snapshots design, D2).
145//
146// ABIVersion records the core that produced State. Without it, "discard
147// a snapshot from an older ABI" is unenforceable prose.
148// DocImportReport is the report an import made
149// (migrations/0009_import_reports.sql): docx.Report as JSON, sealed like
150// the update log. One per imported document; a document made any other
151// way has none.
152type DocImportReport struct {
153 ID int64 `gorm:"primaryKey"`
154 DocID int64 `gorm:"column:doc_id;not null;uniqueIndex"`
155
156 ReportBin []byte `gorm:"column:report_bin;not null"`
157 ContentEncoding int `gorm:"column:content_encoding;not null"`
158
159 CreatedAt time.Time `gorm:"column:created_at;not null"`
160}
161
162func (DocImportReport) TableName() string { return "doc_import_reports" }
163
164type DocSnapshot struct {
165 ID int64 `gorm:"primaryKey"`
166 DocID int64 `gorm:"column:doc_id;not null;index"`
167 Seq int64 `gorm:"column:seq;not null"`
168
169 State []byte `gorm:"column:state;not null"`
170 ContentEncoding int `gorm:"column:content_encoding;not null"`
171 ABIVersion uint32 `gorm:"column:abi_version;not null"`
172
173 CreatedAt time.Time `gorm:"column:created_at;not null"`
174}
175