A vibe coded tangled fork which supports pijul.
at 30381c8a74e47c864b80604b23ab42f947626fb3 74 lines 1.7 kB view raw
1package db 2 3import ( 4 "context" 5 "database/sql" 6 "fmt" 7 "time" 8 9 _ "github.com/jackc/pgx/v5/stdlib" 10) 11 12func Make(ctx context.Context, dbUrl string, maxConns int) (*sql.DB, error) { 13 db, err := sql.Open("pgx", dbUrl) 14 if err != nil { 15 return nil, fmt.Errorf("opening db: %w", err) 16 } 17 18 db.SetMaxOpenConns(maxConns) 19 db.SetMaxIdleConns(maxConns) 20 db.SetConnMaxIdleTime(time.Hour) 21 22 pingCtx, cancel := context.WithTimeout(ctx, 5*time.Second) 23 defer cancel() 24 if err := db.PingContext(pingCtx); err != nil { 25 db.Close() 26 return nil, fmt.Errorf("ping db: %w", err) 27 } 28 29 conn, err := db.Conn(ctx) 30 if err != nil { 31 return nil, err 32 } 33 defer conn.Close() 34 35 _, err = conn.ExecContext(ctx, ` 36 create table if not exists repos ( 37 did text not null, 38 rkey text not null, 39 at_uri text generated always as ('at://' || did || '/' || 'sh.tangled.repo' || '/' || rkey) stored, 40 cid text not null, 41 42 -- record content 43 name text not null, 44 knot_domain text not null, 45 46 -- sync data 47 git_rev text not null, 48 repo_sha text not null, 49 state text not null default 'pending', 50 error_msg text, 51 retry_count integer not null default 0, 52 retry_after integer not null default 0, 53 54 constraint repos_pkey primary key (did, rkey) 55 ); 56 57 -- knot hosts 58 create table if not exists hosts ( 59 hostname text not null, 60 no_ssl boolean not null default false, 61 status text not null default 'active', 62 last_seq bigint not null default -1, 63 64 constraint hosts_pkey primary key (hostname) 65 ); 66 67 create index if not exists idx_repos_aturi on repos (at_uri); 68 `) 69 if err != nil { 70 return nil, fmt.Errorf("initializing db schema: %w", err) 71 } 72 73 return db, nil 74}