migrate e621 favs to own table

This commit is contained in:
Dylan Knutson
2025-08-20 22:10:57 +00:00
parent 6381067235
commit 8bd6c4b2ae
22 changed files with 1710 additions and 55 deletions

View File

@@ -121,9 +121,11 @@ class Domain::E621::Job::ScanUserFavsJob < Domain::E621::Job::Base
logger.info "upserting #{post_ids.size} favs"
post_ids.each_slice(1000) do |slice|
ReduxApplicationRecord.transaction do
Domain::UserPostFav.upsert_all(
slice.map { |post_id| { user_id: user.id, post_id: post_id } },
unique_by: :index_domain_user_post_favs_on_user_id_and_post_id,
Domain::UserPostFav::E621UserPostFav.upsert_all(
slice.map do |post_id|
{ user_id: user.id, post_id: post_id, removed: false }
end,
unique_by: %i[user_id post_id],
)
end
end

View File

@@ -3,7 +3,7 @@ class Domain::Post::E621Post < Domain::Post
aux_table :e621
has_single_file!
has_faving_users! Domain::User::E621User
has_faving_users! Domain::User::E621User, Domain::UserPostFav::E621UserPostFav
belongs_to_groups! :pools,
Domain::PostGroup::E621Pool,
Domain::PostGroupJoin::E621PoolJoin

View File

@@ -147,9 +147,30 @@ class Domain::User < ReduxApplicationRecord
class_name: klass.name
end
sig { params(klass: T.class_of(Domain::Post)).void }
def self.has_faved_posts!(klass)
sig do
params(
klass: T.class_of(Domain::Post),
fav_model_type: T.class_of(Domain::UserPostFav),
fav_model_order: T.untyped,
).void
end
def self.has_faved_posts!(
klass,
fav_model_type = Domain::UserPostFav,
fav_model_order: nil
)
self.class_has_faved_posts = klass
has_many :user_post_favs,
-> do
rel = extending(CounterCacheWithFallback[:user_post_favs])
rel = rel.order(fav_model_order) if fav_model_order
rel
end,
class_name: fav_model_type.name,
inverse_of: :user,
dependent: :destroy
has_many :faved_posts,
-> { order(klass.param_order_attribute => :desc) },
through: :user_post_favs,

View File

@@ -17,7 +17,7 @@ class Domain::User::E621User < Domain::User
validates :e621_id, presence: true
validates :name, length: { minimum: 1 }, allow_nil: false
has_faved_posts! Domain::Post::E621Post
has_faved_posts! Domain::Post::E621Post, Domain::UserPostFav::E621UserPostFav
sig { override.returns([String, Symbol]) }
def self.param_prefix_and_attribute

View File

@@ -29,7 +29,11 @@ class Domain::User::FaUser < Domain::User
has_followed_users! Domain::User::FaUser
has_followed_by_users! Domain::User::FaUser
has_created_posts! Domain::Post::FaPost
has_faved_posts! Domain::Post::FaPost
has_faved_posts! Domain::Post::FaPost,
Domain::UserPostFav::FaUserPostFav,
fav_model_order: {
fa_fav_id: :desc,
}
enum :state,
{ ok: "ok", account_disabled: "account_disabled", error: "error" },

View File

@@ -16,6 +16,18 @@ class Domain::UserPostFav < ReduxApplicationRecord
belongs_to :post, class_name: "Domain::Post", inverse_of: :user_post_favs
sig { params(user_klass: T.class_of(Domain::User), post_klass: T.class_of(Domain::Post)).void }
def self.user_post_fav_relationships(user_klass, post_klass)
belongs_to_with_counter_cache :user,
class_name: user_klass.name,
inverse_of: :user_post_favs,
counter_cache: :user_post_favs_count
belongs_to :post,
class_name: post_klass.name,
inverse_of: :user_post_favs
end
scope :for_post_type,
->(post_klass) do
post_klass = T.cast(post_klass, T.class_of(Domain::Post))

View File

@@ -0,0 +1,7 @@
# typed: strict
# frozen_string_literal: true
class Domain::UserPostFav::E621UserPostFav < Domain::UserPostFav
self.table_name = "domain_user_post_favs_e621"
user_post_fav_relationships Domain::User::E621User, Domain::Post::E621Post
end

View File

@@ -1,24 +1,11 @@
# typed: strict
class Domain::UserPostFav::FaUserPostFav < Domain::UserPostFav
self.table_name = "domain_user_post_favs_fa"
belongs_to_with_counter_cache :user,
class_name: "Domain::User::FaUser",
inverse_of: :user_post_favs,
counter_cache: :user_post_favs_count
belongs_to :post,
class_name: "Domain::Post::FaPost",
inverse_of: :user_post_favs
user_post_fav_relationships Domain::User::FaUser, Domain::Post::FaPost
scope :with_explicit_time_and_id,
-> { where.not(explicit_time: nil).where.not(fa_fav_id: nil) }
scope :with_inferred_time_and_id,
-> { where.not(inferred_time: nil).where.not(fa_fav_id: nil) }
scope :with_fa_fav_id, -> { where.not(fa_fav_id: nil) }
validates :fa_fav_id, uniqueness: true, if: :fa_fav_id?
before_save :set_inferred_time