diff --git a/src/lib/features/feature-lifecycle/feature-lifecycle-store.ts b/src/lib/features/feature-lifecycle/feature-lifecycle-store.ts index f4d44446dc..1a8cc13ca0 100644 --- a/src/lib/features/feature-lifecycle/feature-lifecycle-store.ts +++ b/src/lib/features/feature-lifecycle/feature-lifecycle-store.ts @@ -22,9 +22,22 @@ export class FeatureLifecycleStore implements IFeatureLifecycleStore { async insert( featureLifecycleStages: FeatureLifecycleStage[], ): Promise { + const existingFeatures = await this.db('features') + .select('name') + .whereIn( + 'name', + featureLifecycleStages.map((stage) => stage.feature), + ); + const existingFeaturesSet = new Set( + existingFeatures.map((item) => item.name), + ); + const validStages = featureLifecycleStages.filter((stage) => + existingFeaturesSet.has(stage.feature), + ); + await this.db('feature_lifecycles') .insert( - featureLifecycleStages.map((stage) => ({ + validStages.map((stage) => ({ feature: stage.feature, stage: stage.stage, })), diff --git a/src/lib/features/feature-lifecycle/feature-lifecycle.e2e.test.ts b/src/lib/features/feature-lifecycle/feature-lifecycle.e2e.test.ts index f9b33fd446..5f2e25a52c 100644 --- a/src/lib/features/feature-lifecycle/feature-lifecycle.e2e.test.ts +++ b/src/lib/features/feature-lifecycle/feature-lifecycle.e2e.test.ts @@ -87,7 +87,12 @@ test('should return lifecycle stages', async () => { await reachedStage('initial'); await expectFeatureStage('initial'); eventBus.emit(CLIENT_METRICS, { - bucket: { toggles: { my_feature_a: 'irrelevant' } }, + bucket: { + toggles: { + my_feature_a: 'irrelevant', + non_existent_feature: 'irrelevent', + }, + }, environment: 'default', }); // missing feature