~bigbes/lethe

ref: 72508a536dd54662af89cce0002f6f3dee1ed0f8 lethe/internal/collector/state/migrations.go -rw-r--r-- 998 bytes
72508a53 — Eugene Blikh docs: refresh search and opencode plan 24 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package state

import (
	"context"

	"github.com/jmoiron/sqlx"
	"go.bigb.es/auxilia/culpa"
)

// applyMigrations runs idempotent DDL to create the ingestion_state and
// outbox tables. It is safe to call multiple times.
func applyMigrations(_ context.Context, db *sqlx.DB) error {
	stmts := []string{
		`CREATE TABLE IF NOT EXISTS ingestion_state (
			tool        TEXT NOT NULL,
			source_file TEXT NOT NULL,
			last_offset INTEGER NOT NULL DEFAULT 0,
			updated_at  INTEGER NOT NULL,
			PRIMARY KEY (tool, source_file)
		)`,
		`CREATE TABLE IF NOT EXISTS outbox (
			id          INTEGER PRIMARY KEY AUTOINCREMENT,
			tool        TEXT NOT NULL,
			host        TEXT NOT NULL,
			source_file TEXT NOT NULL,
			payload     BLOB NOT NULL,
			created_at  INTEGER NOT NULL
		)`,
		`CREATE INDEX IF NOT EXISTS outbox_created_at ON outbox(created_at)`,
	}
	for _, stmt := range stmts {
		if _, err := db.Exec(stmt); err != nil {
			return culpa.Wrap(err, "migration statement failed")
		}
	}
	return nil
}