Skip to content

Delete product inverseAssociations#2280

Merged
glennjacobs merged 4 commits intolunarphp:1.xfrom
wychoong:patch-1
Nov 11, 2025
Merged

Delete product inverseAssociations#2280
glennjacobs merged 4 commits intolunarphp:1.xfrom
wychoong:patch-1

Conversation

@wychoong
Copy link
Contributor

@wychoong wychoong commented Aug 18, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Permanently deleting a product now removes both direct and reverse product relationships, ensuring no lingering links appear on related products.
    • Improves data integrity by fully clearing associated relationships during force delete, preventing inconsistent or orphaned connections.
    • Soft-delete behavior is unchanged.

@vercel
Copy link

vercel bot commented Aug 18, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
lunar-docs Ready Ready Preview Comment Aug 26, 2025 5:30am

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 18, 2025

Walkthrough

Updated ProductObserver::deleting to also delete inverseAssociations() during force deletes, executed after associations()->delete() and before channels()->detach(). No other functional branches or signatures changed.

Changes

Cohort / File(s) Summary of Changes
Observers — Product force-delete logic
packages/core/src/Observers/ProductObserver.php
In deleting(ProductContract $product), added inverseAssociations()->delete() in the force-delete branch after associations()->delete(); other detach/delete steps unchanged; non-force-delete path unaffected.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (3)
packages/core/src/Observers/ProductObserver.php (3)

14-31: Consider wrapping the force-delete cascade in a DB transaction

Multiple destructive operations are performed across relations. Wrapping them in a single transaction reduces the risk of partial cleanup if an intermediate query fails.

Example:

use Illuminate\Support\Facades\DB;

public function deleting(ProductContract $product): void
{
    if ($product->isForceDeleting()) {
        DB::transaction(function () use ($product) {
            $product->variants()->withTrashed()->get()->each->forceDelete();

            $product->collections()->detach();
            $product->customerGroups()->detach();
            $product->urls()->delete();
            $product->productOptions()->detach();

            $product->associations()->delete();
            $product->inverseAssociations()->delete();

            $product->channels()->detach();
            $product->tags()->detach();
        });
    } else {
        $product->variants()->get()->each->delete();
    }
}

15-15: Optional: avoid loading all variants in memory when force-deleting

For large catalogs, loading all variants to delete them may be memory-heavy. Chunking or cursor-based iteration reduces memory footprint.

Example with chunking:

$product->variants()->withTrashed()->chunkById(200, function ($variants) {
    $variants->each->forceDelete();
});

10-12: Docblock mismatch with method/event

The docblock says “Handle the ProductVariant "deleted" event.” but the method is ProductObserver::deleting(ProductContract $product). Consider correcting to avoid confusion.

Suggested wording:

/**
 * Handle the Product "deleting" event.
 */
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between ecb4b46 and 205f157.

📒 Files selected for processing (1)
  • packages/core/src/Observers/ProductObserver.php (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/core/src/Observers/ProductObserver.php (1)
packages/core/src/Models/ProductVariant.php (1)
  • product (89-92)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (18)
  • GitHub Check: search - PHP 8.4 - L12.* ↑
  • GitHub Check: core - PHP 8.4 - L12.* ↑ E
  • GitHub Check: admin - PHP 8.4 - L12.* ↑ E
  • GitHub Check: stripe - PHP 8.4 - L12.* ↑
  • GitHub Check: stripe - PHP 8.4 - L12.* ↑ E
  • GitHub Check: core - PHP 8.4 - L11.* ↑ E
  • GitHub Check: stripe - PHP 8.3 - L12.* ↑
  • GitHub Check: admin - PHP 8.4 - L11.* ↑ E
  • GitHub Check: admin - PHP 8.4 - L11.* ↑
  • GitHub Check: admin - PHP 8.3 - L12.* ↑ E
  • GitHub Check: core - PHP 8.4 - L11.* ↑
  • GitHub Check: core - PHP 8.3 - L12.* ↑
  • GitHub Check: core - PHP 8.3 - L12.* ↑ E
  • GitHub Check: admin - PHP 8.3 - L12.* ↑
  • GitHub Check: admin - PHP 8.3 - L11.* ↑
  • GitHub Check: admin - PHP 8.3 - L11.* ↑ E
  • GitHub Check: core - PHP 8.3 - L11.* ↑
  • GitHub Check: core - PHP 8.3 - L11.* ↑ E
🔇 Additional comments (1)
packages/core/src/Observers/ProductObserver.php (1)

27-28: Verified hard-delete semantics for ProductAssociation
The ProductAssociation model does not use SoftDeletes, so associations()->delete() and inverseAssociations()->delete() already perform permanent deletes. No forceDelete() changes are needed.

@glennjacobs glennjacobs merged commit 9657278 into lunarphp:1.x Nov 11, 2025
83 of 84 checks passed
@wychoong wychoong deleted the patch-1 branch November 12, 2025 00:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants