Repository navigation
Enforce deletion permissions and state rules across single and batch APIs #3421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bcb37
wants to merge
3
commits into
dev
Choose a base branch
from
feature/3326-enforce-deletion-policy
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+851
−229
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 63 additions & 4 deletions
67
packages/backend/src/api/repositories/DeletionRepository.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,84 @@ | ||
| import { EntityManager, Repository } from 'typeorm'; | ||
| import { BatchDeleteEntity } from 'upgrade_types'; | ||
| import { | ||
| BatchDeleteEntity, | ||
| EXPERIMENT_STATE, | ||
| FEATURE_FLAG_STATUS, | ||
| SEGMENT_TYPE, | ||
| STANDARD_LIST_TYPE, | ||
| } from 'upgrade_types'; | ||
| import { EntityRepository } from '../../typeorm-typedi-extensions'; | ||
| import { Experiment } from '../models/Experiment'; | ||
| import { FeatureFlag } from '../models/FeatureFlag'; | ||
| import { Segment } from '../models/Segment'; | ||
| import repositoryError from './utils/repositoryError'; | ||
|
|
||
| /** The locked fields the deletion policy evaluates. Only the fields for the target's entity type are set. */ | ||
| export interface DeletionTarget { | ||
| id: string; | ||
| state?: EXPERIMENT_STATE; | ||
| status?: FEATURE_FLAG_STATUS; | ||
| type?: SEGMENT_TYPE; | ||
| } | ||
|
|
||
| const policyFields = { | ||
| experiments: { id: true, state: true }, | ||
| flags: { id: true, status: true }, | ||
| segments: { id: true, type: true }, | ||
| }; | ||
|
|
||
| /** All reads and locks use the caller's deletion transaction, never the repository's default manager. */ | ||
| @EntityRepository() | ||
| export class DeletionRepository extends Repository<DeletionRepository> { | ||
| public async findForDeletion( | ||
| entity: BatchDeleteEntity, | ||
| id: string, | ||
| manager: EntityManager | ||
| ): Promise<{ id: string } | null> { | ||
| ): Promise<DeletionTarget | null> { | ||
| const target = { experiments: Experiment, flags: FeatureFlag, segments: Segment }[entity]; | ||
| return manager | ||
| .getRepository<{ id: string }>(target) | ||
| .findOne({ where: { id }, select: { id: true }, lock: { mode: 'pessimistic_write' } }) | ||
| .getRepository<DeletionTarget>(target) | ||
| .findOne({ where: { id }, select: policyFields[entity], lock: { mode: 'pessimistic_write' } }) | ||
| .catch((errorMsg: any) => { | ||
| throw repositoryError('DeletionRepository', 'findForDeletion', { entity, id }, errorMsg); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Whether a segment has the Used status that `SegmentService.getSegmentStatus()` reports, evaluated for one | ||
| * segment instead of every segment. A segment is Used when it, or any segment that contains it, is a list of a | ||
| * non-archived experiment or of any feature flag, or when it is a member of a segment-type list that belongs to | ||
| * another segment. | ||
| */ | ||
| public async isSegmentInUse(segmentId: string, manager: EntityManager): Promise<boolean> { | ||
| const rows: { used: boolean }[] = await manager | ||
| .query( | ||
| `WITH RECURSIVE containing ("id") AS ( | ||
| SELECT $1::uuid | ||
| UNION | ||
| SELECT sfs."parentSegmentId" FROM "segment_for_segment" sfs JOIN containing c ON sfs."childSegmentId" = c."id" | ||
| ) | ||
| SELECT ( | ||
| EXISTS ( | ||
| SELECT 1 FROM "experiment_segment_inclusion" esi JOIN "experiment" e ON e."id" = esi."experimentId" | ||
| WHERE esi."segmentId" IN (SELECT "id" FROM containing) AND e."state" <> $2 | ||
| ) | ||
|
Copilot marked this conversation as resolved.
Resolved
|
||
| OR EXISTS ( | ||
| SELECT 1 FROM "experiment_segment_exclusion" ese JOIN "experiment" e ON e."id" = ese."experimentId" | ||
| WHERE ese."segmentId" IN (SELECT "id" FROM containing) AND e."state" <> $2 | ||
| ) | ||
| OR EXISTS (SELECT 1 FROM "feature_flag_segment_inclusion" WHERE "segmentId" IN (SELECT "id" FROM containing)) | ||
| OR EXISTS (SELECT 1 FROM "feature_flag_segment_exclusion" WHERE "segmentId" IN (SELECT "id" FROM containing)) | ||
| OR EXISTS ( | ||
| SELECT 1 FROM "segment_for_segment" m JOIN "segment" l ON l."id" = m."parentSegmentId" | ||
| WHERE m."childSegmentId" = $1 AND l."listType" = $3 | ||
| AND EXISTS (SELECT 1 FROM "segment_for_segment" o WHERE o."childSegmentId" = l."id") | ||
| ) | ||
| ) AS "used"`, | ||
| [segmentId, EXPERIMENT_STATE.ARCHIVED, STANDARD_LIST_TYPE.SEGMENT] | ||
| ) | ||
| .catch((errorMsg: any) => { | ||
| throw repositoryError('DeletionRepository', 'isSegmentInUse', { segmentId }, errorMsg); | ||
| }); | ||
| return rows[0]?.used === true; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Execution failures stop the remaining items."
I guess this is pre-existing and i didn't catch it before, but I don't think one error should stop the execution, other items could delete successfully and there's no reason i can think of to stop that from happening, is there? It can return the non-delete reason and the UI can show the reason for items that didn't delete.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's unlikely that this will ever happen from a UI interaction, since batch deletes are disabled as long as at least one item isn't deletable. But, yeah, I figured for an API call (like one made by load tests) it makes sense to delete what's deletable and report what isn't.