Skip to content

[4.x] commands: add $scopeTenantsQuery - #1528

Merged
stancl merged 1 commit into
masterfrom
push-kzymywtykpxq
Nov 12, 2026
Merged

stancl merged 1 commit into
masterfrom
push-kzymywtykpxq

Conversation

@stancl

@stancl stancl commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Commands using HasTenantOptions now have a static $scopeTenantsQuery closure that receives the tenants query:

Migrate::$scopeTenantsQuery = fn (Builder $q) => $q->where('region', 'eu');

It's declared in the trait, so each command class has its own scope. The scope is applied as a nested where after --tenants, --skip-tenants and --with-pending, so it's always combined with those options, including when the scope uses orWhere(). This also covers the tenant chunks built for parallel runs.

MigrateFresh's nested tenants:migrate call and the MigrateDatabase and SeedDatabase jobs run for tenants that were already selected, so they bypass the scope using withoutTenantsQueryScope().

Summary by CodeRabbit

  • New Features

    • Tenant commands can now apply custom filters when selecting tenants.
  • Bug Fixes

    • Tenant migrations and seeding run without unintended tenant-query filters from the calling command, helping ensure the selected tenant is processed.
    • Fresh migrations preserve tenant selection behavior when invoking tenant migrations.

Commands using HasTenantOptions now have a static $scopeTenantsQuery
closure that receives the tenants query:

    Migrate::$scopeTenantsQuery = fn (Builder $q) => $q->where('region', 'eu');

It's declared in the trait, so each command class has its own scope.
The scope is applied as a nested where after --tenants, --skip-tenants
and --with-pending, so it's always combined with those options, including
when the scope uses orWhere(). This also covers the tenant chunks built
for parallel runs.

MigrateFresh's nested tenants:migrate call and the MigrateDatabase and
SeedDatabase jobs run for tenants that were already selected, so they
bypass the scope using withoutTenantsQueryScope().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.67%. Comparing base (58d8e6d) to head (7be2371).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1528      +/-   ##
============================================
+ Coverage     88.31%   88.67%   +0.35%     
- Complexity     1327     1380      +53     
============================================
  Files           188      188              
  Lines          3782     3937     +155     
============================================
+ Hits           3340     3491     +151     
- Misses          442      446       +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change adds configurable tenant query scopes and a helper to temporarily disable them. Tenant-specific migration and seeding flows use the helper. Tests cover scoped selection, command isolation, and tenant-specific execution.

Changes: Tenant query scoping

Layer / File(s) Summary
Scope definition and tenant selection
src/Concerns/HasTenantOptions.php, tests/CommandsTest.php
HasTenantOptions adds a query-scope callback and a helper that restores the callback after execution. Tests cover scoped tenant selection, chunking, --tenants filtering, and command scope boundaries.
Tenant-specific migration and seeding
src/Commands/MigrateFresh.php, src/Jobs/MigrateDatabase.php, src/Jobs/SeedDatabase.php, tests/CommandsTest.php
The migration and seeding flows temporarily disable the relevant query scope when invoking tenant-specific commands. Tests cover separate scopes for migration commands and migration of a created tenant.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Feature

Suggested reviewers: lukinovec, jimish-gamit, drbyte

Merge Risk: Minimal · up to 7be23

This adds an optional per-command tenant query scope and bypasses it for flows that operate on already-selected tenants. No concrete merge-blocking risk remains. Scope callbacks that add joins are not supported, which could be documented.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "[4.x] commands: add $scopeTenantsQuery" names the main change. The PR adds a static $scopeTenantsQuery closure to the HasTenantOptions trait, which applies a command-specific filter to the …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "[4.x] commands: add $scopeTenantsQuery" names the main change. The PR adds a static $scopeTenantsQuery closure to the HasTenantOptions trait, which applies a command-specific filter to the tenant query. The changeset also adds withoutTenantsQueryScope() and updates MigrateFresh, MigrateDatabase, and SeedDatabase to bypass the scope. The title covers the primary API addition and omits the bypass helper, which is expected for a short title.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

A rabbit checks the tenant rows,
And scopes the query where it goes.
When migrations need one key,
The scope steps back temporarily.
Then hops right back when work is through.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: ee37b4e9-b187-496d-b7c6-ed9fb69f8d58
Commits

Reviewing files that changed from the base of the PR and between d8fe210 and 7be2371.

Files selected for processing: 5

  • src/Commands/MigrateFresh.php
  • src/Concerns/HasTenantOptions.php
  • src/Jobs/MigrateDatabase.php
  • src/Jobs/SeedDatabase.php
  • tests/CommandsTest.php

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@stancl
stancl merged commit c61c852 into master Nov 12, 2026
14 checks passed
@stancl
stancl deleted the push-kzymywtykpxq branch November 12, 2026 11:14
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.

1 participant