diff --git a/internal/store/rebase_agreement_atomic_test.go b/internal/store/rebase_agreement_atomic_test.go new file mode 100644 index 00000000..5f59e86d --- /dev/null +++ b/internal/store/rebase_agreement_atomic_test.go @@ -0,0 +1,15 @@ +package store + +import "testing" + +// An injected failure of the second SQL statement must roll back the first. +func TestRebaseAgreedHashesRollsBackOnNamedRootSQLFailure(t *testing.T) { + s := openTestStore(t) + if err := s.CreateGame(Game{ID: "atomic", Name: "Atomic", SavePath: "/old"}); err != nil { t.Fatal(err) } + if err := s.SetAgreedHash("atomic", "peer", "OLD"); err != nil { t.Fatal(err) } + if err := s.SetAgreedHashForRoot("atomic", "peer", "config", "ROOT"); err != nil { t.Fatal(err) } + if _, err := s.db.Exec(`CREATE TRIGGER fail_root_update BEFORE UPDATE ON game_root_sync_state BEGIN SELECT RAISE(ABORT, 'injected second statement failure'); END`); err != nil { t.Fatal(err) } + if err := s.RebaseAgreedHashesForGame("atomic", "NEW"); err == nil { t.Fatal("injected failure not reported") } + if got := s.GetAgreedHash("atomic", "peer"); got != "OLD" { t.Fatalf("primary agreement committed despite second failure: %q", got) } + if got := s.GetAgreedHashForRoot("atomic", "peer", "config"); got != "ROOT" { t.Fatalf("named-root agreement changed: %q", got) } +} diff --git a/internal/store/root_sync_state.go b/internal/store/root_sync_state.go index f271d295..bba85a81 100644 --- a/internal/store/root_sync_state.go +++ b/internal/store/root_sync_state.go @@ -171,24 +171,23 @@ func (s *Store) SetSyncStateForRoot(gameID, peerID, root string, files, dirs []s // records which paths the two devices share, and losing it would make every // deletion look like a new file. func (s *Store) RebaseAgreedHashesForGame(gameID, newHash string) error { - // Only rows that already held an agreement are rewritten. A peer this - // game has never converged with must keep no base at all — inventing one - // would claim an agreement that never happened. - _, err := s.db.Exec( - `UPDATE game_peer_sync_state SET agreed_hash = ?, pushed_hash = '' - WHERE game_id = ? AND agreed_hash != ''`, newHash, gameID) + tx, err := s.db.Beginx() if err != nil { + return err + } + defer tx.Rollback() + // Preserve old agreement semantics without partially committing. + if _, err := tx.Exec( + `UPDATE game_peer_sync_state SET agreed_hash = ?, pushed_hash = '' + WHERE game_id = ? AND agreed_hash != ''`, newHash, gameID); err != nil { return fmt.Errorf("rebase agreed hashes for %s: %w", gameID, err) } - // Extra locations are not affected by the primary rebase; their own bases - // are cleared, since a rule can exclude files inside them too and there is - // no single hash that fits every location. - if _, err := s.db.Exec( - `UPDATE game_root_sync_state SET agreed_hash = '', pushed_hash = '' WHERE game_id = ?`, - gameID); err != nil { + // Named roots are independently reconciled after exclusion changes. + if _, err := tx.Exec( + `UPDATE game_root_sync_state SET agreed_hash = '', pushed_hash = '' WHERE game_id = ?`, gameID); err != nil { return fmt.Errorf("clear root agreed hashes for %s: %w", gameID, err) } - return nil + return tx.Commit() } func (s *Store) ClearAgreedHashesForGame(gameID string) error {