A vibe coded tangled fork which supports pijul.
1package db
2
3import (
4 "context"
5 "database/sql"
6 "fmt"
7 "strings"
8 _ "github.com/mattn/go-sqlite3"
9)
10
11func Make(ctx context.Context, dbPath string) (*sql.DB, error) {
12 // https://github.com/mattn/go-sqlite3#connection-string
13 opts := []string{
14 "_foreign_keys=1",
15 "_journal_mode=WAL",
16 "_synchronous=NORMAL",
17 "_auto_vacuum=incremental",
18 }
19
20 db, err := sql.Open("sqlite3", dbPath+"?"+strings.Join(opts, "&"))
21 if err != nil {
22 return nil, err
23 }
24
25 conn, err := db.Conn(ctx)
26 if err != nil {
27 return nil, err
28 }
29 defer conn.Close()
30
31 _, err = conn.ExecContext(ctx, `
32 create table if not exists repos (
33 did text not null,
34 rkey text not null,
35 at_uri text generated always as ('at://' || did || '/' || 'sh.tangled.repo' || '/' || rkey) stored,
36 cid text not null,
37
38 -- record content
39 name text not null,
40 knot_domain text not null,
41
42 -- sync data
43 git_rev text not null,
44 repo_sha text not null,
45 state text not null default 'pending',
46 error_msg text,
47 retry_count integer not null default 0,
48 retry_after integer not null default 0,
49
50 unique(did, rkey)
51 );
52
53 -- knot hosts
54 create table if not exists hosts (
55 hostname text not null,
56 no_ssl integer not null default 0,
57 status text not null default 'active',
58 last_seq integer not null default -1,
59
60 unique(hostname)
61 );
62 `)
63 if err != nil {
64 return nil, fmt.Errorf("initializing db schema: %w", err)
65 }
66
67 return db, nil
68}