Compare commits
No commits in common. "45125bd02d85cd550186722dd15439781149b127" and "f87d979582e351299f808037f59532fb4f681e62" have entirely different histories.
45125bd02d
...
f87d979582
|
@ -6,7 +6,6 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
"go-mod.ewintr.nl/planner/item"
|
||||
)
|
||||
|
||||
|
@ -51,10 +50,14 @@ func TestMemoryUpdate(t *testing.T) {
|
|||
if actErr != nil {
|
||||
t.Errorf("exp nil, got %v", actErr)
|
||||
}
|
||||
if diff := cmp.Diff([]item.Item{t1, t2}, actItems, cmpopts.SortSlices(func(i, j item.Item) bool {
|
||||
return i.ID < j.ID
|
||||
})); diff != "" {
|
||||
t.Errorf("(exp +, got -)\n%s", diff)
|
||||
if len(actItems) != 2 {
|
||||
t.Errorf("exp 2, gor %d", len(actItems))
|
||||
}
|
||||
if actItems[0].ID != t1.ID {
|
||||
t.Errorf("exp %v, got %v", actItems[0].ID, t1.ID)
|
||||
}
|
||||
if actItems[1].ID != t2.ID {
|
||||
t.Errorf("exp %v, got %v", actItems[1].ID, t2.ID)
|
||||
}
|
||||
|
||||
actItems, actErr = mem.Updated([]item.Kind{}, before)
|
||||
|
|
|
@ -19,7 +19,6 @@ 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)`,
|
||||
`ALTER TABLE items ADD COLUMN recurrer JSONB, ADD COLUMN recur_next TIMESTAMP`,
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -59,16 +58,14 @@ func NewPostgres(host, port, dbname, user, password string) (*Postgres, error) {
|
|||
|
||||
func (p *Postgres) Update(item item.Item) error {
|
||||
_, err := p.db.Exec(`
|
||||
INSERT INTO items (id, kind, updated, deleted, body, recurrer, recur_next)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
INSERT INTO items (id, kind, updated, deleted, body)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT (id) DO UPDATE
|
||||
SET kind = EXCLUDED.kind,
|
||||
updated = EXCLUDED.updated,
|
||||
deleted = EXCLUDED.deleted,
|
||||
body = EXCLUDED.body,
|
||||
recurrer = EXCLUDED.recurrer,
|
||||
recur_next = EXCLUDED.recur_next`,
|
||||
item.ID, item.Kind, item.Updated, item.Deleted, item.Body, item.Recurrer, item.RecurNext)
|
||||
body = EXCLUDED.body`,
|
||||
item.ID, item.Kind, item.Updated, item.Deleted, item.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
||||
}
|
||||
|
@ -77,7 +74,7 @@ func (p *Postgres) Update(item item.Item) error {
|
|||
|
||||
func (p *Postgres) Updated(ks []item.Kind, t time.Time) ([]item.Item, error) {
|
||||
query := `
|
||||
SELECT id, kind, updated, deleted, body, recurrer, recur_next
|
||||
SELECT id, kind, updated, deleted, body
|
||||
FROM items
|
||||
WHERE updated > $1`
|
||||
args := []interface{}{t}
|
||||
|
@ -100,7 +97,7 @@ func (p *Postgres) Updated(ks []item.Kind, t time.Time) ([]item.Item, error) {
|
|||
result := make([]item.Item, 0)
|
||||
for rows.Next() {
|
||||
var item item.Item
|
||||
if err := rows.Scan(&item.ID, &item.Kind, &item.Updated, &item.Deleted, &item.Body, &item.Recurrer, &item.RecurNext); err != nil {
|
||||
if err := rows.Scan(&item.ID, &item.Kind, &item.Updated, &item.Deleted, &item.Body); err != nil {
|
||||
return nil, fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
||||
}
|
||||
result = append(result, item)
|
||||
|
@ -109,63 +106,6 @@ func (p *Postgres) Updated(ks []item.Kind, t time.Time) ([]item.Item, error) {
|
|||
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() {
|
||||
var item item.Item
|
||||
if err := rows.Scan(&item.ID, &item.Kind, &item.Updated, &item.Deleted, &item.Body, &item.Recurrer, &item.RecurNext); err != nil {
|
||||
return nil, fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
||||
}
|
||||
result = append(result, item)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (p *Postgres) RecursNext(id string, date 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
|
||||
WHERE id = $2`, date, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Postgres) migrate(wanted []string) error {
|
||||
// Create migration table if not exists
|
||||
_, err := p.db.Exec(`
|
||||
|
|
Loading…
Reference in New Issue