1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-07-26 13:48:33 +02:00

chore: use query to get strategies instead of whether a segment is in use (#5375)

This change is just a refactor, removing code that's no longer used. Instead of
checking just whether a segment is in use, we now extract the list of
strategies that use this segment. This is slightly more costly,
perhaps, but it will be necessary for the upcoming implementation.
This commit is contained in:
Thomas Heartman 2023-11-22 07:54:28 +01:00 committed by GitHub
parent 8337885e47
commit 9ac3d7511a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 1 additions and 64 deletions

View File

@ -100,40 +100,6 @@ const updateStrategyInCr = async (
});
};
describe.each([
[
'updateStrategy',
(segmentId: number) =>
updateStrategyInCr(randomId(), segmentId, FLAG_NAME),
],
[
'addStrategy',
(segmentId: number) => addStrategyToCr(segmentId, FLAG_NAME),
],
])('Should handle %s changes correctly', (_, addOrUpdateStrategy) => {
test.each([
['Draft', true],
['In review', true],
['Scheduled', true],
['Approved', true],
['Rejected', false],
['Cancelled', false],
['Applied', false],
])(
'Changes in %s CRs should make it %s',
async (state, expectedOutcome) => {
await createCR(state);
const segmentId = 3;
await addOrUpdateStrategy(segmentId);
expect(
await readModel.isSegmentUsedInActiveChangeRequests(segmentId),
).toBe(expectedOutcome);
},
);
});
test.each([
['Draft', true],
['In review', true],

View File

@ -10,7 +10,6 @@ type ExistingStrategy = NewStrategy & { id: string };
export type ChangeRequestStrategy = NewStrategy | ExistingStrategy;
export interface IChangeRequestSegmentUsageReadModel {
isSegmentUsedInActiveChangeRequests(segmentId: number): Promise<boolean>;
getStrategiesUsedInActiveChangeRequests(
segmentId: number,
): Promise<ChangeRequestStrategy[]>;

View File

@ -6,24 +6,13 @@ import {
export class FakeChangeRequestSegmentUsageReadModel
implements IChangeRequestSegmentUsageReadModel
{
private isSegmentUsedInActiveChangeRequestsValue: boolean;
strategiesUsedInActiveChangeRequests: ChangeRequestStrategy[];
constructor(
isSegmentUsedInActiveChangeRequests = false,
strategiesUsedInActiveChangeRequests = [],
) {
this.isSegmentUsedInActiveChangeRequestsValue =
isSegmentUsedInActiveChangeRequests;
constructor(strategiesUsedInActiveChangeRequests = []) {
this.strategiesUsedInActiveChangeRequests =
strategiesUsedInActiveChangeRequests;
}
public async isSegmentUsedInActiveChangeRequests(): Promise<boolean> {
return this.isSegmentUsedInActiveChangeRequestsValue;
}
public async getStrategiesUsedInActiveChangeRequests(): Promise<
ChangeRequestStrategy[]
> {

View File

@ -12,23 +12,6 @@ export class ChangeRequestSegmentUsageReadModel
constructor(db: Db) {
this.db = db;
}
public async isSegmentUsedInActiveChangeRequests(
segmentId: number,
): Promise<boolean> {
const result = await this.db.raw(
`SELECT events.*
FROM change_request_events events
JOIN change_requests cr ON events.change_request_id = cr.id
WHERE cr.state IN ('Draft', 'In review', 'Scheduled', 'Approved')
AND events.action IN ('updateStrategy', 'addStrategy');`,
);
const isUsed = result.rows.some((row) =>
row.payload?.segments?.includes(segmentId),
);
return isUsed;
}
mapRow = (row): ChangeRequestStrategy => {
const { payload, project, environment, feature } = row;