oficina / sheets Public

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

Plain git — no account needed to clone.

Download

Download this file

1// Models for everything this package persists.
2//
3// They live here, in package store, rather than in package sheets where
4// they started. The move happened when the HTTP layer needed to open a
5// Handle: store imported sheets for these structs, so sheets could not
6// import store back, and every route that reads a cell is in package
7// sheets. Putting them where the persistence is removes the cycle
8// rather than routing around it — and it is where models.go's own
9// comment on Event already said they conceptually belonged.
10//
11// package sheets keeps Member, which is an app concern (who may hold a
12// session) rather than a storage one, and keeps the Models list that
13// `rastrillo migration check` reads: the list is a []any of values, so
14// it names these types across the package boundary without needing
15// them declared beside it.
16package store
17
18import "time"
19
20// Sheet is one spreadsheet document.
21type Sheet struct {
22 ID uint64 `gorm:"primaryKey"`
23 TitleSealed []byte // seal purpose "sheet.title"
24 CreatedBy uint64
25 CreatedAt time.Time
26 UpdatedAt time.Time
27 DeletedAt *time.Time `gorm:"index"`
28 // FillingSince is set while a sheet is being filled from an
29 // imported file and cleared when that finishes. Non-nil means the
30 // sheet is NOT yet a thing anybody should see: every read path
31 // filters it out exactly as it filters DeletedAt.
32 //
33 // It exists because import cannot be one transaction. Cells are
34 // written and committed one at a time (Handle.Set recalculates and
35 // persists as it goes), so between the sheet row and the last cell
36 // there is a window in which the sheet exists and is wrong — and on
37 // this instance every team member sees every sheet, so that window
38 // is visible to the whole team. Three things go wrong in it: a
39 // half-filled sheet looks complete, a collaborator can edit one
40 // that is about to be discarded, and a process that stops
41 // mid-import (a deploy) leaves the partial behind forever, because
42 // the job that would have cleaned it up was in memory and died with
43 // it.
44 //
45 // A timestamp rather than a bool so the stale ones are also
46 // identifiable — an import interrupted by a deploy leaves a row
47 // whose FillingSince never clears, and a sweeper or an operator can
48 // find those by age. Invisible is already the safe state, so
49 // nothing has to run for correctness.
50 FillingSince *time.Time `gorm:"index"`
51}
52
53// ImportReport is what an import changed or left behind, one per
54// imported sheet: xlsx.Report's codes, coordinates and counts, never
55// content. Sealed all the same, because which cells were frozen or
56// differ is a fact about the sheet, and the bucket holds none of those
57// in plaintext.
58//
59// It is written in the same transaction that reveals the sheet, so a
60// visible imported sheet always has its report and a discarded import
61// never leaves one behind.
62type ImportReport struct {
63 SheetID uint64 `gorm:"primaryKey;autoIncrement:false"`
64 ReportSealed []byte // seal purpose "import.report"
65 CreatedAt time.Time
66}
67
68// Tab is one sheet within a document — the tabs along the bottom.
69type Tab struct {
70 ID uint64 `gorm:"primaryKey"`
71 SheetID uint64 `gorm:"index"`
72 NameSealed []byte // seal purpose "tab.name"
73 Position int
74 FrozenRows int
75 FrozenCols int
76 ContentVersion uint64 `gorm:"not null;default:0"`
77}
78
79// Row and Col carry stable identity. Position is a property of the record,
80// never part of a key — so inserting a row above a comment or a range does
81// not move what they point at. See spec section 4.2 for what this does and
82// does not buy: it removes reference rewriting for insert and delete only.
83//
84// (TabID, Position) is a uniqueIndex, not a plain one: at most one Row (or
85// Col) may ever claim a given grid coordinate within a tab. Nothing wrote
86// Row/Col rows until store.Handle.EnsureRow/EnsureCol (XLSX import), which
87// is why this constraint arrives with them rather than with the schema
88// itself — see that file's doc comment for the race a plain index would
89// leave open across two processes.
90type Row struct {
91 ID uint64 `gorm:"primaryKey"`
92 TabID uint64 `gorm:"uniqueIndex:idx_row_tab_pos"`
93 Position int `gorm:"uniqueIndex:idx_row_tab_pos"`
94 Height int
95}
96
97type Col struct {
98 ID uint64 `gorm:"primaryKey"`
99 TabID uint64 `gorm:"uniqueIndex:idx_col_tab_pos"`
100 Position int `gorm:"uniqueIndex:idx_col_tab_pos"`
101 Width int
102}
103
104// Cell is sparse: only non-empty cells exist.
105//
106// CacheVersion records the InputVersion its CachedSealed was derived from.
107// A cache whose CacheVersion trails its input is stale and must not be
108// served — which is how a crash mid-recalc is detected rather than assumed
109// away. See spec section 4.4.
110type Cell struct {
111 TabID uint64 `gorm:"primaryKey"`
112 RowID uint64 `gorm:"primaryKey"`
113 ColID uint64 `gorm:"primaryKey"`
114 InputSealed []byte // seal purpose "cell.input"
115 CachedSealed []byte // seal purpose "cell.cached"
116 InputVersion uint64
117 CacheVersion uint64
118 FormatID *uint64
119 UpdatedBy uint64
120 UpdatedAt time.Time
121}
122
123// Format is interned. A column of 5,000 identically styled cells is one row
124// here, not 5,000 — which is the difference between a format change being
125// cheap and being a table rewrite.
126type Format struct {
127 ID uint64 `gorm:"primaryKey"`
128 Digest string `gorm:"uniqueIndex;size:64"`
129 NumberFmt string
130 Bold bool
131 Italic bool
132 FillIntentH *float64 // OKLCH hue; nil means no fill
133 FillIntentC *float64 // OKLCH chroma
134 FillHex string // retained from XLSX import for lossless re-export
135 // InkHex is the AUTHOR'S font colour, retained from XLSX import the
136 // same way FillHex is, and for the same reason: re-exporting a file
137 // with a colour we invented would lie about it.
138 //
139 // It exists because spec §11.1's obligation needs a pair. A fill
140 // alone cannot fail a contrast check — a fill plus the ink somebody
141 // chose to put on it can, and Excel ships such a pair as a preset
142 // (#9C6500 on #FFEB9C, 4.12:1). Empty means the file said nothing,
143 // and the theme's own ink is used, which is a pairing this app
144 // controls and cannot get wrong.
145 InkHex string
146 // InkIntentH/InkIntentC are ink's hue and chroma with no lightness,
147 // the same shape as FillIntentH/C and for the same reason: a colour
148 // THIS app applied should follow the reader's light/dark choice,
149 // where a colour an imported file authored must not. So a text
150 // colour picked in the toolbar is stored as intent and resolved
151 // per-theme, and InkHex stays what it has always been — the
152 // author's literal, retained for lossless re-export.
153 //
154 // Nil means "no ink of ours"; the theme's own text colour is used,
155 // which is a pairing this app controls and cannot get wrong.
156 InkIntentH *float64
157 InkIntentC *float64
158 Align string
159}
160
161// Event is the append-only log: live-sync fan-out, version history, undo
162// and audit. Its behaviour (AppendInput, AppendRecalc, the Kind consts)
163// lives in package store, which is where it conceptually belongs — store
164// owns persistence. The struct lives here instead, aliased back as
165// store.Event, purely because of what migrations.go needs: `rastrillo
166// migration check` diffs this package's Models against Schema, and
167// package sheets cannot import store (store already imports sheets for
168// Sheet/Tab/Row/Col/Cell/Format, so the reverse import would cycle). A
169// model the check can't see is a model it can never catch drifting, which
170// defeats the point of this file. See migrations.go and store/events.go.
171type Event struct {
172 ID uint64 `gorm:"primaryKey"`
173 Kind string `gorm:"index;size:16"`
174 TabID uint64 `gorm:"index"`
175 ActorID uint64
176 PayloadSealed []byte // seal purpose "event.payload"; empty for recalc
177 TriggerID uint64 // recalc only: the input event that caused it
178 DerivedCount int // recalc only: how many cells the pass touched
179 CreatedAt time.Time
180}
181