diff --git a/pkg/hold/server.go b/pkg/hold/server.go index 848f404..0442216 100644 --- a/pkg/hold/server.go +++ b/pkg/hold/server.go @@ -123,7 +123,26 @@ func NewHoldServer(cfg *Config) (*HoldServer, error) { return nil, fmt.Errorf("failed to create S3 service: %w", err) } - // Bootstrap PDS with captain record, hold owner as first crew member, and profile + // Bootstrap events from existing repo records (one-time migration). + // Must run BEFORE the live event handler is wired, so it captures + // the full historical state without interference from new writes. + if err := s.broadcaster.BootstrapFromRepo(s.PDS); err != nil { + slog.Warn("Failed to bootstrap events from repo", "error", err) + } + + // Backfill records index from existing MST data (one-time on startup) + if err := s.PDS.BackfillRecordsIndex(ctx); err != nil { + slog.Warn("Failed to backfill records index", "error", err) + } + + // Wire up repo event handler with records indexing + broadcaster. + // Must be BEFORE Bootstrap so that record creates/updates during + // bootstrap (captain, crew, profile) emit to the firehose. + indexingHandler := s.PDS.CreateRecordsIndexEventHandler(s.broadcaster.SetRepoEventHandler()) + s.PDS.RepomgrRef().SetEventHandler(indexingHandler, true) + + // Bootstrap PDS with captain record, hold owner as first crew member, and profile. + // Now that the event handler is wired, any changes here emit to the firehose. if err := s.PDS.Bootstrap(ctx, s3Service, pds.BootstrapConfig{ OwnerDID: cfg.Registration.OwnerDID, Public: cfg.Server.Public, @@ -148,20 +167,6 @@ func NewHoldServer(cfg *Config) (*HoldServer, error) { } } - // Bootstrap events from existing repo records (one-time migration) - if err := s.broadcaster.BootstrapFromRepo(s.PDS); err != nil { - slog.Warn("Failed to bootstrap events from repo", "error", err) - } - - // Backfill records index from existing MST data (one-time on startup) - if err := s.PDS.BackfillRecordsIndex(ctx); err != nil { - slog.Warn("Failed to backfill records index", "error", err) - } - - // Wire up repo event handler with records indexing + broadcaster - indexingHandler := s.PDS.CreateRecordsIndexEventHandler(s.broadcaster.SetRepoEventHandler()) - s.PDS.RepomgrRef().SetEventHandler(indexingHandler, true) - slog.Info("Embedded PDS initialized successfully with firehose and records index enabled") } else { return nil, fmt.Errorf("database path is required for embedded PDS authorization")