Skip to content

Commit b9b0fde

Browse files
committed
fix: check ps_sync_state instead of ps_buckets for sync completion
ps_buckets is only populated when checkpoint messages are received, which may not happen even though data syncs. ps_sync_state tracks when sync actually completed and is more reliable.
1 parent 5bdf62c commit b9b0fde

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

‎internal/powersync/sync.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (s *Sync) IsRunning() bool {
157157
return s.cancel != nil
158158
}
159159

160-
// WaitForFirstSync blocks until the first sync completes (data in ps_buckets).
160+
// WaitForFirstSync blocks until the first sync completes.
161161
// Returns an error if the context is cancelled or sync fails.
162162
func (s *Sync) WaitForFirstSync(ctx context.Context) error {
163163
ticker := time.NewTicker(100 * time.Millisecond)
@@ -173,10 +173,11 @@ func (s *Sync) WaitForFirstSync(ctx context.Context) error {
173173
return err
174174
}
175175

176-
// Check if ps_buckets has data
176+
// Check if sync has completed by looking at ps_sync_state
177+
// This table is populated when PowerSync finishes its first sync
177178
if s.db != nil {
178179
var count int64
179-
err := s.db.QueryRow("SELECT COUNT(*) FROM ps_buckets").Scan(&count)
180+
err := s.db.QueryRow("SELECT COUNT(*) FROM ps_sync_state").Scan(&count)
180181
if err == nil && count > 0 {
181182
return nil
182183
}

‎internal/powersync/sync_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,15 +459,15 @@ func TestSync_TransientErrorRetries(t *testing.T) {
459459
func TestSync_WaitForFirstSync(t *testing.T) {
460460
t.Parallel()
461461

462-
t.Run("returns when ps_buckets has data", func(t *testing.T) {
462+
t.Run("returns when ps_sync_state has data", func(t *testing.T) {
463463
t.Parallel()
464464

465465
db := powersynctest.OpenTestDBWithSchema(t)
466466

467467
mock := &powersynctest.MockStreamer{
468468
ConnectFunc: func(ctx context.Context, req *powersync.StreamingSyncRequest, handler powersync.LineHandler) error {
469-
// Simulate sync by inserting data into ps_buckets
470-
_, err := db.Exec("INSERT INTO ps_buckets (name) VALUES ('test')")
469+
// Simulate sync completion by inserting into ps_sync_state
470+
_, err := db.Exec("INSERT INTO ps_sync_state (priority, last_synced_at) VALUES (0, datetime('now'))")
471471
if err != nil {
472472
return err
473473
}

0 commit comments

Comments
 (0)