Skip to content

Commit 7700872

Browse files
authored
Merge pull request #357 from hackclub/fix-ahoy-backfill-job-model-naming
fix(ahoy): fix incorrect insert of backfilled ahoy...
2 parents 30896d2 + 7e6d306 commit 7700872

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

app/jobs/ahoy_backfill_project_created_job.rb

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@ class AhoyBackfillProjectCreatedJob < ApplicationJob
44
queue_as :default
55

66
def perform
7-
result = ActiveRecord::Base.connection.execute(<<~SQL)
8-
INSERT INTO ahoy_events (name, user_id, time, properties)
9-
SELECT 'project_created', pm.user_id, MIN(pm.created_at), '{"source":"backfill"}'::jsonb
10-
FROM project_memberships pm
11-
WHERE NOT EXISTS (
12-
SELECT 1 FROM ahoy_events ae
13-
WHERE ae.user_id = pm.user_id AND ae.name = 'project_created'
14-
)
15-
GROUP BY pm.user_id
16-
SQL
7+
already_tracked = Ahoy::Event
8+
.where(name: "project_created")
9+
.where.not(user_id: nil)
10+
.distinct
11+
.pluck(:user_id)
1712

18-
Rails.logger.info("[AhoyBackfillProjectCreated] backfilled #{result.cmd_tuples} events")
13+
missing = Project::Membership
14+
.where.not(user_id: already_tracked)
15+
.group(:user_id)
16+
.minimum(:created_at)
17+
18+
return if missing.empty?
19+
20+
rows = missing.map do |user_id, first_project_at|
21+
{ name: "project_created", user_id: user_id, time: first_project_at, properties: { source: "backfill" } }
22+
end
23+
24+
Ahoy::Event.insert_all(rows)
25+
26+
Rails.logger.info("[AhoyBackfillProjectCreated] backfilled #{rows.size} events")
1927
end
2028
end

0 commit comments

Comments
 (0)