@@ -217,7 +217,7 @@ func runImportPieces(ctx context.Context, db *harmonydb.DB, si paths.SectorIndex
217217 continue
218218 }
219219
220- result , err := importStagedStorachaPiece (ctx , db , si , storageID , targetPath , filepath .Join (staging , entry .Name ()))
220+ result , err := importStagedStorachaPiece (ctx , db , si , storageID , targetPath , filepath .Join (staging , entry .Name ()), true )
221221 if err != nil {
222222 return out , err
223223 }
@@ -276,7 +276,7 @@ func runImportPieces(ctx context.Context, db *harmonydb.DB, si paths.SectorIndex
276276 return out , xerrors .Errorf ("renaming old path: %w" , err )
277277 }
278278
279- result , err := importStagedStorachaPiece (ctx , db , si , storageID , targetPath , stagingPath )
279+ result , err := importStagedStorachaPiece (ctx , db , si , storageID , targetPath , stagingPath , true )
280280 if err != nil {
281281 return out , err
282282 }
@@ -334,13 +334,15 @@ func storachaPieceInfoFromFileName(name string) (*mk20.PieceInfo, bool, error) {
334334
335335// importStagedStorachaPiece finishes one target/storacha-staging/<cid>.car.
336336// It first creates or reuses parked_pieces/refs in a committed DB transaction.
337+ // makePDPPieceRef keeps import-pieces behavior unchanged while letting aggregate
338+ // imports use the aggregate migration data_url and skip the public PDP piece ref.
337339// If an existing incomplete parked_piece still has a live task, it returns
338340// Imported=false and leaves the staged file untouched for a later retry.
339341// Otherwise it consumes the staged file: remove it if the final
340342// target/piece/s-t00-<id> already exists, or rename it to that final path. It
341343// then declares the piece location and marks parked_pieces.complete unless the
342344// row was already complete.
343- func importStagedStorachaPiece (ctx context.Context , db * harmonydb.DB , si paths.SectorIndex , storageID storiface.ID , targetPath , stagingPath string ) (storachaImportResult , error ) {
345+ func importStagedStorachaPiece (ctx context.Context , db * harmonydb.DB , si paths.SectorIndex , storageID storiface.ID , targetPath , stagingPath string , makePDPPieceRef bool ) (storachaImportResult , error ) {
344346 info , err := os .Stat (stagingPath )
345347 if err != nil {
346348 return storachaImportResult {}, xerrors .Errorf ("checking staging file: %w" , err )
@@ -383,7 +385,7 @@ func importStagedStorachaPiece(ctx context.Context, db *harmonydb.DB, si paths.S
383385 return true , nil
384386 }
385387
386- if err := ensureStorachaRefsTx (tx , parkedPieceID , pi ); err != nil {
388+ if err := ensureStorachaRefsTx (tx , parkedPieceID , pi , makePDPPieceRef ); err != nil {
387389 return false , err
388390 }
389391
@@ -474,7 +476,7 @@ func recoverFinalStorachaPieces(ctx context.Context, db *harmonydb.DB, si paths.
474476 break
475477 }
476478
477- imported , err := recoverFinalStorachaPiece (ctx , db , si , storageID , targetPath , row )
479+ imported , err := recoverFinalStorachaPiece (ctx , db , si , storageID , targetPath , row , true )
478480 if err != nil {
479481 return err
480482 }
@@ -510,7 +512,7 @@ func storachaPieceCIDV2FromRow(row storachaFinalRecoveryRow) (string, error) {
510512// DB row, reuses the same claim/ref checks as staged import, then declares the
511513// final file and marks the parked piece complete. It returns false when the
512514// final file is missing or a live task still owns the parked_piece.
513- func recoverFinalStorachaPiece (ctx context.Context , db * harmonydb.DB , si paths.SectorIndex , storageID storiface.ID , targetPath string , row storachaFinalRecoveryRow ) (bool , error ) {
515+ func recoverFinalStorachaPiece (ctx context.Context , db * harmonydb.DB , si paths.SectorIndex , storageID storiface.ID , targetPath string , row storachaFinalRecoveryRow , makePDPPieceRef bool ) (bool , error ) {
514516 finalPath := storachaFinalPiecePath (targetPath , row .ID )
515517 info , err := os .Stat (finalPath )
516518 if os .IsNotExist (err ) {
@@ -542,7 +544,7 @@ func recoverFinalStorachaPiece(ctx context.Context, db *harmonydb.DB, si paths.S
542544 if ! state .UseStagedFile {
543545 return true , nil
544546 }
545- if err := ensureStorachaRefsTx (tx , row .ID , pi ); err != nil {
547+ if err := ensureStorachaRefsTx (tx , row .ID , pi , makePDPPieceRef ); err != nil {
546548 return false , err
547549 }
548550 return true , nil
@@ -617,12 +619,15 @@ func claimExistingStorachaPieceTx(tx *harmonydb.Tx, parkedPieceID int64) (storac
617619 return state , nil
618620}
619621
620- // ensureStorachaRefsTx enforces the intended one-to-one shape for this import.
621- // It locks storacha-migration parked_piece_refs for this parked piece, fails if
622- // more than one exists, inserts the missing ref if needed, then does the same
623- // for pdp_piecerefs through the unique piece_ref relation. Existing PDP refs
624- // must already point at the same service and piece CID.
625- func ensureStorachaRefsTx (tx * harmonydb.Tx , parkedPieceID int64 , pi * mk20.PieceInfo ) error {
622+ // ensureStorachaRefsTx enforces the intended one-to-one shape for this migration
623+ // mode. Direct imports create storacha-migration refs and PDP refs; aggregate
624+ // imports create storacha-migration-aggregate refs and intentionally skip PDP refs.
625+ func ensureStorachaRefsTx (tx * harmonydb.Tx , parkedPieceID int64 , pi * mk20.PieceInfo , makePDPPieceRef bool ) error {
626+ dataURL := storachaMigrationAggregateDataURL
627+ if makePDPPieceRef {
628+ dataURL = storachaMigrationDataURL
629+ }
630+
626631 var refs []struct {
627632 RefID int64 `db:"ref_id"`
628633 }
@@ -633,7 +638,7 @@ func ensureStorachaRefsTx(tx *harmonydb.Tx, parkedPieceID int64, pi *mk20.PieceI
633638 AND data_url = $2
634639 AND long_term = TRUE
635640 ORDER BY ref_id
636- FOR UPDATE` , parkedPieceID , storachaMigrationDataURL )
641+ FOR UPDATE` , parkedPieceID , dataURL )
637642 if err != nil {
638643 return xerrors .Errorf ("query storacha parked piece refs: %w" , err )
639644 }
@@ -648,18 +653,26 @@ func ensureStorachaRefsTx(tx *harmonydb.Tx, parkedPieceID int64, pi *mk20.PieceI
648653 err = tx .QueryRow (`
649654 INSERT INTO parked_piece_refs (piece_id, data_url, long_term)
650655 VALUES ($1, $2, TRUE)
651- RETURNING ref_id` , parkedPieceID , storachaMigrationDataURL ).Scan (& pieceRef )
656+ RETURNING ref_id` , parkedPieceID , dataURL ).Scan (& pieceRef )
652657 if err != nil {
653658 return xerrors .Errorf ("insert storacha parked piece ref: %w" , err )
654659 }
655660 }
656661
662+ if ! makePDPPieceRef {
663+ return nil
664+ }
665+
666+ return ensurePDPPieceRef (tx , pieceRef , pi )
667+ }
668+
669+ func ensurePDPPieceRef (tx * harmonydb.Tx , pieceRef int64 , pi * mk20.PieceInfo ) error {
657670 var pdpRefs []struct {
658671 ID int64 `db:"id"`
659672 Service string `db:"service"`
660673 PieceCID string `db:"piece_cid"`
661674 }
662- err = tx .Select (& pdpRefs , `
675+ err : = tx .Select (& pdpRefs , `
663676 SELECT id, service, piece_cid
664677 FROM pdp_piecerefs
665678 WHERE piece_ref = $1
0 commit comments