Skip to content

ref(backup): Move relocation logic into models#55065

Merged
azaslavsky merged 1 commit into
masterfrom
azaslavsky/backup/relocation_logic
Aug 29, 2023
Merged

ref(backup): Move relocation logic into models#55065
azaslavsky merged 1 commit into
masterfrom
azaslavsky/backup/relocation_logic

Conversation

@azaslavsky

Copy link
Copy Markdown
Contributor

We've accumulated a decent number of special cases for how we do relocation, what with Actors and Teams having circular dependencies, Users auto-creating their UserEmails, the need to sanitize out dangerous models like UserPermissions, etc, etc, etc.

This is a simple refactoring change that uses a roughly "typeclass"-ish pattern: we define two new methods on BaseModel, _normalize_before_relocation_import to transform the data (no transform by default) and write_relocation_import to actually save it to the database. Downstream models that need special logic at either of these stages are then free overwrite one or both of these steps as they please.

The main benefit of this approach is that this logic lives closer to the models it affects, rather than in some farflung section of the codebase. When we make changes to them in the future, relocation logic will be right there, encouraging folks to remember to change how that works (if necessary) as well.

@azaslavsky azaslavsky requested a review from corps August 18, 2023 18:26
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Aug 18, 2023
@azaslavsky

Copy link
Copy Markdown
Contributor Author

@corps This PR is still in draft mode because its on top of a stack which is blocked on a getsentry merge (hence those tests failing), but it is otherwise ready for review.

@codecov

codecov Bot commented Aug 18, 2023

Copy link
Copy Markdown

Codecov Report

Merging #55065 (f6dd2cd) into master (95f56de) will decrease coverage by 0.01%.
Report is 12 commits behind head on master.
The diff coverage is 91.66%.

❗ Current head f6dd2cd differs from pull request most recent head 9abb7c0. Consider uploading reports for the commit 9abb7c0 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #55065      +/-   ##
==========================================
- Coverage   79.98%   79.98%   -0.01%     
==========================================
  Files        5037     5037              
  Lines      214727   214717      -10     
  Branches    36477    36462      -15     
==========================================
- Hits       171753   171740      -13     
- Misses      37695    37703       +8     
+ Partials     5279     5274       -5     
Files Changed Coverage Δ
src/sentry/backup/imports.py 94.44% <75.00%> (-2.05%) ⬇️
src/sentry/models/team.py 86.88% <81.81%> (-0.33%) ⬇️
src/sentry/db/models/base.py 90.41% <95.83%> (+0.98%) ⬆️
src/sentry/models/actor.py 89.86% <100.00%> (+0.35%) ⬆️
src/sentry/models/useremail.py 98.14% <100.00%> (+0.27%) ⬆️
src/sentry/testutils/helpers/backups.py 97.52% <100.00%> (+0.02%) ⬆️

... and 28 files with indirect coverage changes

@azaslavsky azaslavsky force-pushed the azaslavsky/backup/relocation_scope_in_dependencies branch from 43a3961 to d1feaf1 Compare August 18, 2023 22:09
Base automatically changed from azaslavsky/backup/relocation_scope_in_dependencies to master August 18, 2023 22:38
@azaslavsky azaslavsky force-pushed the azaslavsky/backup/relocation_logic branch from 0eab32a to 3779534 Compare August 28, 2023 17:38
@azaslavsky azaslavsky marked this pull request as ready for review August 28, 2023 17:39
@azaslavsky azaslavsky requested a review from a team August 28, 2023 17:39
We've accumulated a decent number of special cases for how we do
relocation, what with Actors and Teams having circular dependencies,
Users auto-creating their UserEmails, the need to sanitize out dangerous
models like UserPermissions, etc, etc, etc.

This is a simple refactoring change that uses a roughly "typeclass"-ish
pattern: we define two new methods on `BaseModel`,
`_normalize_before_relocation_import` to transform the data (no
transform by default) and `write_relocation_import` to actually save it
to the database. Downstream models that need special logic at either of
these stages are then free overwrite one or both of these steps as they
please.

The main benefit of this approach is that this logic lives closer to the
models it affects, rather than in some farflung section of the codebase.
When we make changes to them in the future, relocation logic will be
right there, encouraging folks to remember to change how that works (if
necessary) as well.
@azaslavsky azaslavsky force-pushed the azaslavsky/backup/relocation_logic branch from 4f94278 to 9abb7c0 Compare August 29, 2023 00:01
@azaslavsky azaslavsky merged commit b11cc74 into master Aug 29, 2023
@azaslavsky azaslavsky deleted the azaslavsky/backup/relocation_logic branch August 29, 2023 00:32
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants