Skip to content

Conversation

@lukinovec
Copy link
Contributor

@lukinovec lukinovec commented Nov 4, 2025

This PR adds listeners for handling cleanup of pivot records/mappings

  • When any synced resource got deleted - on SyncedResourceDeleted (fired when any synced resource is deleted), DeleteResourceMapping. Previously, the cleanup was only happening on SyncMaster/central resource deletion. Now, triggerDeleteEvent is called while deleting/force deleting any synced resource, making the cleanup possible even for tenant resources.
  • when a tenant gets deleted - on TenantDeleted, DeleteAllTenantMappings -- commented out by default (until now, the pivot records were only getting deleted when the tenant key column had a constraint, and the constraint had onDelete('cascade')-- db-level)

This PR also improves some resource syncing implementation details, for example, the ResourceSyncing trait now uses static::deleted() instead of static::deleting(), which is safer. The SyncedResourceDeleted event with the DeleteResourceMapping listener also lets us separate the resource x pivot record deletion concerns, and improve the performance (e.g. the mass delete of pivot records when SyncMaster's deleted instead of a for loop where we were deleting the pivot records one by one).

The PivotWithRelation interface got simplified and renamed to PivotWithCentralResource. The interface now only requires the getCentralResourceClass(): string method that should return the central resource's class name (instead of a model instance).

We also move global ID generation to a separate method to make it easily overridable, in case a developer wants to use different UniqueIdentifierGenerator than the one used for tenant IDs.

We also add support for morph maps.

@codecov
Copy link

codecov bot commented Nov 4, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.89%. Comparing base (45cf702) to head (159e600).
⚠️ Report is 6 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1411      +/-   ##
============================================
+ Coverage     85.79%   85.89%   +0.10%     
- Complexity     1137     1143       +6     
============================================
  Files           181      184       +3     
  Lines          3329     3353      +24     
============================================
+ Hits           2856     2880      +24     
  Misses          473      473              

☔ View full report in Codecov by Sentry.
📢 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.

@lukinovec lukinovec marked this pull request as ready for review November 5, 2025 16:32
@stancl stancl changed the title Improve resource syncing (refactor + handle mapping cleanup) [4.x] Improve resource syncing (refactor + handle mapping cleanup) Nov 14, 2025
@stancl stancl force-pushed the resource-syncing-refactor branch 2 times, most recently from cf01ce9 to 14d40b3 Compare November 14, 2025 11:17
@stancl stancl marked this pull request as draft November 17, 2025 17:06
@stancl
Copy link
Member

stancl commented Nov 17, 2025

We should also support using a UniqueIdentifierGenerator distinct from the one used for tenant key generation.

@stancl stancl self-assigned this Nov 17, 2025
@stancl stancl force-pushed the resource-syncing-refactor branch 2 times, most recently from cbd2f45 to 3974ad0 Compare November 21, 2025 01:08
@stancl stancl force-pushed the resource-syncing-refactor branch 2 times, most recently from b1c533c to f53fcb2 Compare November 21, 2025 01:22
@stancl stancl requested a review from Copilot November 21, 2025 01:24

This comment was marked as resolved.

@stancl stancl force-pushed the resource-syncing-refactor branch 5 times, most recently from 66fdf96 to 12df6f1 Compare November 25, 2025 04:30
lukinovec and others added 4 commits November 26, 2025 05:52
Also move pivot record deletion to that listener and improve tests

The 'tenant pivot records are deleted along with the tenants to which
they belong to' test is failing in this commit -- the listener
for deleting mappings when a *tenant* is deleted is only implemented
in the next commit. The only change done here is to re-add FKs
(necessary for passing *in this commit* in that specific dataset
variant) that were removed from the default test migration as we now
have the DeleteResourceMapping listener that's enabled by default.
Also make all resource syncing-related listener closures static.

Also correct return type for getGlobalIdentifierKey to string|int.
(We intentionally do not support returning null like many other
"get x key" methods would since such a case might break resource
syncing logic. This is also why we use inline getAttribute() in the
creating listener instead of calling the method.)
The old names of the class and method were misleading. We don't
actually need any relation. And we don't even need a model instance
as we were returning previously -- the only use of that method was
in TriggerSyncingEvents which would immediately use ::class on the
returned value. Therefore, all we are asking for in this interface
is just the central resource class.
@stancl stancl force-pushed the resource-syncing-refactor branch from 12df6f1 to 04a20ca Compare November 26, 2025 04:53
@stancl stancl force-pushed the resource-syncing-refactor branch from e85ee25 to 159e600 Compare December 12, 2025 02:44
@stancl stancl changed the title [4.x] Improve resource syncing (refactor + handle mapping cleanup) [4.x] Improve resource syncing (refactor + mapping cleanup + morph maps) Dec 12, 2025
@stancl stancl requested a review from Copilot December 12, 2025 02:50
@stancl stancl marked this pull request as ready for review December 12, 2025 02:50

This comment was marked as resolved.

@stancl stancl merged commit a778e17 into master Dec 12, 2025
17 checks passed
@stancl stancl deleted the resource-syncing-refactor branch December 12, 2025 03: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