Documentation
¶
Index ¶
- func ApplyEvent(tx *sql.Tx, event Event, validator EntityValidator) (bool, error)
- func BackfillOrphanEntities(tx *sql.Tx, sessionID string) (int, error)
- func BackfillStaleIssues(tx *sql.Tx, sessionID string) (int, error)
- func InitServerEventLog(db *sql.DB) error
- func MarkEventsSynced(tx *sql.Tx, acks []Ack) error
- type Ack
- type ApplyResult
- type ConflictRecord
- type EntityValidator
- type Event
- type FailedEvent
- type PullResult
- type PushResult
- type Rejection
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyEvent ¶
ApplyEvent applies a single sync event to the database within the given transaction. The validator is called to check that the entity type is allowed before any SQL is executed. Returns true if an existing row was overwritten (create/update only).
func BackfillOrphanEntities ¶
BackfillOrphanEntities scans all syncable tables for rows that have no corresponding action_log entry and inserts synthetic "create" events so they get picked up by the normal push pipeline.
Only runs when the client has never pulled from the server (last_pulled_server_seq == 0). After the first pull, entities in the DB may have come from the server, and backfilling those would create duplicate events.
Returns the total number of entities backfilled.
func BackfillStaleIssues ¶
BackfillStaleIssues inserts synthetic update events for issues whose updated_at timestamp is newer than the latest action_log entry. This helps sync older data where status changes weren't logged.
Only runs when the client has never pulled from the server.
func InitServerEventLog ¶
InitServerEventLog creates the events table and index if they don't exist.
Types ¶
type ApplyResult ¶
type ApplyResult struct {
LastAppliedSeq int64
Applied int
Overwrites int
Conflicts []ConflictRecord
Failed []FailedEvent
}
ApplyResult summarises the outcome of applying a batch of events.
func ApplyRemoteEvents ¶
func ApplyRemoteEvents(tx *sql.Tx, events []Event, myDeviceID string, validator EntityValidator, lastSyncAt *time.Time) (ApplyResult, error)
ApplyRemoteEvents applies a batch of remote events to the local database. Events with invalid entity types are logged and added to the Failed list. lastSyncAt gates conflict detection: overwrites are only flagged as conflicts when the local row was modified after lastSyncAt. Pass nil to skip conflict recording.
type ConflictRecord ¶
type ConflictRecord struct {
EntityType string
EntityID string
ServerSeq int64
LocalData json.RawMessage
RemoteData json.RawMessage
OverwrittenAt time.Time
}
ConflictRecord captures the details of a local row overwritten by a remote event.
type EntityValidator ¶
EntityValidator returns true if the given entity type is allowed.
type Event ¶
type Event struct {
ClientActionID int64
DeviceID string
SessionID string
ActionType string
EntityType string
EntityID string
Payload []byte // JSON
ClientTimestamp time.Time
ServerSeq int64
}
Event represents a single sync action from a device.
func GetPendingEvents ¶
GetPendingEvents reads unsynced, non-undone action_log rows and returns them as Events. It uses rowid for ordering and as ClientActionID. Before querying, it backfills synthetic "create" entries for any entities that exist in syncable tables but have no action_log row (e.g. pre-existing data).
func GetPendingEventsPreserveSession ¶ added in v0.45.0
GetPendingEventsPreserveSession reads unsynced, non-undone action_log rows and returns them as Events whose SessionID is the value stored on each row (NOT a caller-supplied session). Use this from the td-sync post-commit promotion path so per-actor session_ids stamped by the REST middleware (`twu_*` / `twa_*_as_*`) propagate end-to-end into events.db.
Behavioural differences vs GetPendingEvents:
- Per-row session_id is taken from action_log.session_id rather than overwritten with the caller's session.
- DeviceID is the constant supplied by the caller (typically api.TdWatchServerDeviceID = "td_watch_server" — see plan §10 Q2).
- No backfill (BackfillOrphanEntities / BackfillStaleIssues): promotion is for fresh writes from the REST handler that just committed. The backfill helpers exist to repair pre-existing data on the local CLI before the first push and would race / duplicate when run repeatedly on the server side.
Other field semantics (action_type mapping, entity_type normalization, {schema_version, new_data, previous_data} payload envelope, timestamp parsing, ClientActionID = rowid) match GetPendingEvents exactly so downstream consumers (InsertServerEvents, ApplyRemoteEvents) can't tell the two paths apart.
type FailedEvent ¶
FailedEvent records a single event that could not be applied.
type PullResult ¶
PullResult is the server response to a pull request.
func GetEventsSince ¶
func GetEventsSince(tx *sql.Tx, afterSeq int64, limit int, excludeDevice string) (PullResult, error)
GetEventsSince retrieves events after the given sequence number. If excludeDevice is non-empty, events from that device are filtered out.
type PushResult ¶
PushResult is the server response to a push request.
func InsertServerEvents ¶
func InsertServerEvents(tx *sql.Tx, events []Event) (PushResult, error)
InsertServerEvents inserts events into the server event log within the given transaction. Duplicates (by device_id, session_id, client_action_id) are rejected, not errored.
func InsertServerEventsAttached ¶ added in v0.45.0
InsertServerEventsAttached behaves like InsertServerEvents but targets the `events` table in the named schema. Pass "main" for the conventional single-DB case; pass an attached schema name (e.g. "events_db") to write into a database that was opened with `ATTACH DATABASE … AS events_db`.
This is the path used by the td-watch post-commit promotion: project.db is the main schema (action_log lives there) and events.db is attached so the promotion + synced_at flip commit atomically.