feat(storage): add native PostgreSQL runtime and verified offline application import
Build and publish container images / images (push) Failing after 12m29s

This commit is contained in:
Anya Tokugawa committed 2026-11-15 05:47:14 +09:00
1 parent 96065d1e89
commit 0ba1dcfef0
243 files changed
+21441 -3090

No files matched your search

+49 -32
View File
@@ -26,6 +26,9 @@ type TaskFocus struct {
}
func (s *Store) ensureTaskFocusSchema() error {
if s.sqlBackend == storePostgreSQL {
return s.ensurePostgreSQLTaskProductivity(context.Background())
}
_, err := s.DB.Exec(`CREATE TABLE IF NOT EXISTS organizer_focus(domain_uuid TEXT NOT NULL,owner_uuid TEXT NOT NULL,uuid TEXT PRIMARY KEY,task_uuid TEXT NOT NULL,phase TEXT NOT NULL,state TEXT NOT NULL,duration_ms INTEGER NOT NULL,remaining_ms INTEGER NOT NULL,deadline_ms INTEGER NOT NULL,created_at_ms INTEGER NOT NULL,updated_at_ms INTEGER NOT NULL,version INTEGER NOT NULL);
CREATE UNIQUE INDEX IF NOT EXISTS organizer_focus_active ON organizer_focus(owner_uuid) WHERE state IN ('running','paused');
CREATE INDEX IF NOT EXISTS organizer_focus_due ON organizer_focus(state,deadline_ms,uuid);
@@ -45,13 +48,14 @@ func scanFocus(row interface{ Scan(...any) error }) (TaskFocus, error) {
return f, err
}
func (s *Store) LatestTaskFocus(ctx context.Context, domain, actor string) (*TaskFocus, error) {
if err := taskActor(ctx, s.DB, domain, actor, false); err != nil {
func (s *Store) LatestTaskFocus(ctx context.Context, domain, actor string) (result0 *TaskFocus, resultErr error) {
defer func() { resultErr = s.sqlError("organizer repository", resultErr) }()
if err := s.taskActor(ctx, s.DB, domain, actor, false); err != nil {
return nil, err
}
f, err := scanFocus(s.DB.QueryRowContext(ctx, `SELECT `+focusColumns+` FROM organizer_focus WHERE domain_uuid=? AND owner_uuid=? AND state IN ('running','paused')`, domain, actor))
f, err := scanFocus(s.DB.QueryRowContext(ctx, `SELECT `+focusColumns+` FROM organizer_focus WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2)+` AND state IN ('running','paused')`, domain, actor))
if errors.Is(err, sql.ErrNoRows) {
f, err = scanFocus(s.DB.QueryRowContext(ctx, `SELECT `+focusColumns+` FROM organizer_focus WHERE domain_uuid=? AND owner_uuid=? ORDER BY created_at_ms DESC,uuid DESC LIMIT 1`, domain, actor))
f, err = scanFocus(s.DB.QueryRowContext(ctx, `SELECT `+focusColumns+` FROM organizer_focus WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2)+` ORDER BY created_at_ms DESC,uuid DESC LIMIT 1`, domain, actor))
}
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
@@ -62,22 +66,25 @@ func (s *Store) LatestTaskFocus(ctx context.Context, domain, actor string) (*Tas
return &f, nil
}
func recordFocusTx(ctx context.Context, tx *sql.Tx, f TaskFocus, action string) error {
if err := recordFocusSpanTx(ctx, tx, f); err != nil {
func (s *Store) recordFocusTx(ctx context.Context, tx *sql.Tx, f TaskFocus, action string) error {
if err := s.recordFocusSpanTx(ctx, tx, f); err != nil {
return err
}
data, err := json.Marshal(f)
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, `INSERT INTO organizer_focus_changes(session_uuid,version,action,created_at_ms,data) VALUES(?,?,?,?,?)`, f.UUID, f.Version, action, f.UpdatedMS, string(data))
_, err = tx.ExecContext(ctx, `INSERT INTO organizer_focus_changes(session_uuid,version,action,created_at_ms,data) VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,`+s.sqlParam(5)+`)`, f.UUID, f.Version, action, f.UpdatedMS, string(data))
return err
}
// ChangeTaskFocus serializes transitions, including cross-Domain starts, under
// SQLite's writer lock. The partial unique index is a second line of defense.
// the backend's writer lock. The partial unique index is a second line of defense.
// The supplied time is server-controlled (also allows deterministic tests).
func (s *Store) ChangeTaskFocus(ctx context.Context, domain, actor, action string, f TaskFocus, expected int64, now time.Time) (TaskFocus, error) {
func (s *Store) ChangeTaskFocus(ctx context.Context, domain, actor, action string, f TaskFocus, expected int64, now time.Time) (result0 TaskFocus, resultErr error) {
defer func() { resultErr = s.sqlError("organizer repository", resultErr) }()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
if _, err := uuid.Parse(f.UUID); err != nil {
return f, ErrTaskInvalid
}
@@ -86,20 +93,23 @@ func (s *Store) ChangeTaskFocus(ctx context.Context, domain, actor, action strin
return f, err
}
defer tx.Rollback()
if _, err = tx.ExecContext(ctx, `UPDATE domain_memberships SET state=state WHERE domain_uuid=? AND principal_uuid=?`, domain, actor); err != nil {
if err = s.lockTaskWriter(ctx, tx); err != nil {
return f, err
}
if err = taskActor(ctx, tx, domain, actor, true); err != nil {
if _, err = tx.ExecContext(ctx, `UPDATE domain_memberships SET state=state WHERE domain_uuid=`+s.sqlParam(1)+` AND principal_uuid=`+s.sqlParam(2)+``, domain, actor); err != nil {
return f, err
}
enabled, err := taskReminderExtensionEnabled(ctx, tx, domain)
if err = s.taskActor(ctx, tx, domain, actor, true); err != nil {
return f, err
}
enabled, err := s.taskReminderExtensionEnabled(ctx, tx, domain)
if err != nil {
return f, err
}
if !enabled {
return f, ErrAccessDenied
}
old, err := scanFocus(tx.QueryRowContext(ctx, `SELECT `+focusColumns+` FROM organizer_focus WHERE domain_uuid=? AND owner_uuid=? AND uuid=?`, domain, actor, f.UUID))
old, err := scanFocus(tx.QueryRowContext(ctx, `SELECT `+focusColumns+` FROM organizer_focus WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2)+` AND uuid=`+s.sqlParam(3)+``, domain, actor, f.UUID))
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return f, err
}
@@ -116,7 +126,7 @@ func (s *Store) ChangeTaskFocus(ctx context.Context, domain, actor, action strin
}
if f.TaskUUID != "" {
var exists bool
if err = tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM organizer_items WHERE domain_uuid=? AND owner_uuid=? AND uuid=? AND kind='task' AND archived=0 AND status NOT IN ('done','cancelled'))`, domain, actor, f.TaskUUID).Scan(&exists); err != nil {
if err = tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM organizer_items WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2)+` AND uuid=`+s.sqlParam(3)+` AND kind='task' AND archived=0 AND status NOT IN ('done','cancelled'))`, domain, actor, f.TaskUUID).Scan(&exists); err != nil {
return f, err
}
if !exists {
@@ -124,16 +134,16 @@ func (s *Store) ChangeTaskFocus(ctx context.Context, domain, actor, action strin
}
}
var otherDomain, otherID string
err = tx.QueryRowContext(ctx, `SELECT domain_uuid,uuid FROM organizer_focus WHERE owner_uuid=? AND state IN ('running','paused')`, actor).Scan(&otherDomain, &otherID)
err = tx.QueryRowContext(ctx, `SELECT domain_uuid,uuid FROM organizer_focus WHERE owner_uuid=`+s.sqlParam(1)+` AND state IN ('running','paused')`, actor).Scan(&otherDomain, &otherID)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return f, err
}
if err == nil {
allowed := taskActor(ctx, tx, otherDomain, actor, false)
allowed := s.taskActor(ctx, tx, otherDomain, actor, false)
if allowed != nil && !errors.Is(allowed, ErrAccessDenied) {
return f, allowed
}
on, e := taskReminderExtensionEnabled(ctx, tx, otherDomain)
on, e := s.taskReminderExtensionEnabled(ctx, tx, otherDomain)
if e != nil {
return f, e
}
@@ -142,7 +152,7 @@ func (s *Store) ChangeTaskFocus(ctx context.Context, domain, actor, action strin
}
// A timer stranded in a revoked/disabled Domain must not lock the
// user out forever. Release only their timer, never another task.
stale, e := scanFocus(tx.QueryRowContext(ctx, `SELECT `+focusColumns+` FROM organizer_focus WHERE uuid=?`, otherID))
stale, e := scanFocus(tx.QueryRowContext(ctx, `SELECT `+focusColumns+` FROM organizer_focus WHERE uuid=`+s.sqlParam(1)+``, otherID))
if e != nil {
return f, e
}
@@ -153,10 +163,10 @@ func (s *Store) ChangeTaskFocus(ctx context.Context, domain, actor, action strin
stale.DeadlineMS = 0
stale.UpdatedMS = ms
stale.Version++
if _, e = tx.ExecContext(ctx, `UPDATE organizer_focus SET state='cancelled',deadline_ms=0,remaining_ms=?,updated_at_ms=?,version=? WHERE uuid=?`, stale.RemainingMS, ms, stale.Version, stale.UUID); e != nil {
if _, e = tx.ExecContext(ctx, `UPDATE organizer_focus SET state='cancelled',deadline_ms=0,remaining_ms=`+s.sqlParam(1)+`,updated_at_ms=`+s.sqlParam(2)+`,version=`+s.sqlParam(3)+` WHERE uuid=`+s.sqlParam(4)+``, stale.RemainingMS, ms, stale.Version, stale.UUID); e != nil {
return f, e
}
if e = recordFocusTx(ctx, tx, stale, "revoked"); e != nil {
if e = s.recordFocusTx(ctx, tx, stale, "revoked"); e != nil {
return f, e
}
}
@@ -166,7 +176,7 @@ func (s *Store) ChangeTaskFocus(ctx context.Context, domain, actor, action strin
f.CreatedMS = ms
f.UpdatedMS = ms
f.Version = 1
res, e := tx.ExecContext(ctx, `INSERT OR IGNORE INTO organizer_focus(domain_uuid,owner_uuid,uuid,task_uuid,phase,state,duration_ms,remaining_ms,deadline_ms,created_at_ms,updated_at_ms,version) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)`, domain, actor, f.UUID, f.TaskUUID, f.Phase, f.State, f.DurationMS, f.RemainingMS, f.DeadlineMS, f.CreatedMS, f.UpdatedMS, f.Version)
res, e := tx.ExecContext(ctx, `INSERT INTO organizer_focus(domain_uuid,owner_uuid,uuid,task_uuid,phase,state,duration_ms,remaining_ms,deadline_ms,created_at_ms,updated_at_ms,version) VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,`+s.sqlParam(5)+`,`+s.sqlParam(6)+`,`+s.sqlParam(7)+`,`+s.sqlParam(8)+`,`+s.sqlParam(9)+`,`+s.sqlParam(10)+`,`+s.sqlParam(11)+`,`+s.sqlParam(12)+`) ON CONFLICT DO NOTHING`, domain, actor, f.UUID, f.TaskUUID, f.Phase, f.State, f.DurationMS, f.RemainingMS, f.DeadlineMS, f.CreatedMS, f.UpdatedMS, f.Version)
if e != nil {
return f, e
}
@@ -211,11 +221,11 @@ func (s *Store) ChangeTaskFocus(ctx context.Context, domain, actor, action strin
}
f.Version++
f.UpdatedMS = ms
if _, err = tx.ExecContext(ctx, `UPDATE organizer_focus SET state=?,remaining_ms=?,deadline_ms=?,updated_at_ms=?,version=? WHERE uuid=?`, f.State, f.RemainingMS, f.DeadlineMS, f.UpdatedMS, f.Version, f.UUID); err != nil {
if _, err = tx.ExecContext(ctx, `UPDATE organizer_focus SET state=`+s.sqlParam(1)+`,remaining_ms=`+s.sqlParam(2)+`,deadline_ms=`+s.sqlParam(3)+`,updated_at_ms=`+s.sqlParam(4)+`,version=`+s.sqlParam(5)+` WHERE uuid=`+s.sqlParam(6)+``, f.State, f.RemainingMS, f.DeadlineMS, f.UpdatedMS, f.Version, f.UUID); err != nil {
return f, err
}
}
if err = recordFocusTx(ctx, tx, f, action); err != nil {
if err = s.recordFocusTx(ctx, tx, f, action); err != nil {
return f, err
}
return f, tx.Commit()
@@ -223,7 +233,8 @@ func (s *Store) ChangeTaskFocus(ctx context.Context, domain, actor, action strin
// A bounded, indexed sweep completes timers even while all browsers are closed.
// Completing a timer never changes a task's status, due date or planned date.
func (s *Store) CompleteTaskFocus(ctx context.Context, now time.Time, limit int) (int, error) {
func (s *Store) CompleteTaskFocus(ctx context.Context, now time.Time, limit int) (result0 int, resultErr error) {
defer func() { resultErr = s.sqlError("organizer repository", resultErr) }()
if limit < 1 || limit > 100 {
limit = 100
}
@@ -234,10 +245,13 @@ func (s *Store) CompleteTaskFocus(ctx context.Context, now time.Time, limit int)
return 0, err
}
defer tx.Rollback()
if _, err = tx.ExecContext(ctx, `UPDATE organizer_focus SET state=state WHERE rowid=-1`); err != nil {
if err = s.lockTaskWriter(ctx, tx); err != nil {
return 0, err
}
rows, err := tx.QueryContext(ctx, `SELECT domain_uuid,owner_uuid,uuid FROM organizer_focus WHERE state='running' AND deadline_ms<=? ORDER BY deadline_ms,uuid LIMIT ?`, now.UnixMilli(), limit)
if _, err = tx.ExecContext(ctx, `UPDATE organizer_focus SET state=state WHERE uuid=''`); err != nil {
return 0, err
}
rows, err := tx.QueryContext(ctx, `SELECT domain_uuid,owner_uuid,uuid FROM organizer_focus WHERE state='running' AND deadline_ms<=`+s.sqlParam(1)+` ORDER BY deadline_ms,uuid LIMIT `+s.sqlParam(2)+``, now.UnixMilli(), limit)
if err != nil {
return 0, err
}
@@ -257,15 +271,15 @@ func (s *Store) CompleteTaskFocus(ctx context.Context, now time.Time, limit int)
return 0, err
}
for _, e := range items {
f, err := scanFocus(tx.QueryRowContext(ctx, `SELECT `+focusColumns+` FROM organizer_focus WHERE uuid=?`, e.id))
f, err := scanFocus(tx.QueryRowContext(ctx, `SELECT `+focusColumns+` FROM organizer_focus WHERE uuid=`+s.sqlParam(1)+``, e.id))
if err != nil {
return 0, err
}
enabled, err := taskReminderExtensionEnabled(ctx, tx, e.domain)
enabled, err := s.taskReminderExtensionEnabled(ctx, tx, e.domain)
if err != nil {
return 0, err
}
auth := taskActor(ctx, tx, e.domain, e.owner, false)
auth := s.taskActor(ctx, tx, e.domain, e.owner, false)
if auth != nil && !errors.Is(auth, ErrAccessDenied) {
return 0, auth
}
@@ -287,10 +301,10 @@ func (s *Store) CompleteTaskFocus(ctx context.Context, now time.Time, limit int)
f.DeadlineMS = 0
f.UpdatedMS = now.UnixMilli()
f.Version++
if _, err = tx.ExecContext(ctx, `UPDATE organizer_focus SET state=?,remaining_ms=0,deadline_ms=0,updated_at_ms=?,version=? WHERE uuid=?`, f.State, f.UpdatedMS, f.Version, f.UUID); err != nil {
if _, err = tx.ExecContext(ctx, `UPDATE organizer_focus SET state=`+s.sqlParam(1)+`,remaining_ms=0,deadline_ms=0,updated_at_ms=`+s.sqlParam(2)+`,version=`+s.sqlParam(3)+` WHERE uuid=`+s.sqlParam(4)+``, f.State, f.UpdatedMS, f.Version, f.UUID); err != nil {
return 0, err
}
if err = recordFocusTx(ctx, tx, f, action); err != nil {
if err = s.recordFocusTx(ctx, tx, f, action); err != nil {
return 0, err
}
}
@@ -301,7 +315,10 @@ func (s *Store) CompleteTaskFocus(ctx context.Context, now time.Time, limit int)
}
func (s *Store) EnsureTaskFocusEvent() error {
if s.sqlBackend == storePostgreSQL {
return s.ensureNativeBuiltinEvent("builtin.task.focus", "Focus timer completion", TaskFocusEventType, 60, -2, 3)
}
now := time.Now().UnixMilli()
_, err := s.DB.Exec(`INSERT OR IGNORE INTO event_definitions(uuid,name,event_type,event_key,payload,schedule_kind,interval_seconds,next_run_at_ms,enabled,priority,max_attempts,created_at_ms,updated_at_ms) VALUES('builtin.task.focus','Focus timer completion',?,'local','{}','interval',60,?,1,-2,3,?,?)`, TaskFocusEventType, now, now, now)
_, err := s.DB.Exec(`INSERT OR IGNORE INTO event_definitions(uuid,name,event_type,event_key,payload,schedule_kind,interval_seconds,next_run_at_ms,enabled,priority,max_attempts,created_at_ms,updated_at_ms) VALUES('builtin.task.focus','Focus timer completion',`+s.sqlParam(1)+`,'local','{}','interval',60,`+s.sqlParam(2)+`,1,-2,3,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`)`, TaskFocusEventType, now, now, now)
return err
}