Skip to content

Commit

Permalink
feat: do not insert into database stages that already exist (#6964)
Browse files Browse the repository at this point in the history
Previously when we had thousands of metrics coming in, we were trying to
write them all to database and running into on conflict
  • Loading branch information
sjaanus committed Apr 30, 2024
1 parent a66b3c6 commit 2ba250f
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions src/lib/features/feature-lifecycle/feature-lifecycle-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down

0 comments on commit 2ba250f

Please sign in to comment.