feat(sync): a stable identity per contact, indexed

Two PCs exchanging changes through a folder must be able to name the SAME
contact in both logs. "the QSO with M0ABC at 14:32" is a guess and the two
machines can disagree about which row that is, so an edit or a deletion cannot
be addressed at all without an identity that travels with the record.

A real indexed column, not a key inside extras_json: the identity is resolved
once per incoming change, and scanning JSON for it would turn every sync into a
full read of a 120 000-QSO logbook. A column costs one migration; the JSON would
cost a scan every time. That was the decision to confirm, and it is confirmed.

It follows the award_refs pattern — read in selectCols, absent from columnList,
written only through its own methods. So an ordinary edit cannot clobber it,
which matters: another machine addresses the contact by that id, and losing it
makes the same QSO arrive again as a new one. Pinned by a test that saves an
edit with the field deliberately blanked.

Existing contacts are stamped in batches inside one transaction rather than one
commit each — on a remote MySQL the round trip dominates, and 120 000 commits is
the difference between a minute and an afternoon. And a dedupe-key map lets two
machines that already hold the same imported log recognise each other's contacts
instead of copying 120 000 of them across.

MySQL nearly lost its logbook to this. It cannot index a TEXT column without a
prefix length: the migration would fail with error 1170 and fail again on every
startup, with no way out from the interface. The translator only emits VARCHAR
for names listed by hand in varcharColumns. sync_uid is now listed — and a test
reads the migrations, works out which indexed columns are TEXT, and fails if the
list does not cover them, so the next one cannot reach a shared logbook.
This commit is contained in:
2026-08-16 23:16:52 +02:00
parent 7d664bd1de
commit 724e68b38d
6 changed files with 464 additions and 2 deletions
+138 -2
View File
@@ -215,6 +215,12 @@ type QSO struct {
// that doesn't know about it can't clobber it.
AwardRefs string `json:"award_refs,omitempty"`
// SyncUID is this contact's stable identity for folder synchronisation —
// what lets another machine name the same QSO when it edits or deletes it.
// Like AwardRefs it is read here but NOT in columnList, so an ordinary edit
// can never clobber it; it is written only by SetSyncUID / BackfillSyncUIDs.
SyncUID string `json:"sync_uid,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
@@ -271,7 +277,7 @@ const columnList = `callsign, qso_date, qso_date_off, band, band_rx, mode, submo
// award_refs is read here but is NOT part of columnList (the insert/update
// write path) — it is a derived cache written only via SetAwardRefs, so a
// normal QSO write can never clobber it.
const selectCols = `id, ` + columnList + `, award_refs, created_at, updated_at`
const selectCols = `id, ` + columnList + `, award_refs, sync_uid, created_at, updated_at`
// columnCount is derived from columnList at init so they can never drift.
var columnCount = countColumns(columnList)
@@ -3076,6 +3082,7 @@ func scanQSO(s scanner) (QSO, error) {
myARRLSect, myVUCCGrids sql.NullString
extrasJSON sql.NullString
awardRefs sql.NullString
syncUID sql.NullString
createdStr, updatedStr string
)
if err := s.Scan(
@@ -3103,7 +3110,7 @@ func scanQSO(s scanner) (QSO, error) {
&skcc, &fists, &tenTen, &contactedOp, &eqCall, &pfx, &myName, &class,
&darcDOK, &myDarcDOK, &region, &silentKey, &swl, &qsoComplete, &qsoRandom,
&creditGranted, &creditSubmitted, &myARRLSect, &myVUCCGrids,
&extrasJSON, &awardRefs, &createdStr, &updatedStr,
&extrasJSON, &awardRefs, &syncUID, &createdStr, &updatedStr,
); err != nil {
return QSO{}, fmt.Errorf("scan qso: %w", err)
}
@@ -3303,6 +3310,7 @@ func scanQSO(s scanner) (QSO, error) {
q.MyVUCCGrids = myVUCCGrids.String
q.Extras = decodeExtras(extrasJSON.String)
q.AwardRefs = awardRefs.String
q.SyncUID = syncUID.String
return q, nil
}
@@ -3413,3 +3421,131 @@ func (r *Repo) OrderedIDs(ctx context.Context) ([]int64, time.Time, error) {
// Parsed once, for the newest row only — the ordering came from SQL.
return out, parseTimeLoose(lastDate), nil
}
// --- Folder synchronisation identity -----------------------------------------
// SetSyncUID stamps a contact's sync identity. Targeted UPDATE: it must never
// go through the normal write path, or an ordinary edit would clobber it.
func (r *Repo) SetSyncUID(ctx context.Context, id int64, uid string) error {
_, err := r.db.ExecContext(ctx, `UPDATE qso SET sync_uid = ? WHERE id = ?`, uid, id)
return err
}
// IDBySyncUID resolves an incoming change to a local row. Indexed, so this is
// the one lookup a sync performs per record and it stays a key hit rather than
// a scan — which is the whole reason sync_uid is a column and not a JSON key.
func (r *Repo) IDBySyncUID(ctx context.Context, uid string) (int64, bool, error) {
if uid == "" {
return 0, false, nil
}
var id int64
err := r.db.QueryRowContext(ctx, `SELECT id FROM qso WHERE sync_uid = ? LIMIT 1`, uid).Scan(&id)
if err == sql.ErrNoRows {
return 0, false, nil
}
if err != nil {
return 0, false, err
}
return id, true, nil
}
// CountWithoutSyncUID reports how many contacts still have no identity — what
// the settings panel shows before the first synchronisation.
func (r *Repo) CountWithoutSyncUID(ctx context.Context) (int64, error) {
var n int64
err := r.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM qso WHERE sync_uid = '' OR sync_uid IS NULL`).Scan(&n)
return n, err
}
// NeedSyncUID returns the ids of contacts still without an identity, at most
// limit of them.
//
// Batched rather than "all of them": a 120 000-QSO logbook is given its
// identities a few thousand at a time, so the first synchronisation cannot hold
// the database — or the interface — for however long a single enormous
// transaction would take on a remote MySQL.
func (r *Repo) NeedSyncUID(ctx context.Context, limit int) ([]int64, error) {
if limit <= 0 {
limit = 2000
}
rows, err := r.db.QueryContext(ctx,
`SELECT id FROM qso WHERE sync_uid = '' OR sync_uid IS NULL ORDER BY id LIMIT ?`, limit)
if err != nil {
return nil, err
}
defer rows.Close()
var out []int64
for rows.Next() {
var id int64
if err := rows.Scan(&id); err != nil {
return nil, err
}
out = append(out, id)
}
return out, rows.Err()
}
// AssignSyncUIDs stamps a batch of identities in ONE transaction.
//
// One statement per row inside a single transaction, not one transaction per
// row: on a remote MySQL the round trip dominates, and 120 000 separate commits
// is the difference between a minute and an afternoon.
func (r *Repo) AssignSyncUIDs(ctx context.Context, uids map[int64]string) error {
if len(uids) == 0 {
return nil
}
tx, err := r.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
stmt, err := tx.PrepareContext(ctx, `UPDATE qso SET sync_uid = ? WHERE id = ? AND (sync_uid = '' OR sync_uid IS NULL)`)
if err != nil {
return err
}
defer stmt.Close()
for id, uid := range uids {
if _, err := stmt.ExecContext(ctx, uid, id); err != nil {
return err
}
}
return tx.Commit()
}
// SyncUIDByDedupeKey maps every contact's dedupe key to its sync identity.
//
// This is what stops two machines that already hold the SAME imported log from
// copying it to each other: an incoming contact whose identity is unknown but
// whose key matches one here is the same contact, and it adopts the incoming
// identity instead of being inserted a second time.
//
// One scan of four short columns, done once per synchronisation pass — the same
// shape as the importer's own dedupe load, which is the cheapest read in the
// repo.
func (r *Repo) SyncUIDByDedupeKey(ctx context.Context) (map[string]struct {
ID int64
UID string
}, error) {
rows, err := r.db.QueryContext(ctx, `
SELECT id, sync_uid, callsign, substr(qso_date, 1, 16), band, mode FROM qso`)
if err != nil {
return nil, err
}
defer rows.Close()
out := make(map[string]struct {
ID int64
UID string
}, 1024)
for rows.Next() {
var id int64
var uid, call, when, band, mode sql.NullString
if err := rows.Scan(&id, &uid, &call, &when, &band, &mode); err != nil {
return nil, err
}
out[DedupeKey(call.String, when.String, band.String, mode.String)] = struct {
ID int64
UID string
}{ID: id, UID: uid.String}
}
return out, rows.Err()
}