2024-09-08 11:17:49 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
_ "modernc.org/sqlite"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
timestampFormat = "2006-01-02 15:04:05"
|
|
|
|
)
|
|
|
|
|
|
|
|
var migrations = []string{
|
2024-09-09 07:32:05 +02:00
|
|
|
`CREATE TABLE items ("id" TEXT UNIQUE, "kind" TEXT, "updated" TIMESTAMP, "deleted" INTEGER, "body" TEXT)`,
|
2024-09-08 11:17:49 +02:00
|
|
|
`PRAGMA journal_mode=WAL`,
|
|
|
|
`PRAGMA synchronous=NORMAL`,
|
|
|
|
`PRAGMA cache_size=2000`,
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrInvalidConfiguration = errors.New("invalid configuration")
|
|
|
|
ErrIncompatibleSQLMigration = errors.New("incompatible migration")
|
|
|
|
ErrNotEnoughSQLMigrations = errors.New("already more migrations than wanted")
|
|
|
|
ErrSqliteFailure = errors.New("sqlite returned an error")
|
|
|
|
)
|
|
|
|
|
|
|
|
type Sqlite struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSqlite(dbPath string) (*Sqlite, error) {
|
|
|
|
db, err := sql.Open("sqlite", dbPath)
|
|
|
|
if err != nil {
|
|
|
|
return &Sqlite{}, fmt.Errorf("%w: %v", ErrInvalidConfiguration, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &Sqlite{
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.migrate(migrations); err != nil {
|
|
|
|
return &Sqlite{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2024-09-09 07:36:05 +02:00
|
|
|
func (s *Sqlite) Update(item Item) error {
|
2024-09-08 11:17:49 +02:00
|
|
|
if _, err := s.db.Exec(`
|
|
|
|
INSERT INTO items
|
2024-09-09 07:32:05 +02:00
|
|
|
(id, kind, updated, deleted, body)
|
2024-09-08 11:17:49 +02:00
|
|
|
VALUES
|
2024-09-09 07:32:05 +02:00
|
|
|
(?, ?, ?, ?, ?)
|
2024-09-08 11:17:49 +02:00
|
|
|
ON CONFLICT(id) DO UPDATE
|
|
|
|
SET
|
|
|
|
kind=?,
|
|
|
|
updated=?,
|
2024-09-09 07:32:05 +02:00
|
|
|
deleted=?,
|
2024-09-08 11:17:49 +02:00
|
|
|
body=?`,
|
2024-09-09 07:36:05 +02:00
|
|
|
item.ID, item.Kind, item.Updated.Format(timestampFormat), item.Deleted, item.Body,
|
|
|
|
item.Kind, item.Updated.Format(timestampFormat), item.Deleted, item.Body); err != nil {
|
2024-09-08 11:17:49 +02:00
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-09 07:36:05 +02:00
|
|
|
func (s *Sqlite) Updated(t time.Time) ([]Item, error) {
|
2024-09-08 11:17:49 +02:00
|
|
|
rows, err := s.db.Query(`
|
2024-09-09 07:32:05 +02:00
|
|
|
SELECT id, kind, updated, deleted, body
|
2024-09-08 11:17:49 +02:00
|
|
|
FROM items
|
|
|
|
WHERE updated > ?`, t.Format(timestampFormat))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
|
2024-09-09 07:36:05 +02:00
|
|
|
result := make([]Item, 0)
|
2024-09-08 11:17:49 +02:00
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
2024-09-09 07:36:05 +02:00
|
|
|
var item Item
|
|
|
|
if err := rows.Scan(&item.ID, &item.Kind, &item.Updated, &item.Deleted, &item.Body); err != nil {
|
2024-09-08 11:17:49 +02:00
|
|
|
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
result = append(result, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sqlite) migrate(wanted []string) error {
|
|
|
|
// admin table
|
|
|
|
if _, err := s.db.Exec(`
|
|
|
|
CREATE TABLE IF NOT EXISTS migration
|
|
|
|
("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "query" TEXT)
|
|
|
|
`); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// find existing
|
|
|
|
rows, err := s.db.Query(`SELECT query FROM migration ORDER BY id`)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
existing := []string{}
|
|
|
|
for rows.Next() {
|
|
|
|
var query string
|
|
|
|
if err := rows.Scan(&query); err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
existing = append(existing, string(query))
|
|
|
|
}
|
|
|
|
rows.Close()
|
|
|
|
|
|
|
|
// compare
|
|
|
|
missing, err := compareMigrations(wanted, existing)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute missing
|
|
|
|
for _, query := range missing {
|
|
|
|
if _, err := s.db.Exec(string(query)); err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// register
|
|
|
|
if _, err := s.db.Exec(`
|
|
|
|
INSERT INTO migration
|
|
|
|
(query) VALUES (?)
|
|
|
|
`, query); err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareMigrations(wanted, existing []string) ([]string, error) {
|
|
|
|
needed := []string{}
|
|
|
|
if len(wanted) < len(existing) {
|
|
|
|
return []string{}, ErrNotEnoughSQLMigrations
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, want := range wanted {
|
|
|
|
switch {
|
|
|
|
case i >= len(existing):
|
|
|
|
needed = append(needed, want)
|
|
|
|
case want == existing[i]:
|
|
|
|
// do nothing
|
|
|
|
case want != existing[i]:
|
|
|
|
return []string{}, fmt.Errorf("%w: %v", ErrIncompatibleSQLMigration, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return needed, nil
|
|
|
|
}
|