feat: Add recurrer and recur_next columns to items table in Postgres

This commit is contained in:
Erik Winter (aider) 2024-12-02 07:55:49 +01:00
parent f87d979582
commit 66613ff6ea
1 changed files with 9 additions and 6 deletions

View File

@ -19,6 +19,7 @@ var migrations = []string{
`CREATE TABLE items (id TEXT PRIMARY KEY, kind TEXT, updated TIMESTAMP, deleted BOOLEAN, body TEXT)`, `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_updated ON items(updated)`,
`CREATE INDEX idx_items_kind ON items(kind)`, `CREATE INDEX idx_items_kind ON items(kind)`,
`ALTER TABLE items ADD COLUMN recurrer JSONB, ADD COLUMN recur_next TIMESTAMP`,
} }
var ( var (
@ -58,14 +59,16 @@ func NewPostgres(host, port, dbname, user, password string) (*Postgres, error) {
func (p *Postgres) Update(item item.Item) error { func (p *Postgres) Update(item item.Item) error {
_, err := p.db.Exec(` _, err := p.db.Exec(`
INSERT INTO items (id, kind, updated, deleted, body) INSERT INTO items (id, kind, updated, deleted, body, recurrer, recur_next)
VALUES ($1, $2, $3, $4, $5) VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (id) DO UPDATE ON CONFLICT (id) DO UPDATE
SET kind = EXCLUDED.kind, SET kind = EXCLUDED.kind,
updated = EXCLUDED.updated, updated = EXCLUDED.updated,
deleted = EXCLUDED.deleted, deleted = EXCLUDED.deleted,
body = EXCLUDED.body`, body = EXCLUDED.body,
item.ID, item.Kind, item.Updated, item.Deleted, item.Body) recurrer = EXCLUDED.recurrer,
recur_next = EXCLUDED.recur_next`,
item.ID, item.Kind, item.Updated, item.Deleted, item.Body, item.Recurrer, item.RecurNext)
if err != nil { if err != nil {
return fmt.Errorf("%w: %v", ErrPostgresFailure, err) return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
} }
@ -74,7 +77,7 @@ func (p *Postgres) Update(item item.Item) error {
func (p *Postgres) Updated(ks []item.Kind, t time.Time) ([]item.Item, error) { func (p *Postgres) Updated(ks []item.Kind, t time.Time) ([]item.Item, error) {
query := ` query := `
SELECT id, kind, updated, deleted, body SELECT id, kind, updated, deleted, body, recurrer, recur_next
FROM items FROM items
WHERE updated > $1` WHERE updated > $1`
args := []interface{}{t} args := []interface{}{t}
@ -97,7 +100,7 @@ func (p *Postgres) Updated(ks []item.Kind, t time.Time) ([]item.Item, error) {
result := make([]item.Item, 0) result := make([]item.Item, 0)
for rows.Next() { for rows.Next() {
var item item.Item var item item.Item
if err := rows.Scan(&item.ID, &item.Kind, &item.Updated, &item.Deleted, &item.Body); err != nil { 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) return nil, fmt.Errorf("%w: %v", ErrPostgresFailure, err)
} }
result = append(result, item) result = append(result, item)