1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-09 00:18:00 +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:
Jaanus Sellin 2024-04-30 09:07:20 +03:00 committed by GitHub
parent a66b3c65c1
commit 2ba250fa41
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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> {