-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding
author
field to fact-checks and explainers (#2217)
Currently, our system only stores and exposes the last user who updated an article (fact-check or explainer). This PR aims to store and expose the original creator of the article in the database and make it available via the GraphQL API. - Database: Add a new column (author_id) to store the user who originally created an article (tables fact_checks and explainers). - Callback: Implement callbacks to set the author automatically when the article is created. - GraphQL API: Update the schema to expose the article creator alongside the last updated user. - Backfill Existing Data: Implement a migration rake task that determines the original creator from version history and populates author_id for existing articles. - Tests: Add automated tests.
- Loading branch information
Showing
12 changed files
with
137 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class AddAuthorToArticles < ActiveRecord::Migration[6.1] | ||
def change | ||
add_reference :fact_checks, :author, index: true | ||
add_reference :explainers, :author, index: true | ||
add_reference :claim_descriptions, :author, index: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
lib/tasks/migrate/20250214142854_set_author_for_articles.rake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# rake check:migrate:set_author_for_articles | ||
ActiveRecord::Base.logger = nil | ||
namespace :check do | ||
namespace :migrate do | ||
task set_author_for_articles: :environment do | ||
started = Time.now.to_i | ||
|
||
# Initially set the author for records that were not changed since they were created | ||
[ClaimDescription, FactCheck, Explainer].each do |model| | ||
query = model.where('created_at = updated_at').where(author_id: nil).order('id ASC') | ||
puts "[#{Time.now}] Total of instances of model #{model} to be updated: #{query.count}." | ||
i = 0 | ||
query.in_batches(of: 1000) do |batch| | ||
i += 1 | ||
batch.update_all('author_id = user_id') | ||
puts "[#{Time.now}] Updated author for batch ##{i} of instances of model #{model}." | ||
end | ||
end | ||
|
||
# For claims and fact-checks, look into the versions table to update the records that were updated since they were created | ||
Team.all.order('id ASC').find_each do |team| | ||
puts "[#{Time.now}] Updating article authors for workspace #{team.slug}..." | ||
claims = ClaimDescription.where(author_id: nil, team_id: team.id).order('id ASC') | ||
claims_count = claims.count | ||
puts "[#{Time.now}] Total of claims to be updated: #{claims_count}." | ||
i = 0 | ||
claims.find_each do |claim| | ||
i += 1 | ||
|
||
# Update claim | ||
claim_author = Version.from_partition(team.id).where(item_type: 'ClaimDescription', event: 'create', item_id: claim.id).last&.whodunnit | ||
# Skip if no author found from the versions table | ||
if claim_author.to_i == 0 | ||
puts "[#{Time.now}] Skipping claim #{claim.id} because no author was found from the versions table." | ||
else | ||
claim.update_column(:author_id, claim_author.to_i) | ||
puts "[#{Time.now}] [#{i}/#{claims_count}] Updated author to be #{claim_author} for claim #{claim.id}." | ||
end | ||
|
||
# Update fact-check | ||
fact_check = claim.fact_check | ||
# Skip if no fact-check found for claim | ||
if fact_check.nil? | ||
puts "[#{Time.now}] [#{i}/#{claims_count}] Skipping, no fact-check found for claim #{claim.id}." | ||
next | ||
end | ||
fact_check_author = Version.from_partition(team.id).where(item_type: 'FactCheck', event: 'create', item_id: fact_check.id).last&.whodunnit | ||
# Skip if no author found from the versions table | ||
if fact_check_author.to_i == 0 | ||
puts "[#{Time.now}] Skipping fact-check #{fact_check.id} because no author was found from the versions table." | ||
else | ||
fact_check.update_column(:author_id, fact_check_author.to_i) | ||
puts "[#{Time.now}] [#{i}/#{claims_count}] Updated author to be #{fact_check_author} for fact-check #{fact_check.id}." | ||
end | ||
end | ||
|
||
puts "[#{Time.now}] Updated article authors for workspace #{team.slug}." | ||
end | ||
|
||
minutes = ((Time.now.to_i - started) / 60).to_i | ||
puts "[#{Time.now}] Done in #{minutes} minutes." | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters