batch processor for migrating FA user favs

This commit is contained in:
Dylan Knutson
2025-07-11 23:20:44 +00:00
parent c37e3c1024
commit 7a02282893
17 changed files with 266 additions and 136 deletions

View File

@@ -0,0 +1,38 @@
# typed: strict
class AddTypeToDomainUserPostFavs < ActiveRecord::Migration[7.2]
extend T::Sig
sig { void }
def change
mirai_tablespace!
create_enum(
"domain_user_post_fav_type",
%w[Domain::UserPostFav Domain::UserPostFav::FaUserPostFav],
)
change_table :domain_user_post_favs do |t|
t.enum :type,
null: false,
enum_type: "domain_user_post_fav_type",
default: "Domain::UserPostFav"
t.jsonb :json_attributes, default: {}
end
[
["fav_id", "integer", { unique: true }],
%w[inferred_time integer],
%w[explicit_time integer],
].each do |column_name, column_type, options|
add_json_index(
"domain_user_post_favs",
column_name,
column_type,
{ where: "type = 'Domain::UserPostFav::FaUserPostFav'" }.merge(
options || {},
),
)
end
end
end

View File

@@ -1,61 +0,0 @@
# typed: strict
class AddTypeToDomainUserPostFavs < ActiveRecord::Migration[7.2]
extend T::Sig
sig { void }
def change
mirai_tablespace!
create_enum(
"domain_user_post_fav_type",
%w[Domain::UserPostFav Domain::UserPostFav::FaUserPostFav],
)
change_table :domain_user_post_favs do |t|
t.enum :type,
null: false,
enum_type: "domain_user_post_fav_type",
default: "Domain::UserPostFav"
t.jsonb :json_attributes, default: {}
end
[
["fav_id", "integer", { unique: true }],
%w[inferred_time integer],
%w[explicit_time integer],
].each do |column_name, column_type, options|
add_json_index(
"domain_user_post_favs",
column_name,
column_type,
{ where: "type = 'Domain::UserPostFav::FaUserPostFav'" }.merge(
options || {},
),
)
end
up_only { execute <<-SQL }
UPDATE domain_user_post_favs
SET type = 'Domain::UserPostFav::FaUserPostFav'
FROM domain_posts
WHERE domain_posts.id = domain_user_post_favs.post_id
AND domain_posts.type = 'Domain::Post::FaPost'
SQL
# backfill values from domain_fa_fav_id_and_dates
up_only { execute <<-SQL }
UPDATE domain_user_post_favs
SET json_attributes =
domain_user_post_favs.json_attributes
|| jsonb_build_object('fav_id', domain_fa_fav_id_and_dates.fav_fa_id)
|| jsonb_build_object('explicit_time', EXTRACT(EPOCH FROM domain_fa_fav_id_and_dates.date)::integer)
FROM domain_fa_fav_id_and_dates
JOIN domain_posts ON (domain_posts.json_attributes->>'fa_id')::integer = domain_fa_fav_id_and_dates.post_fa_id
WHERE domain_posts.type = 'Domain::Post::FaPost'
AND domain_user_post_favs.post_id = domain_posts.id
AND domain_fa_fav_id_and_dates.user_id = domain_user_post_favs.user_id
AND domain_user_post_favs.type = 'Domain::UserPostFav::FaUserPostFav'
SQL
end
end

View File

@@ -9273,7 +9273,7 @@ ALTER TABLE ONLY public.domain_twitter_tweets
SET search_path TO "$user", public;
INSERT INTO "schema_migrations" (version) VALUES
('20250711014944'),
('20250711014943'),
('20250710204708'),
('20250709235107'),
('20250628000003'),