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:
parent
a66b3c65c1
commit
2ba250fa41
@ -22,29 +22,28 @@ export class FeatureLifecycleStore implements IFeatureLifecycleStore {
|
|||||||
async insert(
|
async insert(
|
||||||
featureLifecycleStages: FeatureLifecycleStage[],
|
featureLifecycleStages: FeatureLifecycleStage[],
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const existingFeatures = await this.db('features')
|
const joinedLifecycleStages = featureLifecycleStages
|
||||||
.select('name')
|
.map((stage) => `('${stage.feature}', '${stage.stage}')`)
|
||||||
.whereIn(
|
.join(', ');
|
||||||
'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')
|
const query = this.db
|
||||||
.insert(
|
.with(
|
||||||
validStages.map((stage) => ({
|
'new_stages',
|
||||||
feature: stage.feature,
|
this.db.raw(`
|
||||||
stage: stage.stage,
|
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'])
|
.onConflict(['feature', 'stage'])
|
||||||
.ignore();
|
.ignore();
|
||||||
|
await query;
|
||||||
}
|
}
|
||||||
|
|
||||||
async get(feature: string): Promise<FeatureLifecycleView> {
|
async get(feature: string): Promise<FeatureLifecycleView> {
|
||||||
|
Loading…
Reference in New Issue
Block a user