Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2798,9 +2798,13 @@ func (c *Cache) storeInDatabase(
return fmt.Errorf("error parsing the nar URL: %w", err)
}

// Normalize the NAR URL to remove any narinfo hash prefix.
// This ensures nar_files.hash matches what's actually stored in the storage layer.
normalizedNarURL := narURL.Normalize()
Comment thread
kalbasit marked this conversation as resolved.

// Check if nar_file already exists (multiple narinfos can share the same nar_file)

narFileID, err := c.ensureNarFile(ctx, qtx, narURL, narInfo.FileSize)
narFileID, err := createOrUpdateNarFile(ctx, qtx, normalizedNarURL, narInfo.FileSize)
if err != nil {
return err
}
Expand Down Expand Up @@ -2943,7 +2947,7 @@ func (c *Cache) checkAndFixNarInfosForNar(ctx context.Context, narURL nar.URL) e
return errors.Join(errs...)
}

func (c *Cache) ensureNarFile(
func createOrUpdateNarFile(
ctx context.Context,
qtx database.Querier,
narURL nar.URL,
Expand Down Expand Up @@ -3195,21 +3199,20 @@ func storeNarInfoInDatabase(ctx context.Context, db database.Querier, hash strin
return fmt.Errorf("error parsing the nar URL: %w", err)
}

// Normalize the NAR URL to remove any narinfo hash prefix.
// This ensures nar_files.hash matches what's actually stored in the storage layer.
normalizedNarURL := narURL.Normalize()
Comment thread
kalbasit marked this conversation as resolved.

// Create or get nar_file record
newNarFile, err := qtx.CreateNarFile(ctx, database.CreateNarFileParams{
Hash: narURL.Hash,
Compression: narURL.Compression.String(),
Query: narURL.Query.Encode(),
FileSize: narInfo.FileSize,
})
narFileID, err := createOrUpdateNarFile(ctx, qtx, normalizedNarURL, narInfo.FileSize)
if err != nil {
return fmt.Errorf("error creating or updating nar_file record in the database: %w", err)
return err
}

// Link narinfo to nar_file
if err := qtx.LinkNarInfoToNarFile(ctx, database.LinkNarInfoToNarFileParams{
NarInfoID: nir.ID,
NarFileID: newNarFile.ID,
NarFileID: narFileID,
}); err != nil {
// Duplicate key errors are expected with UPSERT
if !database.IsDuplicateKeyError(err) {
Expand Down