migrate fa posts to aux table

This commit is contained in:
Dylan Knutson
2025-07-26 05:39:32 +00:00
parent ca4729f7d1
commit bc4143ae12
9 changed files with 51 additions and 103 deletions

View File

@@ -77,8 +77,9 @@ class Domain::PostsController < DomainController
@user = T.must(@user)
authorize @user
@posts = @user.faved_posts.with_user_post_fav
# raise @posts.to_sql
@posts = @user.faved_posts
@post_favs =
Domain::UserPostFav.where(user: @user, post: @posts).index_by(&:post_id)
# Apply pagination through posts_relation
@posts = posts_relation(@posts, skip_ordering: true)

View File

@@ -97,33 +97,6 @@ class Domain::Post < ReduxApplicationRecord
source: :user
end
# special purpose association for preloading user_post_favs
belongs_to :user_post_fav,
class_name: "::Domain::UserPostFav",
foreign_key: %i[user_id id],
primary_key: %i[user_id post_id],
optional: true
scope :with_user_post_fav,
-> do
self
.reorder("")
.includes(:user_post_fav)
.select("#{Domain::Post.table_name}.*")
.select("#{Domain::UserPostFav.table_name}.user_id as user_id")
.select(
"(#{Domain::UserPostFav.table_name}.json_attributes->>'fav_id')::integer as fav_id",
)
.order(
# if there is no fav_id, use post_order_attribute to put the post after
# the post with the next highest post_order_attribute
# otherwise sort by fav_id, then post_order_attribute
Arel.sql(
"fav_id DESC, (#{Domain::Post.table_name}.json_attributes->>'#{self.post_order_attribute}')::integer DESC",
),
)
end
has_many :user_post_favs,
class_name: "::Domain::UserPostFav",
inverse_of: :post,

View File

@@ -12,7 +12,7 @@ class Domain::Post::FaPost < Domain::Post
has_single_creator! Domain::User::FaUser
has_faving_users! Domain::User::FaUser
after_initialize { self.state ||= "ok" }
after_initialize { self.state ||= "ok" if new_record? }
enum :state,
{

View File

@@ -52,7 +52,8 @@
<% end %>
<% end %>
<span class="flex-grow text-right">
<% if (faved_at = post.user_post_fav&.faved_at) && (time = faved_at.time) %>
<% user_post_fav = @post_favs && @post_favs[post.id] %>
<% if (faved_at = user_post_fav&.faved_at) && (time = faved_at.time) %>
<span class="flex items-center gap-1 justify-end">
<span
title="<%= time&.in_time_zone&.strftime("%Y-%m-%d %I:%M:%S %p %Z") %>"

View File

@@ -1,5 +1,6 @@
<%# nasty hack, otherwise postgres uses a bad query plan %>
<% fav_posts = user.faved_posts.with_user_post_fav.includes(:creator).limit(5) %>
<% fav_posts = user.faved_posts.includes(:creator).limit(5) %>
<% post_favs = Domain::UserPostFav.where(user: user, post: fav_posts).index_by(&:post_id) %>
<section class="animated-shadow-sky sky-section">
<h2 class="section-header">
<span class="font-medium text-slate-900">Favorited Posts</span>
@@ -20,7 +21,8 @@
}
) %>
<span class="whitespace-nowrap flex-grow text-right text-slate-500">
<% if (faved_at = post.user_post_fav&.faved_at) && (time = faved_at.time) %>
<% user_post_fav = post_favs[post.id] %>
<% if (faved_at = user_post_fav&.faved_at) && (time = faved_at.time) %>
<%= time_ago_in_words(faved_at.time) %> ago
<% else %>
unknown

View File

@@ -19,33 +19,13 @@ class MigrateFaPostsToAux < ActiveRecord::Migration[7.2]
[:num_views, :integer, {}],
[:scanned_at, :timestamp, {}],
[:scan_file_error, :string, {}],
[
:last_user_page,
:references,
{ foreign_key: { to_table: :http_log_entries }, index: false },
],
[
:first_browse_page,
:references,
{ foreign_key: { to_table: :http_log_entries }, index: false },
],
[
:first_gallery_page,
:references,
{ foreign_key: { to_table: :http_log_entries }, index: false },
],
[
:first_seen_entry,
:references,
{ foreign_key: { to_table: :http_log_entries }, index: false },
],
[:last_user_page_id, :integer, {}],
[:first_browse_page_id, :integer, {}],
[:first_gallery_page_id, :integer, {}],
[:first_seen_entry_id, :integer, {}],
[:fuzzysearch_checked_at, :timestamp, {}],
[:fuzzysearch_json, :jsonb, {}],
[
:fuzzysearch_entry,
:references,
{ foreign_key: { to_table: :http_log_entries }, index: false },
],
[:fuzzysearch_entry_id, :integer, {}],
[:tried_from_fur_archiver, :boolean, { default: false }],
[:tried_from_tor, :boolean, { default: false }],
]

View File

@@ -0,0 +1,5 @@
class CreateIndexOnFaId < ActiveRecord::Migration[7.2]
def change
add_index :domain_posts_fa_aux, :fa_id
end
end

View File

