mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	feat: do not insert into database stages that already exist (#6964)
Previously when we had thousands of metrics coming in, we were trying to write them all to database and running into on conflict
This commit is contained in:
		
							parent
							
								
									a66b3c65c1
								
							
						
					
					
						commit
						2ba250fa41
					
				| @ -22,29 +22,28 @@ export class FeatureLifecycleStore implements IFeatureLifecycleStore { | ||||
|     async insert( | ||||
|         featureLifecycleStages: FeatureLifecycleStage[], | ||||
|     ): Promise<void> { | ||||
|         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), | ||||
|         ); | ||||
|         const joinedLifecycleStages = featureLifecycleStages | ||||
|             .map((stage) => `('${stage.feature}', '${stage.stage}')`) | ||||
|             .join(', '); | ||||
| 
 | ||||
|         await this.db('feature_lifecycles') | ||||
|             .insert( | ||||
|                 validStages.map((stage) => ({ | ||||
|                     feature: stage.feature, | ||||
|                     stage: stage.stage, | ||||
|                 })), | ||||
|         const query = this.db | ||||
|             .with( | ||||
|                 'new_stages', | ||||
|                 this.db.raw(` | ||||
|                     SELECT v.feature, v.stage | ||||
|                     FROM (VALUES ${joinedLifecycleStages}) AS v(feature, stage) | ||||
|                     JOIN features ON features.name = v.feature | ||||
|                     LEFT JOIN feature_lifecycles ON feature_lifecycles.feature = v.feature AND feature_lifecycles.stage = v.stage | ||||
|                     WHERE feature_lifecycles.feature IS NULL AND feature_lifecycles.stage IS NULL | ||||
|                 `),
 | ||||
|             ) | ||||
|             .returning('*') | ||||
|             .insert((query) => { | ||||
|                 query.select('feature', 'stage').from('new_stages'); | ||||
|             }) | ||||
|             .into('feature_lifecycles') | ||||
|             .onConflict(['feature', 'stage']) | ||||
|             .ignore(); | ||||
|         await query; | ||||
|     } | ||||
| 
 | ||||
|     async get(feature: string): Promise<FeatureLifecycleView> { | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user