2024-09-18 18:04:27 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2024-12-20 15:31:57 +01:00
|
|
|
"encoding/json"
|
2024-09-18 18:04:27 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
_ "github.com/lib/pq"
|
2024-09-20 07:09:30 +02:00
|
|
|
"go-mod.ewintr.nl/planner/item"
|
2024-09-18 18:04:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
timestampFormat = "2006-01-02 15:04:05"
|
|
|
|
)
|
|
|
|
|
|
|
|
var migrations = []string{
|
|
|
|
`CREATE TABLE items (id TEXT PRIMARY KEY, kind TEXT, updated TIMESTAMP, deleted BOOLEAN, body TEXT)`,
|
|
|
|
`CREATE INDEX idx_items_updated ON items(updated)`,
|
|
|
|
`CREATE INDEX idx_items_kind ON items(kind)`,
|
2024-12-01 10:22:47 +01:00
|
|
|
`ALTER TABLE items ADD COLUMN recurrer JSONB, ADD COLUMN recur_next TIMESTAMP`,
|
2024-09-18 18:04:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrInvalidConfiguration = errors.New("invalid configuration")
|
|
|
|
ErrIncompatibleSQLMigration = errors.New("incompatible migration")
|
|
|
|
ErrNotEnoughSQLMigrations = errors.New("already more migrations than wanted")
|
|
|
|
ErrPostgresFailure = errors.New("postgres returned an error")
|
|
|
|
)
|
|
|
|
|
|
|
|
type Postgres struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPostgres(host, port, dbname, user, password string) (*Postgres, error) {
|
|
|
|
connStr := fmt.Sprintf("host=%s port=%s dbname=%s user=%s password=%s sslmode=disable", host, port, dbname, user, password)
|
|
|
|
|
|
|
|
db, err := sql.Open("postgres", connStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: %v", ErrInvalidConfiguration, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the connection
|
|
|
|
if err := db.Ping(); err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: %v", ErrInvalidConfiguration, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := &Postgres{
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.migrate(migrations); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2024-12-20 15:37:52 +01:00
|
|
|
func (p *Postgres) Update(i item.Item, ts time.Time) error {
|
2024-12-21 14:04:28 +01:00
|
|
|
var recurrerJSON any
|
2024-12-20 15:37:52 +01:00
|
|
|
if i.Recurrer != nil {
|
2024-12-21 14:03:10 +01:00
|
|
|
var err error
|
2024-12-20 15:37:52 +01:00
|
|
|
recurrerJSON, err = json.Marshal(i.Recurrer)
|
2024-12-20 15:31:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
|
|
|
}
|
2024-12-21 14:03:10 +01:00
|
|
|
} else {
|
|
|
|
recurrerJSON = nil
|
2024-12-20 15:31:57 +01:00
|
|
|
}
|
|
|
|
|
2024-12-21 14:04:28 +01:00
|
|
|
_, err := p.db.Exec(`
|
2024-12-01 10:22:47 +01:00
|
|
|
INSERT INTO items (id, kind, updated, deleted, body, recurrer, recur_next)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
2024-09-18 18:04:27 +02:00
|
|
|
ON CONFLICT (id) DO UPDATE
|
|
|
|
SET kind = EXCLUDED.kind,
|
|
|
|
updated = EXCLUDED.updated,
|
|
|
|
deleted = EXCLUDED.deleted,
|
2024-12-01 10:22:47 +01:00
|
|
|
body = EXCLUDED.body,
|
|
|
|
recurrer = EXCLUDED.recurrer,
|
|
|
|
recur_next = EXCLUDED.recur_next`,
|
2024-12-20 15:37:52 +01:00
|
|
|
i.ID, i.Kind, ts, i.Deleted, i.Body, recurrerJSON, i.RecurNext)
|
2024-09-18 18:04:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Postgres) Updated(ks []item.Kind, t time.Time) ([]item.Item, error) {
|
|
|
|
query := `
|
2024-12-01 10:22:47 +01:00
|
|
|
SELECT id, kind, updated, deleted, body, recurrer, recur_next
|
2024-09-18 18:04:27 +02:00
|
|
|
FROM items
|
|
|
|
WHERE updated > $1`
|
|
|
|
args := []interface{}{t}
|
|
|
|
|
|
|
|
if len(ks) > 0 {
|
|
|
|
placeholder := make([]string, len(ks))
|
|
|
|
for i := range ks {
|
|
|
|
placeholder[i] = fmt.Sprintf("$%d", i+2)
|
|
|
|
args = append(args, string(ks[i]))
|
|
|
|
}
|
|
|
|
query += fmt.Sprintf(" AND kind = ANY(ARRAY[%s])", strings.Join(placeholder, ","))
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := p.db.Query(query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
result := make([]item.Item, 0)
|
|
|
|
for rows.Next() {
|
2024-12-20 15:37:52 +01:00
|
|
|
var i item.Item
|
2024-12-01 10:22:47 +01:00
|
|
|
var recurNext sql.NullTime
|
2024-12-21 14:05:04 +01:00
|
|
|
var recurrerJSON sql.NullString
|
2024-12-20 15:37:52 +01:00
|
|
|
if err := rows.Scan(&i.ID, &i.Kind, &i.Updated, &i.Deleted, &i.Body, &recurrerJSON, &recurNext); err != nil {
|
2024-09-18 18:04:27 +02:00
|
|
|
return nil, fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
|
|
|
}
|
2024-12-21 14:05:04 +01:00
|
|
|
if recurrerJSON.Valid && recurrerJSON.String != "" {
|
2024-12-20 15:32:47 +01:00
|
|
|
var recurrer item.Recur
|
2024-12-21 14:05:04 +01:00
|
|
|
if err := json.Unmarshal([]byte(recurrerJSON.String), &recurrer); err != nil {
|
2024-12-20 15:32:47 +01:00
|
|
|
return nil, fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
|
|
|
}
|
2024-12-20 15:37:52 +01:00
|
|
|
i.Recurrer = &recurrer
|
2024-12-20 15:32:47 +01:00
|
|
|
}
|
2024-12-01 10:22:47 +01:00
|
|
|
if recurNext.Valid {
|
2024-12-20 15:37:52 +01:00
|
|
|
i.RecurNext = recurNext.Time
|
2024-12-01 10:22:47 +01:00
|
|
|
}
|
2024-12-20 15:37:52 +01:00
|
|
|
result = append(result, i)
|
2024-12-01 10:22:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Postgres) RecursBefore(date time.Time) ([]item.Item, error) {
|
|
|
|
query := `
|
|
|
|
SELECT id, kind, updated, deleted, body, recurrer, recur_next
|
|
|
|
FROM items
|
|
|
|
WHERE recur_next <= $1 AND recurrer IS NOT NULL`
|
|
|
|
|
|
|
|
rows, err := p.db.Query(query, date)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
result := make([]item.Item, 0)
|
|
|
|
for rows.Next() {
|
2024-12-20 15:37:52 +01:00
|
|
|
var i item.Item
|
2024-12-01 10:22:47 +01:00
|
|
|
var recurNext sql.NullTime
|
2024-12-21 14:05:04 +01:00
|
|
|
var recurrerJSON sql.NullString
|
2024-12-20 15:37:52 +01:00
|
|
|
if err := rows.Scan(&i.ID, &i.Kind, &i.Updated, &i.Deleted, &i.Body, &recurrerJSON, &recurNext); err != nil {
|
2024-12-01 10:22:47 +01:00
|
|
|
return nil, fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
|
|
|
}
|
2024-12-21 14:05:04 +01:00
|
|
|
if recurrerJSON.Valid && recurrerJSON.String != "" {
|
2024-12-20 15:32:47 +01:00
|
|
|
var recurrer item.Recur
|
|
|
|
if err := json.Unmarshal(recurrerJSON, &recurrer); err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
|
|
|
}
|
2024-12-20 15:37:52 +01:00
|
|
|
i.Recurrer = &recurrer
|
2024-12-20 15:32:47 +01:00
|
|
|
}
|
2024-12-01 10:22:47 +01:00
|
|
|
if recurNext.Valid {
|
2024-12-20 15:37:52 +01:00
|
|
|
i.RecurNext = recurNext.Time
|
2024-12-01 10:22:47 +01:00
|
|
|
}
|
2024-12-20 15:37:52 +01:00
|
|
|
result = append(result, i)
|
2024-09-18 18:04:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2024-12-01 10:22:47 +01:00
|
|
|
func (p *Postgres) RecursNext(id string, date time.Time, ts time.Time) error {
|
|
|
|
var recurrer *item.Recur
|
|
|
|
err := p.db.QueryRow(`
|
|
|
|
SELECT recurrer
|
|
|
|
FROM items
|
|
|
|
WHERE id = $1`, id).Scan(&recurrer)
|
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return ErrNotFound
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if recurrer == nil {
|
|
|
|
return ErrNotARecurrer
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the new date is actually a valid recurrence
|
|
|
|
if !recurrer.On(date) {
|
|
|
|
return fmt.Errorf("%w: date %v is not a valid recurrence", ErrPostgresFailure, date)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = p.db.Exec(`
|
|
|
|
UPDATE items
|
|
|
|
SET recur_next = $1,
|
|
|
|
updated = $2
|
|
|
|
WHERE id = $3`, date, ts, id)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-18 18:04:27 +02:00
|
|
|
func (p *Postgres) migrate(wanted []string) error {
|
|
|
|
// Create migration table if not exists
|
|
|
|
_, err := p.db.Exec(`
|
|
|
|
CREATE TABLE IF NOT EXISTS migration
|
|
|
|
(id SERIAL PRIMARY KEY, query TEXT)
|
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find existing migrations
|
|
|
|
rows, err := p.db.Query(`SELECT query FROM migration ORDER BY id`)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var existing []string
|
|
|
|
for rows.Next() {
|
|
|
|
var query string
|
|
|
|
if err := rows.Scan(&query); err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
|
|
|
}
|
|
|
|
existing = append(existing, query)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare and execute missing migrations
|
|
|
|
missing, err := compareMigrations(wanted, existing)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, query := range missing {
|
|
|
|
if _, err := p.db.Exec(query); err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register migration
|
|
|
|
if _, err := p.db.Exec(`
|
|
|
|
INSERT INTO migration (query) VALUES ($1)
|
|
|
|
`, query); err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareMigrations(wanted, existing []string) ([]string, error) {
|
|
|
|
var needed []string
|
|
|
|
if len(wanted) < len(existing) {
|
|
|
|
return nil, 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 nil, fmt.Errorf("%w: %v", ErrIncompatibleSQLMigration, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return needed, nil
|
|
|
|
}
|