feat: add annotations, undo system, and schema updates
Add annotations as JSON column on tasks table with Annotate/Denotate methods and CLI commands. Add undo system backed by change_log with lightweight undo_stack table (capped at 10 entries). All mutating CLI commands (add, done, delete, modify, start, stop) now record undo entries. Undo restores prior task state from change_log data. Schema changes (in v1 migration): - annotations TEXT column on tasks - undo_stack table - annotations field in change_log triggers Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -44,6 +44,12 @@ const (
|
||||
PriorityHigh Priority = 3
|
||||
)
|
||||
|
||||
// Annotation represents a timestamped note on a task
|
||||
type Annotation struct {
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
type Task struct {
|
||||
// Identity
|
||||
UUID uuid.UUID `json:"uuid"`
|
||||
@@ -69,6 +75,9 @@ type Task struct {
|
||||
RecurrenceDuration *time.Duration `json:"recurrence_duration,omitempty"`
|
||||
ParentUUID *uuid.UUID `json:"parent_uuid,omitempty"`
|
||||
|
||||
// Annotations (stored as JSON in DB)
|
||||
Annotations []Annotation `json:"annotations,omitempty"`
|
||||
|
||||
// Derived fields (not stored in DB)
|
||||
Tags []string `json:"tags"`
|
||||
Urgency float64 `json:"urgency"`
|
||||
@@ -77,24 +86,25 @@ type Task struct {
|
||||
// MarshalJSON emits Task with unix timestamps (int64) instead of RFC3339 strings.
|
||||
func (t Task) MarshalJSON() ([]byte, error) {
|
||||
type taskJSON struct {
|
||||
UUID uuid.UUID `json:"uuid"`
|
||||
ID int `json:"id"`
|
||||
Status Status `json:"status"`
|
||||
Description string `json:"description"`
|
||||
Project *string `json:"project"`
|
||||
Priority Priority `json:"priority"`
|
||||
Created int64 `json:"created"`
|
||||
Modified int64 `json:"modified"`
|
||||
Start *int64 `json:"start,omitempty"`
|
||||
End *int64 `json:"end,omitempty"`
|
||||
Due *int64 `json:"due,omitempty"`
|
||||
Scheduled *int64 `json:"scheduled,omitempty"`
|
||||
Wait *int64 `json:"wait,omitempty"`
|
||||
Until *int64 `json:"until,omitempty"`
|
||||
RecurrenceDuration *int64 `json:"recurrence_duration,omitempty"`
|
||||
ParentUUID *uuid.UUID `json:"parent_uuid,omitempty"`
|
||||
Tags []string `json:"tags"`
|
||||
Urgency float64 `json:"urgency"`
|
||||
UUID uuid.UUID `json:"uuid"`
|
||||
ID int `json:"id"`
|
||||
Status Status `json:"status"`
|
||||
Description string `json:"description"`
|
||||
Project *string `json:"project"`
|
||||
Priority Priority `json:"priority"`
|
||||
Created int64 `json:"created"`
|
||||
Modified int64 `json:"modified"`
|
||||
Start *int64 `json:"start,omitempty"`
|
||||
End *int64 `json:"end,omitempty"`
|
||||
Due *int64 `json:"due,omitempty"`
|
||||
Scheduled *int64 `json:"scheduled,omitempty"`
|
||||
Wait *int64 `json:"wait,omitempty"`
|
||||
Until *int64 `json:"until,omitempty"`
|
||||
RecurrenceDuration *int64 `json:"recurrence_duration,omitempty"`
|
||||
ParentUUID *uuid.UUID `json:"parent_uuid,omitempty"`
|
||||
Annotations []Annotation `json:"annotations,omitempty"`
|
||||
Tags []string `json:"tags"`
|
||||
Urgency float64 `json:"urgency"`
|
||||
}
|
||||
|
||||
toUnix := func(tp *time.Time) *int64 {
|
||||
@@ -128,6 +138,7 @@ func (t Task) MarshalJSON() ([]byte, error) {
|
||||
Until: toUnix(t.Until),
|
||||
RecurrenceDuration: recurDur,
|
||||
ParentUUID: t.ParentUUID,
|
||||
Annotations: t.Annotations,
|
||||
Tags: t.Tags,
|
||||
Urgency: t.Urgency,
|
||||
})
|
||||
@@ -136,24 +147,25 @@ func (t Task) MarshalJSON() ([]byte, error) {
|
||||
// UnmarshalJSON parses Task from JSON with unix timestamps (int64) and duration in seconds.
|
||||
func (t *Task) UnmarshalJSON(data []byte) error {
|
||||
type taskJSON struct {
|
||||
UUID uuid.UUID `json:"uuid"`
|
||||
ID int `json:"id"`
|
||||
Status Status `json:"status"`
|
||||
Description string `json:"description"`
|
||||
Project *string `json:"project"`
|
||||
Priority Priority `json:"priority"`
|
||||
Created int64 `json:"created"`
|
||||
Modified int64 `json:"modified"`
|
||||
Start *int64 `json:"start,omitempty"`
|
||||
End *int64 `json:"end,omitempty"`
|
||||
Due *int64 `json:"due,omitempty"`
|
||||
Scheduled *int64 `json:"scheduled,omitempty"`
|
||||
Wait *int64 `json:"wait,omitempty"`
|
||||
Until *int64 `json:"until,omitempty"`
|
||||
RecurrenceDuration *int64 `json:"recurrence_duration,omitempty"`
|
||||
ParentUUID *uuid.UUID `json:"parent_uuid,omitempty"`
|
||||
Tags []string `json:"tags"`
|
||||
Urgency float64 `json:"urgency"`
|
||||
UUID uuid.UUID `json:"uuid"`
|
||||
ID int `json:"id"`
|
||||
Status Status `json:"status"`
|
||||
Description string `json:"description"`
|
||||
Project *string `json:"project"`
|
||||
Priority Priority `json:"priority"`
|
||||
Created int64 `json:"created"`
|
||||
Modified int64 `json:"modified"`
|
||||
Start *int64 `json:"start,omitempty"`
|
||||
End *int64 `json:"end,omitempty"`
|
||||
Due *int64 `json:"due,omitempty"`
|
||||
Scheduled *int64 `json:"scheduled,omitempty"`
|
||||
Wait *int64 `json:"wait,omitempty"`
|
||||
Until *int64 `json:"until,omitempty"`
|
||||
RecurrenceDuration *int64 `json:"recurrence_duration,omitempty"`
|
||||
ParentUUID *uuid.UUID `json:"parent_uuid,omitempty"`
|
||||
Annotations []Annotation `json:"annotations,omitempty"`
|
||||
Tags []string `json:"tags"`
|
||||
Urgency float64 `json:"urgency"`
|
||||
}
|
||||
|
||||
var raw taskJSON
|
||||
@@ -184,6 +196,7 @@ func (t *Task) UnmarshalJSON(data []byte) error {
|
||||
t.Wait = fromUnix(raw.Wait)
|
||||
t.Until = fromUnix(raw.Until)
|
||||
t.ParentUUID = raw.ParentUUID
|
||||
t.Annotations = raw.Annotations
|
||||
t.Tags = raw.Tags
|
||||
t.Urgency = raw.Urgency
|
||||
|
||||
@@ -263,6 +276,32 @@ func uuidPtrToSQL(u *uuid.UUID) interface{} {
|
||||
return u.String()
|
||||
}
|
||||
|
||||
func annotationsToSQL(annotations []Annotation) interface{} {
|
||||
if len(annotations) == 0 {
|
||||
return nil
|
||||
}
|
||||
data, err := json.Marshal(annotations)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func sqlToAnnotations(v interface{}) []Annotation {
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
str, ok := v.(string)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
var annotations []Annotation
|
||||
if err := json.Unmarshal([]byte(str), &annotations); err != nil {
|
||||
return nil
|
||||
}
|
||||
return annotations
|
||||
}
|
||||
|
||||
func sqlToUUIDPtr(v interface{}) *uuid.UUID {
|
||||
if v == nil {
|
||||
return nil
|
||||
@@ -337,25 +376,26 @@ func GetTask(taskUUID uuid.UUID) (*Task, error) {
|
||||
query := `
|
||||
SELECT id, uuid, status, description, project, priority,
|
||||
created, modified, start, end, due, scheduled, wait, until_date,
|
||||
recurrence_duration, parent_uuid
|
||||
recurrence_duration, parent_uuid, annotations
|
||||
FROM tasks
|
||||
WHERE uuid = ?
|
||||
`
|
||||
|
||||
task := &Task{}
|
||||
var (
|
||||
uuidStr string
|
||||
project interface{}
|
||||
created int64
|
||||
modified int64
|
||||
start interface{}
|
||||
end interface{}
|
||||
due interface{}
|
||||
scheduled interface{}
|
||||
wait interface{}
|
||||
until interface{}
|
||||
recurDuration interface{}
|
||||
parentUUIDStr interface{}
|
||||
uuidStr string
|
||||
project interface{}
|
||||
created int64
|
||||
modified int64
|
||||
start interface{}
|
||||
end interface{}
|
||||
due interface{}
|
||||
scheduled interface{}
|
||||
wait interface{}
|
||||
until interface{}
|
||||
recurDuration interface{}
|
||||
parentUUIDStr interface{}
|
||||
annotationsStr interface{}
|
||||
)
|
||||
|
||||
err := db.QueryRow(query, taskUUID.String()).Scan(
|
||||
@@ -375,6 +415,7 @@ func GetTask(taskUUID uuid.UUID) (*Task, error) {
|
||||
&until,
|
||||
&recurDuration,
|
||||
&parentUUIDStr,
|
||||
&annotationsStr,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
@@ -401,6 +442,7 @@ func GetTask(taskUUID uuid.UUID) (*Task, error) {
|
||||
task.Until = sqlToTime(until)
|
||||
task.RecurrenceDuration = sqlToDuration(recurDuration)
|
||||
task.ParentUUID = sqlToUUIDPtr(parentUUIDStr)
|
||||
task.Annotations = sqlToAnnotations(annotationsStr)
|
||||
|
||||
// Load tags
|
||||
tags, err := task.GetTags()
|
||||
@@ -429,10 +471,10 @@ func GetTasks(filter *Filter) ([]*Task, error) {
|
||||
query := fmt.Sprintf(`
|
||||
SELECT id, uuid, status, description, project, priority,
|
||||
created, modified, start, end, due, scheduled, wait, until_date,
|
||||
recurrence_duration, parent_uuid
|
||||
recurrence_duration, parent_uuid, annotations
|
||||
FROM tasks
|
||||
WHERE %s
|
||||
ORDER BY
|
||||
ORDER BY
|
||||
CASE WHEN due IS NULL THEN 1 ELSE 0 END,
|
||||
due ASC,
|
||||
priority DESC
|
||||
@@ -449,18 +491,19 @@ func GetTasks(filter *Filter) ([]*Task, error) {
|
||||
for rows.Next() {
|
||||
task := &Task{}
|
||||
var (
|
||||
uuidStr string
|
||||
project interface{}
|
||||
created int64
|
||||
modified int64
|
||||
start interface{}
|
||||
end interface{}
|
||||
due interface{}
|
||||
scheduled interface{}
|
||||
wait interface{}
|
||||
until interface{}
|
||||
recurDuration interface{}
|
||||
parentUUIDStr interface{}
|
||||
uuidStr string
|
||||
project interface{}
|
||||
created int64
|
||||
modified int64
|
||||
start interface{}
|
||||
end interface{}
|
||||
due interface{}
|
||||
scheduled interface{}
|
||||
wait interface{}
|
||||
until interface{}
|
||||
recurDuration interface{}
|
||||
parentUUIDStr interface{}
|
||||
annotationsStr interface{}
|
||||
)
|
||||
|
||||
err := rows.Scan(
|
||||
@@ -480,6 +523,7 @@ func GetTasks(filter *Filter) ([]*Task, error) {
|
||||
&until,
|
||||
&recurDuration,
|
||||
&parentUUIDStr,
|
||||
&annotationsStr,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
@@ -506,6 +550,7 @@ func GetTasks(filter *Filter) ([]*Task, error) {
|
||||
task.Until = sqlToTime(until)
|
||||
task.RecurrenceDuration = sqlToDuration(recurDuration)
|
||||
task.ParentUUID = sqlToUUIDPtr(parentUUIDStr)
|
||||
task.Annotations = sqlToAnnotations(annotationsStr)
|
||||
|
||||
// Load tags
|
||||
tags, err := task.GetTags()
|
||||
@@ -543,7 +588,7 @@ func (t *Task) Save() error {
|
||||
status = ?, description = ?, project = ?, priority = ?,
|
||||
modified = ?, start = ?, end = ?, due = ?,
|
||||
scheduled = ?, wait = ?, until_date = ?,
|
||||
recurrence_duration = ?, parent_uuid = ?
|
||||
recurrence_duration = ?, parent_uuid = ?, annotations = ?
|
||||
WHERE uuid = ?
|
||||
`
|
||||
|
||||
@@ -561,6 +606,7 @@ func (t *Task) Save() error {
|
||||
timeToSQL(t.Until),
|
||||
durationToSQL(t.RecurrenceDuration),
|
||||
uuidPtrToSQL(t.ParentUUID),
|
||||
annotationsToSQL(t.Annotations),
|
||||
t.UUID.String(),
|
||||
)
|
||||
|
||||
@@ -573,8 +619,8 @@ func (t *Task) Save() error {
|
||||
INSERT INTO tasks (
|
||||
uuid, status, description, project, priority,
|
||||
created, modified, start, end, due, scheduled, wait, until_date,
|
||||
recurrence_duration, parent_uuid
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
recurrence_duration, parent_uuid, annotations
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`
|
||||
|
||||
result, err := db.Exec(query,
|
||||
@@ -593,6 +639,7 @@ func (t *Task) Save() error {
|
||||
timeToSQL(t.Until),
|
||||
durationToSQL(t.RecurrenceDuration),
|
||||
uuidPtrToSQL(t.ParentUUID),
|
||||
annotationsToSQL(t.Annotations),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user