Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Site
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software/vole.git

Self-hosted forge that lives in your Git repo.

Move release channels to an authoritative channels.db

262e516e838cfc4b16b5d00fd98d5e4ef135aa85 · Keenan Nemetz · 19d ago · parent 24019cb3

Move release channels to an authoritative channels.db

Channels are consumed from the public URL and are server-side publication state,
so they no longer live on the vole branch or in the derived index.

- forge/channel: authoritative SQLite store with schema, validation, and CRUD
- config: data/channels.db, opened and closed with the instance
- CLI: release channel set|list|clear over a new vole-channel SSH verb
- server: maintain for set/clear, read for list, annotated-tag validation,
  audit channel.set/channel.clear, JSON list
- web: channel page and tree read the store; release badges query it
- remove the metadata-branch channel parser/validator, the index-derived channel
  table, and the hook path rules
- backup, instance-data, and architecture docs include channels.db

Refs: 01M7BPHANWMSW5HK2GT0PY74QZ

forge/channel/channel.go +226

@@ -0,0 +1,226 @@
1 // Package channel stores release channels: server-side pointers from a stable
2 // name to one immutable release.
3 //
4 // A channel is consumed from the instance's public URL, not cloned, so the source
5 // of truth is an authoritative SQLite database rather than the repository's
6 // metadata branch. The target tag name and tag object are recorded; the web and
7 // the SSH verbs resolve the tag in the repository and report a dangling channel
8 // when it no longer matches.
9 package channel
10
11 import (
12 "database/sql"
13 "encoding/hex"
14 "errors"
15 "fmt"
16 "regexp"
17 "strings"
18 "time"
19
20 _ "github.com/mattn/go-sqlite3"
21 )
22
23 const (
24 // MaxNameLen bounds the URL segment.
25 MaxNameLen = 32
26 // MaxTagLen is generous for a tag name and still bounds a row.
27 MaxTagLen = 200
28 // MaxDescription bounds the prose shown on the channel page.
29 MaxDescription = 8 << 10
30 )
31
32 var nameRE = regexp.MustCompile(`^[a-z0-9][a-z0-9-]{0,31}$`)
33
34 // Channel is one mutable alias for a release.
35 type Channel struct {
36 Name string
37 Release string
38 ReleaseObject string
39 UpdatedBy string
40 UpdatedAt time.Time
41 Description string
42 }
43
44 // ValidName checks the URL segment. Lowercase keeps the URL and the database key
45 // from differing by case.
46 func ValidName(name string) error {
47 switch {
48 case name == "":
49 return fmt.Errorf("a channel needs a name")
50 case len(name) > MaxNameLen:
51 return fmt.Errorf("channel name is %d bytes, limit %d", len(name), MaxNameLen)
52 case !nameRE.MatchString(name):
53 return fmt.Errorf("channel name %q must be lowercase letters, digits, and dashes, starting with a letter or digit", name)
54 }
55 return nil
56 }
57
58 func validObjectID(id string) error {
59 if len(id) != 40 && len(id) != 64 {
60 return fmt.Errorf("release_object %q is not a 40- or 64-character object id", id)
61 }
62 if _, err := hex.DecodeString(id); err != nil {
63 return fmt.Errorf("release_object %q is not hexadecimal", id)
64 }
65 return nil
66 }
67
68 func validate(ch *Channel) error {
69 switch {
70 case ValidName(ch.Name) != nil:
71 return ValidName(ch.Name)
72 case ch.Release == "":
73 return fmt.Errorf("a channel needs a release tag")
74 case len(ch.Release) > MaxTagLen:
75 return fmt.Errorf("release tag is %d bytes, limit %d", len(ch.Release), MaxTagLen)
76 case strings.ContainsAny(ch.Release, "\x00\n\r\t"):
77 return fmt.Errorf("release tag %q contains a control character", ch.Release)
78 }
79 if err := validObjectID(ch.ReleaseObject); err != nil {
80 return err
81 }
82 if strings.TrimSpace(ch.UpdatedBy) == "" {
83 return fmt.Errorf("updated_by is required")
84 }
85 if ch.UpdatedAt.IsZero() {
86 return fmt.Errorf("updated_at is required")
87 }
88 if len(ch.Description) > MaxDescription {
89 return fmt.Errorf("description is %d bytes, limit %d", len(ch.Description), MaxDescription)
90 }
91 return nil
92 }
93
94 // Store is the authoritative channel table. It is a separate database from the
95 // derived web index: a channel is source state, not a row an index rebuild may
96 // discard.
97 type Store struct{ db *sql.DB }
98
99 // Open opens or creates the channel database.
100 func Open(path string) (*Store, error) {
101 db, err := sql.Open("sqlite3", path+"?_busy_timeout=5000&_journal_mode=WAL")
102 if err != nil {
103 return nil, err
104 }
105 db.SetMaxOpenConns(4)
106 s := &Store{db: db}
107 if _, err := db.Exec(`
108 CREATE TABLE IF NOT EXISTS release_channels (
109 repo_id TEXT NOT NULL,
110 name TEXT NOT NULL,
111 tag TEXT NOT NULL,
112 tag_object TEXT NOT NULL,
113 updated_by TEXT NOT NULL,
114 updated_at INTEGER NOT NULL,
115 description TEXT NOT NULL DEFAULT '',
116 PRIMARY KEY (repo_id, name)
117 );
118 CREATE INDEX IF NOT EXISTS release_channels_tag ON release_channels(repo_id, tag);
119 `); err != nil {
120 db.Close()
121 return nil, fmt.Errorf("channel: create schema: %w", err)
122 }
123 return s, nil
124 }
125
126 // Close releases the database.
127 func (s *Store) Close() error {
128 if s == nil || s.db == nil {
129 return nil
130 }
131 return s.db.Close()
132 }
133
134 // Set creates or moves a channel. One row per (repository, name); a move replaces
135 // the target and metadata in one statement.
136 func (s *Store) Set(repoID string, ch Channel) error {
137 if err := validate(&ch); err != nil {
138 return err
139 }
140 _, err := s.db.Exec(`
141 INSERT INTO release_channels(repo_id, name, tag, tag_object, updated_by, updated_at, description)
142 VALUES (?,?,?,?,?,?,?)
143 ON CONFLICT(repo_id, name) DO UPDATE SET
144 tag=excluded.tag,
145 tag_object=excluded.tag_object,
146 updated_by=excluded.updated_by,
147 updated_at=excluded.updated_at,
148 description=excluded.description`,
149 repoID, ch.Name, ch.Release, ch.ReleaseObject, ch.UpdatedBy, ch.UpdatedAt.Unix(), ch.Description)
150 if err != nil {
151 return fmt.Errorf("channel: set %s: %w", ch.Name, err)
152 }
153 return nil
154 }
155
156 // Clear removes a channel. The bool reports whether a row existed.
157 func (s *Store) Clear(repoID, name string) (bool, error) {
158 if err := ValidName(name); err != nil {
159 return false, err
160 }
161 res, err := s.db.Exec(`DELETE FROM release_channels WHERE repo_id=? AND name=?`, repoID, name)
162 if err != nil {
163 return false, fmt.Errorf("channel: clear %s: %w", name, err)
164 }
165 n, err := res.RowsAffected()
166 return n > 0, err
167 }
168
169 // Get returns one channel.
170 func (s *Store) Get(repoID, name string) (Channel, bool, error) {
171 var ch Channel
172 var at int64
173 err := s.db.QueryRow(`
174 SELECT name, tag, tag_object, updated_by, updated_at, description
175 FROM release_channels WHERE repo_id=? AND name=?`, repoID, name).
176 Scan(&ch.Name, &ch.Release, &ch.ReleaseObject, &ch.UpdatedBy, &at, &ch.Description)
177 if errors.Is(err, sql.ErrNoRows) {
178 return Channel{}, false, nil
179 }
180 if err != nil {
181 return Channel{}, false, err
182 }
183 ch.UpdatedAt = time.Unix(at, 0).UTC()
184 return ch, true, nil
185 }
186
187 // List returns every channel for a repository, sorted by name.
188 func (s *Store) List(repoID string) ([]Channel, error) {
189 rows, err := s.db.Query(`
190 SELECT name, tag, tag_object, updated_by, updated_at, description
191 FROM release_channels WHERE repo_id=? ORDER BY name`, repoID)
192 if err != nil {
193 return nil, err
194 }
195 defer rows.Close()
196 var out []Channel
197 for rows.Next() {
198 var ch Channel
199 var at int64
200 if err := rows.Scan(&ch.Name, &ch.Release, &ch.ReleaseObject, &ch.UpdatedBy, &at, &ch.Description); err != nil {
201 return nil, err
202 }
203 ch.UpdatedAt = time.Unix(at, 0).UTC()
204 out = append(out, ch)
205 }
206 return out, rows.Err()
207 }
208
209 // ForTag returns the channel names that currently name a tag, sorted.
210 func (s *Store) ForTag(repoID, tag string) ([]string, error) {
211 rows, err := s.db.Query(
212 `SELECT name FROM release_channels WHERE repo_id=? AND tag=? ORDER BY name`, repoID, tag)
213 if err != nil {
214 return nil, err
215 }
216 defer rows.Close()
217 var out []string
218 for rows.Next() {
219 var name string
220 if err := rows.Scan(&name); err != nil {
221 return nil, err
222 }
223 out = append(out, name)
224 }
225 return out, rows.Err()
226 }