sync

package
v0.30.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyEvent

func ApplyEvent(tx *sql.Tx, event Event, validator EntityValidator) (bool, error)

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

func BackfillOrphanEntities(tx *sql.Tx, sessionID string) (int, error)

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

func BackfillStaleIssues(tx *sql.Tx, sessionID string) (int, error)

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

func InitServerEventLog(db *sql.DB) error

InitServerEventLog creates the events table and index if they don't exist.

func MarkEventsSynced

func MarkEventsSynced(tx *sql.Tx, acks []Ack) error

MarkEventsSynced updates action_log rows with their server-assigned sequence numbers.

Types

type Ack

type Ack struct {
	ClientActionID int64
	ServerSeq      int64
}

Ack confirms a client action was accepted with a server sequence number.

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

type EntityValidator func(entityType string) bool

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

func GetPendingEvents(tx *sql.Tx, deviceID, sessionID string) ([]Event, error)

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

type FailedEvent struct {
	ServerSeq int64
	Error     error
}

FailedEvent records a single event that could not be applied.

type PullResult

type PullResult struct {
	Events        []Event
	LastServerSeq int64
	HasMore       bool
}

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

type PushResult struct {
	Accepted int
	Acks     []Ack
	Rejected []Rejection
}

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.

type Rejection

type Rejection struct {
	ClientActionID int64
	Reason         string
	ServerSeq      int64 // populated for "duplicate" rejections
}

Rejection explains why a client action was refused.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL