Skip to content

Commit

Permalink
feat: Deprecate feature toggle environment variants api (#7066)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew authored May 16, 2024
1 parent 9281d6b commit 150a7b3
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/lib/db/feature-environment-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,4 +490,13 @@ export class FeatureEnvironmentStore implements IFeatureEnvironmentStore {
);
}
}

async variantExists(featureName: string): Promise<boolean> {
const result = await this.db.raw(
`SELECT EXISTS (SELECT 1 FROM ${T.featureEnvs} WHERE feature_name = ? AND variants <> '[]'::jsonb) AS present`,
[featureName],
);
const { present } = result.rows[0];
return present;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ beforeAll(async () => {
experimental: {
flags: {
strictSchemaValidation: true,
enableLegacyVariants: true,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ export default class ExportImportService
await Promise.all(
featureEnvsWithVariants.map((featureEnvironment) => {
return featureEnvironment.featureName
? this.featureToggleService.saveVariantsOnEnv(
? this.featureToggleService.legacySaveVariantsOnEnv(
dto.project,
featureEnvironment.featureName,
dto.environment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const createContext = async (context: ContextFieldSchema = defaultContext) => {
};

const createVariants = async (feature: string, variants: IVariant[]) => {
await app.services.featureToggleService.saveVariantsOnEnv(
await app.services.featureToggleService.legacySaveVariantsOnEnv(
DEFAULT_PROJECT,
feature,
DEFAULT_ENV,
Expand Down
34 changes: 34 additions & 0 deletions src/lib/features/feature-toggle/feature-toggle-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2184,13 +2184,47 @@ class FeatureToggleService {
return featureToggle;
}

private async verifyLegacyVariants(featureName: string) {
const existingLegacyVariantsExist =
await this.featureEnvironmentStore.variantExists(featureName);
const enableLegacyVariants = this.flagResolver.isEnabled(
'enableLegacyVariants',
);
const useLegacyVariants =
existingLegacyVariantsExist || enableLegacyVariants;
if (!useLegacyVariants) {
throw new InvalidOperationError(
`Environment variants deprecated for feature: ${featureName}. Use strategy variants instead.`,
);
}
}

async saveVariantsOnEnv(
projectId: string,
featureName: string,
environment: string,
newVariants: IVariant[],
auditUser: IAuditUser,
oldVariants?: IVariant[],
): Promise<IVariant[]> {
await this.verifyLegacyVariants(featureName);
return this.legacySaveVariantsOnEnv(
projectId,
featureName,
environment,
newVariants,
auditUser,
oldVariants,
);
}

async legacySaveVariantsOnEnv(
projectId: string,
featureName: string,
environment: string,
newVariants: IVariant[],
auditUser: IAuditUser,
oldVariants?: IVariant[],
): Promise<IVariant[]> {
await variantsArraySchema.validateAsync(newVariants);
const fixedVariants = this.fixVariantWeights(newVariants);
Expand Down
2 changes: 2 additions & 0 deletions src/lib/types/stores/feature-environment-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,6 @@ export interface IFeatureEnvironmentStore
): Promise<void>;

clonePreviousVariants(environment: string, project: string): Promise<void>;

variantExists(featureName: string): Promise<boolean>;
}
114 changes: 114 additions & 0 deletions src/test/e2e/api/admin/project/variants-sunset.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import {
type IUnleashTest,
setupAppWithCustomConfig,
} from '../../../helpers/test-helper';
import dbInit, { type ITestDb } from '../../../helpers/database-init';
import getLogger from '../../../../fixtures/no-logger';
import { WeightType } from '../../../../../lib/types/model';

let app: IUnleashTest;
let db: ITestDb;

beforeAll(async () => {
db = await dbInit('project_feature_variants_api_sunset', getLogger);
app = await setupAppWithCustomConfig(db.stores, {
experimental: {
flags: {
strictSchemaValidation: true,
enableLegacyVariants: false,
},
},
});
await db.stores.environmentStore.create({
name: 'development',
type: 'development',
});
});

afterAll(async () => {
await app.destroy();
await db.destroy();
});

beforeEach(async () => {
await db.stores.featureToggleStore.deleteAll();
});

test('Cannot add environment variants to a new feature', async () => {
const featureName = 'feature-variants-patch';

await db.stores.featureToggleStore.create('default', {
name: featureName,
createdByUserId: 9999,
});
await db.stores.featureEnvironmentStore.addEnvironmentToFeature(
featureName,
'development',
true,
);

const patch = [
{
op: 'add',
path: '/0',
value: {
name: 'a',
weightType: WeightType.VARIABLE,
weight: 1000,
},
},
];

await app.request
.patch(
`/api/admin/projects/default/features/${featureName}/environments/development/variants`,
)
.send(patch)
.expect(403)
.expect((res) => {
expect(res.body.message).toBe(
'Environment variants deprecated for feature: feature-variants-patch. Use strategy variants instead.',
);
});
});

test('Can add environment variants when existing ones exist for this feature', async () => {
const featureName = 'feature-variants-patch';

await db.stores.featureToggleStore.create('default', {
name: featureName,
createdByUserId: 9999,
});
await db.stores.featureEnvironmentStore.addEnvironmentToFeature(
featureName,
'development',
true,
);
await db.stores.featureToggleStore.saveVariants('default', featureName, [
{
name: 'existing-variant',
stickiness: 'default',
weight: 1000,
weightType: WeightType.VARIABLE,
},
]);

const patch = [
{
op: 'add',
path: '/0',
value: {
name: 'a',
weightType: WeightType.VARIABLE,
weight: 1000,
},
},
];

await app.request
.patch(
`/api/admin/projects/default/features/${featureName}/environments/development/variants`,
)
.send(patch)
.expect(200);
});
1 change: 1 addition & 0 deletions src/test/e2e/api/admin/project/variants.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ beforeAll(async () => {
experimental: {
flags: {
strictSchemaValidation: true,
enableLegacyVariants: true,
},
},
});
Expand Down
9 changes: 9 additions & 0 deletions src/test/fixtures/fake-feature-environment-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,13 @@ export default class FakeFeatureEnvironmentStore
features.includes(featureEnv.featureName),
);
}

async variantExists(featureName: string) {
return this.featureEnvironments.some(
(featureEnvironment) =>
featureEnvironment.featureName === featureName &&
featureEnvironment.variants &&
featureEnvironment.variants.length > 0,
);
}
}

0 comments on commit 150a7b3

Please sign in to comment.