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).
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.