1package db
2
3import (
4 "database/sql"
5
6 _ "github.com/mattn/go-sqlite3"
7)
8
9type DB struct {
10 *sql.DB
11}
12
13func Make(dbPath string) (*DB, error) {
14 db, err := sql.Open("sqlite3", dbPath)
15 if err != nil {
16 return nil, err
17 }
18
19 _, err = db.Exec(`
20 pragma journal_mode = WAL;
21 pragma synchronous = normal;
22 pragma foreign_keys = on;
23 pragma temp_store = memory;
24 pragma mmap_size = 30000000000;
25 pragma page_size = 32768;
26 pragma auto_vacuum = incremental;
27 pragma busy_timeout = 5000;
28
29 create table if not exists known_dids (
30 did text primary key
31 );
32
33 create table if not exists pipeline_status (
34 rkey text not null,
35 pipeline text not null,
36 status text not null,
37
38 -- only set if status is 'failed'
39 error text,
40 exit_code integer,
41
42 started_at timestamp not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
43 updated_at timestamp not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
44 finished_at timestamp,
45
46 primary key (rkey)
47 );
48 `)
49 if err != nil {
50 return nil, err
51 }
52
53 return &DB{db}, nil
54}
55
56func (d *DB) SaveLastTimeUs(lastTimeUs int64) error {
57 _, err := d.Exec(`
58 insert into _jetstream (id, last_time_us)
59 values (1, ?)
60 on conflict(id) do update set last_time_us = excluded.last_time_us
61 `, lastTimeUs)
62 return err
63}
64
65func (d *DB) GetLastTimeUs() (int64, error) {
66 var lastTimeUs int64
67 row := d.QueryRow(`select last_time_us from _jetstream where id = 1;`)
68 err := row.Scan(&lastTimeUs)
69 return lastTimeUs, err
70}