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
}