| 1 | package mapa |
| 2 | |
| 3 | import "time" |
| 4 | |
| 5 | // Models is every model the schema generator manages. Keep it in step with |
| 6 | // the structs below: `rastrillo migration check` fails CI when it and the |
| 7 | // migrations disagree. |
| 8 | var Models = []any{ |
| 9 | &Member{}, &Page{}, &Alias{}, &TitlePrint{}, &DailyPrint{}, |
| 10 | &Revision{}, &Draft{}, &Event{}, &ImageRow{}, &ImageTrash{}, |
| 11 | &NotionConnection{}, &NotionRun{}, &NotionPage{}, &NotionImage{}, |
| 12 | } |
| 13 | |
| 14 | // The design is docs/superpowers/specs/2026-11-12-mapa-design.md. §3.1 is |
| 15 | // this file; §4 is the rule every column below answers to: plaintext only |
| 16 | // where the durable file must answer without the key. The gate that holds |
| 17 | // the list to §4 is plaintext_test.go, and it reads the real schema. |
| 18 | |
| 19 | // Kinds and rungs (§3.3). A page's rung is fixed when it is created |
| 20 | // (decision 8), so no write path ever changes Page.Rung. |
| 21 | const ( |
| 22 | KindPage = "page" |
| 23 | KindDaily = "daily" |
| 24 | KindScratch = "scratch" |
| 25 | |
| 26 | RungTeam = "team" |
| 27 | RungPrivate = "private" |
| 28 | ) |
| 29 | |
| 30 | // Member is the lone-shape admission list's local mirror (§3.1): a person |
| 31 | // this instance has seen write. The address is sealed for display and |
| 32 | // printed for lookup; the time zone is the person's own setting. |
| 33 | // |
| 34 | // No timestamps: §4 lists a member's id and address print, nothing more. |
| 35 | type Member struct { |
| 36 | ID int64 |
| 37 | AddressPrint string `gorm:"uniqueIndex;size:64;not null"` |
| 38 | SealedAddress []byte |
| 39 | SealedTimeZone []byte |
| 40 | // SealedPins is the page ids the person pinned to their sidebar |
| 41 | // (level-up §3.2, §8; §11 Q1 proposed per person): sealed, so the |
| 42 | // file does not say which pages anyone pinned. |
| 43 | SealedPins []byte |
| 44 | // SealedLocale is the person's own date and number format, an |
| 45 | // offered locale's code (level-up build note 85), sealed as the |
| 46 | // time zone is. |
| 47 | SealedLocale []byte |
| 48 | } |
| 49 | |
| 50 | // Page is a team page, a daily note or a scratch page. Its title, date and |
| 51 | // body are one sealed record; nothing it says is in this struct. |
| 52 | type Page struct { |
| 53 | ID int64 |
| 54 | // Nonce is the page's half of its suite item id, "<id>.<nonce>", |
| 55 | // written once at creation (doctrine §6.6). It is also the page's |
| 56 | // public id: every URL names a page by it, never by ID, because a |
| 57 | // sequential row id counts everyone's pages, private ones included |
| 58 | // (review M2). |
| 59 | Nonce int64 `gorm:"not null;uniqueIndex"` |
| 60 | Kind string `gorm:"size:8;not null;index"` |
| 61 | Rung string `gorm:"size:8;not null"` |
| 62 | // OwnerID is a member row: the creator of a team page, and the only |
| 63 | // reader of a private one. S2 answers private → owner from it |
| 64 | // without unsealing (§4). |
| 65 | OwnerID int64 `gorm:"not null;index"` |
| 66 | Owner *Member `gorm:"constraint:OnDelete:RESTRICT"` |
| 67 | // Revision is a random positive int63, never a counter, so a |
| 68 | // restored database cannot reissue one memoria holds (§5). |
| 69 | Revision int64 `gorm:"not null"` |
| 70 | // Gone is S1's "gone": the page was deleted. The row stays, so its |
| 71 | // record can be restored as a new page inside the window (§3.4). |
| 72 | Gone bool `gorm:"not null;default:false;index"` |
| 73 | SealedRecord []byte |
| 74 | // The store sets both, from its own clock: GORM's automatic |
| 75 | // timestamps would put the wall clock where the store's belongs. |
| 76 | CreatedAt time.Time `gorm:"autoCreateTime:false"` |
| 77 | UpdatedAt time.Time `gorm:"index;autoUpdateTime:false"` |
| 78 | } |
| 79 | |
| 80 | // Alias is a title a team page used to have. It reserves that title |
| 81 | // until it is freed or its page is deleted (§3.4). |
| 82 | type Alias struct { |
| 83 | ID int64 |
| 84 | PageID int64 `gorm:"not null;index"` |
| 85 | Page *Page `gorm:"constraint:OnDelete:CASCADE"` |
| 86 | SealedTitle []byte |
| 87 | } |
| 88 | |
| 89 | // TitlePrint makes the durable file refuse a duplicate title without |
| 90 | // holding one: the HMAC of a folded title, for every current title and |
| 91 | // alias of a live team page. Unique across both (§3.1). |
| 92 | type TitlePrint struct { |
| 93 | Print string `gorm:"primaryKey;size:64"` |
| 94 | PageID int64 `gorm:"not null;index"` |
| 95 | Page *Page `gorm:"constraint:OnDelete:CASCADE"` |
| 96 | } |
| 97 | |
| 98 | // DailyPrint is one daily note per person per day, without a plaintext |
| 99 | // date: the HMAC of (owner, date). |
| 100 | type DailyPrint struct { |
| 101 | Print string `gorm:"primaryKey;size:64"` |
| 102 | PageID int64 `gorm:"not null;uniqueIndex"` |
| 103 | Page *Page `gorm:"constraint:OnDelete:CASCADE"` |
| 104 | } |
| 105 | |
| 106 | // Revision is the revision store (§5): every committed revision of a |
| 107 | // page, kept for the history window and pruned after it. |
| 108 | type Revision struct { |
| 109 | PageID int64 `gorm:"primaryKey;autoIncrement:false"` |
| 110 | Page *Page `gorm:"constraint:OnDelete:CASCADE"` |
| 111 | Revision int64 `gorm:"primaryKey;autoIncrement:false"` |
| 112 | At time.Time `gorm:"not null;index"` |
| 113 | ActorID int64 `gorm:"not null"` |
| 114 | SealedRecord []byte |
| 115 | } |
| 116 | |
| 117 | // Draft is one person's coalesced autosave text for one page, before it |
| 118 | // is a revision (§5). Keyed by page, or by daily print for a daily note |
| 119 | // that does not exist yet; its first commit creates the note. |
| 120 | // |
| 121 | // A draft is not an item: it has no revision, is never in S1 or S3, and |
| 122 | // is the one write exempt from gate 8 by name. |
| 123 | type Draft struct { |
| 124 | ID int64 |
| 125 | PageID *int64 `gorm:"uniqueIndex:draft_page"` |
| 126 | DailyPrint *string `gorm:"size:64;uniqueIndex:draft_day"` |
| 127 | PersonID int64 `gorm:"uniqueIndex:draft_page;uniqueIndex:draft_day"` |
| 128 | // BaseRevision is the revision the person started from: the merge |
| 129 | // base when the draft is committed. |
| 130 | BaseRevision int64 |
| 131 | // Version is random, redrawn on every draft write. Two tabs share a |
| 132 | // draft, and a tab whose version is not the stored one is stale |
| 133 | // (§5). Random, not a counter, so it says nothing but "changed". |
| 134 | Version int64 `gorm:"not null"` |
| 135 | SealedRecord []byte |
| 136 | UpdatedAt time.Time `gorm:"index;autoUpdateTime:false"` |
| 137 | } |
| 138 | |
| 139 | // Event is one row for every write (§3.1): the feed for the in-memory |
| 140 | // index and, from slice 1 item 6, for S1. A write that logs no event is |
| 141 | // invisible to both, so store.go refuses to commit one (gate 8). |
| 142 | type Event struct { |
| 143 | ID int64 |
| 144 | Nonce int64 `gorm:"not null"` |
| 145 | // At's index, idx_events_at, is named by Recently edited's bulk |
| 146 | // check (recent.go, INDEXED BY): drop or rename it and that query |
| 147 | // fails outright, taking Pinned and Recently edited off every page. |
| 148 | // TestRecentlyEditedReadsByIndex holds it (build note 80). |
| 149 | At time.Time `gorm:"not null;index"` |
| 150 | // ActorID is the person who made the change (suite ruling 9). Mapa |
| 151 | // is never an actor. idx_events_actor_page is Recently edited's: |
| 152 | // a person's pages, each one step, and each page's writes newest |
| 153 | // first by the rowid it ends in (recent.go, build note 80). |
| 154 | ActorID int64 `gorm:"not null;index;index:idx_events_actor_page"` |
| 155 | ObjectKind string `gorm:"size:8;not null;index:idx_events_actor_page"` |
| 156 | ObjectID int64 `gorm:"not null;index;index:idx_events_actor_page"` |
| 157 | Verb string `gorm:"size:16;not null"` |
| 158 | // Revision is the object's revision after this event, or 0 for a |
| 159 | // verb that mints none. |
| 160 | Revision int64 `gorm:"not null"` |
| 161 | } |
| 162 | |
| 163 | // Object kinds and verbs. The vocabulary is ours, never a team's words. |
| 164 | const ( |
| 165 | ObjectPage = "page" |
| 166 | ObjectMember = "member" |
| 167 | |
| 168 | VerbCreated = "created" |
| 169 | VerbEdited = "edited" |
| 170 | VerbRenamed = "renamed" |
| 171 | VerbDeleted = "deleted" |
| 172 | VerbRestored = "restored" |
| 173 | VerbAliasFreed = "alias_freed" |
| 174 | VerbJoined = "joined" |
| 175 | VerbTimeZoneSet = "timezone_set" |
| 176 | // VerbLocaleSet is a change to a person's date and number format. |
| 177 | // Its object is the member (build note 85). |
| 178 | VerbLocaleSet = "locale_set" |
| 179 | // VerbPinsSet is a change to a person's pins. Its object is the |
| 180 | // member, never the page (level-up §8). |
| 181 | VerbPinsSet = "pins" |
| 182 | |
| 183 | // VerbRelinked is "Update links" and "Link it", and VerbMarked is |
| 184 | // "Link to this block", on team pages only (level-up §8, N1): a |
| 185 | // private page logs VerbEdited for both, so the plaintext log says |
| 186 | // nothing about its links. Each replaces the edited row the same |
| 187 | // write would have logged; neither adds one. |
| 188 | VerbRelinked = "relinked" |
| 189 | VerbMarked = "marked" |
| 190 | |
| 191 | // ObjectImport is a person's own act on the Notion import: its |
| 192 | // connection and its runs (Notion import spec §14.2). The index and |
| 193 | // S1 ignore it; it is in the log so gate 8 holds for its routes. |
| 194 | ObjectImport = "import" |
| 195 | ) |
| 196 | |
| 197 | // linkVerb is the verb a link edit to page logs: verb on a team page, |
| 198 | // VerbEdited on a private one (level-up §8). |
| 199 | func linkVerb(page Page, verb string) string { |
| 200 | if page.Rung == RungTeam { |
| 201 | return verb |
| 202 | } |
| 203 | return VerbEdited |
| 204 | } |
| 205 | |
| 206 | // ImageRow is one image in one place (images spec §1.3): its bytes are |
| 207 | // sealed in the object store under ObjectKey, and its sniffed type, |
| 208 | // size and true length in SealedRecord. Everything plaintext here is |
| 209 | // the images spec's gate-4 row: the serving, scope, quota and |
| 210 | // retention it needs without unsealing. |
| 211 | type ImageRow struct { |
| 212 | ID int64 |
| 213 | // PublicID is the URL's id, /i/<public id>: random base 36 from its |
| 214 | // own nonce stream, so it counts nothing. |
| 215 | PublicID string `gorm:"size:16;not null;uniqueIndex"` |
| 216 | // OwnerID is who uploaded or copied it. A pending image is its |
| 217 | // owner's only, and the person's allowance counts by it. |
| 218 | OwnerID int64 `gorm:"not null;index:img_owner,priority:1"` |
| 219 | // PageID is the page it is attached to, nil while pending. Its rung |
| 220 | // is the page's (images §2.4). |
| 221 | PageID *int64 `gorm:"index:img_owner,priority:2;index:img_page,priority:1"` |
| 222 | // CreatedAt starts the pending window (§3.3). |
| 223 | CreatedAt time.Time `gorm:"not null;autoCreateTime:false"` |
| 224 | // UnreferencedAt is when the page's current body stopped naming it, |
| 225 | // stamped once (§3.3); nil while the body names it. |
| 226 | UnreferencedAt *time.Time `gorm:"index:img_page,priority:2"` |
| 227 | // StoredBytes is the ciphertext's size, padded to 64 KiB steps. |
| 228 | StoredBytes int64 `gorm:"not null"` |
| 229 | // MovedFrom is the deleted page a Restore moved it from, so a draft |
| 230 | // left on that page follows it (Fable L4). |
| 231 | MovedFrom *int64 |
| 232 | // ObjectKey is the SHA-256 of the ciphertext, the bucket's key. |
| 233 | ObjectKey string `gorm:"size:64;not null"` |
| 234 | SealedRecord []byte |
| 235 | } |
| 236 | |
| 237 | // TableName is images: the model is ImageRow so that Image can be what |
| 238 | // the store hands out. |
| 239 | func (ImageRow) TableName() string { return "images" } |
| 240 | |
| 241 | // ImageTrash is the queue of bucket objects to delete (§3.3): keys |
| 242 | // only, emptied after the transaction that queued them, and off the |
| 243 | // boot path. |
| 244 | type ImageTrash struct { |
| 245 | ObjectKey string `gorm:"primaryKey;size:64"` |
| 246 | QueuedAt time.Time `gorm:"not null;autoCreateTime:false"` |
| 247 | } |
| 248 | |
| 249 | // TableName is image_trash, as the spec names it. |
| 250 | func (ImageTrash) TableName() string { return "image_trash" } |
| 251 | |