@@ -0,0 +1,17 @@
class MakeIndexUniqueOnFaId < ActiveRecord::Migration[7.2]
def change
# make the exisitng index a unique index
add_index :domain_posts_fa_aux,
:fa_id,
name: "index_domain_posts_fa_aux_on_fa_id_unique",
unique: true
remove_index :domain_posts_fa_aux,
:fa_id,
name: "index_domain_posts_fa_aux_on_fa_id"
rename_index :domain_posts_fa_aux,
"index_domain_posts_fa_aux_on_fa_id_unique",
"index_domain_posts_fa_aux_on_fa_id"
end
end

View File

@@ -1410,13 +1410,13 @@ CREATE TABLE public.domain_posts_fa_aux (
num_views integer,
scanned_at timestamp without time zone,
scan_file_error character varying,
last_user_page_id bigint,
first_browse_page_id bigint,
first_gallery_page_id bigint,
first_seen_entry_id bigint,
last_user_page_id integer,
first_browse_page_id integer,
first_gallery_page_id integer,
first_seen_entry_id integer,
fuzzysearch_checked_at timestamp without time zone,
fuzzysearch_json jsonb,
fuzzysearch_entry_id bigint,
fuzzysearch_entry_id integer,
tried_from_fur_archiver boolean DEFAULT false,
tried_from_tor boolean DEFAULT false
);
@@ -4033,6 +4033,13 @@ CREATE INDEX index_domain_posts_e621_aux_on_base_table_id ON public.domain_posts
CREATE INDEX index_domain_posts_fa_aux_on_base_table_id ON public.domain_posts_fa_aux USING btree (base_table_id);
--
-- Name: index_domain_posts_fa_aux_on_fa_id; Type: INDEX; Schema: public; Owner: -
--
CREATE UNIQUE INDEX index_domain_posts_fa_aux_on_fa_id ON public.domain_posts_fa_aux USING btree (fa_id);
--
-- Name: index_domain_posts_on_posted_at; Type: INDEX; Schema: public; Owner: -
--
@@ -5128,14 +5135,6 @@ ALTER TABLE ONLY public.domain_user_user_follows
ADD CONSTRAINT fk_rails_4b2ab65400 FOREIGN KEY (from_id) REFERENCES public.domain_users(id);
--
-- Name: domain_posts_fa_aux fk_rails_5ec6ca402e; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.domain_posts_fa_aux
ADD CONSTRAINT fk_rails_5ec6ca402e FOREIGN KEY (first_seen_entry_id) REFERENCES public.http_log_entries(id);
--
-- Name: domain_twitter_medias fk_rails_5fffa41fa6; Type: FK CONSTRAINT; Schema: public; Owner: -
--
@@ -5184,14 +5183,6 @@ ALTER TABLE ONLY public.domain_user_search_names
ADD CONSTRAINT fk_rails_8475fe75b5 FOREIGN KEY (user_id) REFERENCES public.domain_users(id);
--
-- Name: domain_posts_fa_aux fk_rails_9231b05b1c; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.domain_posts_fa_aux
ADD CONSTRAINT fk_rails_9231b05b1c FOREIGN KEY (first_browse_page_id) REFERENCES public.http_log_entries(id);
--
-- Name: good_job_execution_log_lines_collections fk_rails_98c288034f; Type: FK CONSTRAINT; Schema: public; Owner: -
--
@@ -5216,14 +5207,6 @@ ALTER TABLE ONLY public.domain_posts_e621_aux
ADD CONSTRAINT fk_rails_a90522803d FOREIGN KEY (last_index_page_id) REFERENCES public.http_log_entries(id);
--
-- Name: domain_posts_fa_aux fk_rails_addb1e1118; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.domain_posts_fa_aux
ADD CONSTRAINT fk_rails_addb1e1118 FOREIGN KEY (first_gallery_page_id) REFERENCES public.http_log_entries(id);
--
-- Name: domain_posts_e621_aux fk_rails_ae368c64c2; Type: FK CONSTRAINT; Schema: public; Owner: -
--
@@ -5232,14 +5215,6 @@ ALTER TABLE ONLY public.domain_posts_e621_aux
ADD CONSTRAINT fk_rails_ae368c64c2 FOREIGN KEY (base_table_id) REFERENCES public.domain_posts(id);
--
-- Name: domain_posts_fa_aux fk_rails_ae8beb22db; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.domain_posts_fa_aux
ADD CONSTRAINT fk_rails_ae8beb22db FOREIGN KEY (fuzzysearch_entry_id) REFERENCES public.http_log_entries(id);
--
-- Name: domain_user_user_follows fk_rails_b45e6e3979; Type: FK CONSTRAINT; Schema: public; Owner: -
--
@@ -5328,14 +5303,6 @@ ALTER TABLE ONLY public.domain_post_files
ADD CONSTRAINT fk_rails_d059c07f77 FOREIGN KEY (log_entry_id) REFERENCES public.http_log_entries(id);
--
-- Name: domain_posts_fa_aux fk_rails_d5abc02732; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.domain_posts_fa_aux
ADD CONSTRAINT fk_rails_d5abc02732 FOREIGN KEY (last_user_page_id) REFERENCES public.http_log_entries(id);
--
-- Name: domain_posts_e621_aux fk_rails_d691739802; Type: FK CONSTRAINT; Schema: public; Owner: -
--
@@ -5383,6 +5350,8 @@ ALTER TABLE ONLY public.domain_twitter_tweets
SET search_path TO "$user", public;
INSERT INTO "schema_migrations" (version) VALUES
('20250726051748'),
('20250726051451'),
('20250725192431'),
('20250724213505'),
('20250723194407'),