migrate ib posts to aux table

This commit is contained in:
Dylan Knutson
2025-07-27 17:54:29 +00:00
parent 1f44ec2fa2
commit c43d1ca197
9 changed files with 255 additions and 94 deletions

View File

@@ -0,0 +1,72 @@
# typed: strict
# frozen_string_literal: true
class MigrateIbPostDataToAux < ActiveRecord::Migration[7.2]
sig { void }
def up
cols = [
[:ib_id, :integer, { index: true }],
[:state, :string, {}],
[:rating, :string, {}],
[:submission_type, :string, {}],
[:title, :string, {}],
[:writing, :string, {}],
[:description, :string, {}],
[:num_views, :integer, {}],
[:num_files, :integer, {}],
[:num_favs, :integer, {}],
[:num_comments, :integer, {}],
[:keywords, :jsonb, { default: [] }],
[:last_file_updated_at, :timestamp, {}],
[
:deep_update_log_entry,
:references,
{ foreign_key: { to_table: :http_log_entries }, index: false },
],
[
:shallow_update_log_entry,
:references,
{ foreign_key: { to_table: :http_log_entries }, index: false },
],
[:shallow_updated_at, :timestamp, {}],
[:deep_updated_at, :timestamp, {}],
[:ib_detail_raw, :jsonb, {}],
]
create_aux_table :domain_posts, :ib do |t|
cols.each { |name, type, opts| t.send(type, name, **opts) }
end
col_names =
cols.map do |name, type, opts|
type == :references ? "#{name}_id" : "#{name}"
end
col_selects =
cols.map do |name, type, opts|
if type == :references
"(json_attributes->>'#{name}_id')::integer as #{name}_id"
elsif type == :string
"(json_attributes->>'#{name}')::text as #{name}"
else
"(json_attributes->>'#{name}')::#{type} as #{name}"
end
end
execute <<~SQL
INSERT INTO domain_posts_ib_aux (
base_table_id,
#{col_names.join(",\n ")}
)
SELECT
id as base_table_id,
#{col_selects.join(",\n ")}
FROM domain_posts WHERE type = 'Domain::Post::InkbunnyPost'
SQL
end
sig { void }
def down
drop_table :domain_posts_ib_aux
end
end

View File

@@ -1441,6 +1441,52 @@ CREATE SEQUENCE public.domain_posts_fa_aux_base_table_id_seq
ALTER SEQUENCE public.domain_posts_fa_aux_base_table_id_seq OWNED BY public.domain_posts_fa_aux.base_table_id;
--
-- Name: domain_posts_ib_aux; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.domain_posts_ib_aux (
base_table_id bigint NOT NULL,
ib_id integer,
state character varying,
rating character varying,
submission_type character varying,
title character varying,
writing character varying,
description character varying,
num_views integer,
num_files integer,
num_favs integer,
num_comments integer,
keywords jsonb DEFAULT '[]'::jsonb,
last_file_updated_at timestamp without time zone,
deep_update_log_entry_id bigint,
shallow_update_log_entry_id bigint,
shallow_updated_at timestamp without time zone,
deep_updated_at timestamp without time zone,
ib_detail_raw jsonb
);
--
-- Name: domain_posts_ib_aux_base_table_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.domain_posts_ib_aux_base_table_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: domain_posts_ib_aux_base_table_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.domain_posts_ib_aux_base_table_id_seq OWNED BY public.domain_posts_ib_aux.base_table_id;
--
-- Name: domain_posts_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
@@ -2878,6 +2924,13 @@ ALTER TABLE ONLY public.domain_posts_e621_aux ALTER COLUMN base_table_id SET DEF
ALTER TABLE ONLY public.domain_posts_fa_aux ALTER COLUMN base_table_id SET DEFAULT nextval('public.domain_posts_fa_aux_base_table_id_seq'::regclass);
--
-- Name: domain_posts_ib_aux base_table_id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.domain_posts_ib_aux ALTER COLUMN base_table_id SET DEFAULT nextval('public.domain_posts_ib_aux_base_table_id_seq'::regclass);
--
-- Name: domain_twitter_tweets id; Type: DEFAULT; Schema: public; Owner: -
--
@@ -3083,6 +3136,14 @@ ALTER TABLE ONLY public.domain_posts_fa_aux
ADD CONSTRAINT domain_posts_fa_aux_pkey PRIMARY KEY (base_table_id);
--
-- Name: domain_posts_ib_aux domain_posts_ib_aux_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.domain_posts_ib_aux
ADD CONSTRAINT domain_posts_ib_aux_pkey PRIMARY KEY (base_table_id);
--
-- Name: domain_posts domain_posts_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
@@ -4040,6 +4101,20 @@ CREATE INDEX index_domain_posts_fa_aux_on_base_table_id ON public.domain_posts_f
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_ib_aux_on_base_table_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_domain_posts_ib_aux_on_base_table_id ON public.domain_posts_ib_aux USING btree (base_table_id);
--
-- Name: index_domain_posts_ib_aux_on_ib_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_domain_posts_ib_aux_on_ib_id ON public.domain_posts_ib_aux USING btree (ib_id);
--
-- Name: index_domain_posts_on_posted_at; Type: INDEX; Schema: public; Owner: -
--
@@ -5111,6 +5186,14 @@ ALTER TABLE ONLY public.domain_users_inkbunny_aux
ADD CONSTRAINT fk_rails_304ea0307f FOREIGN KEY (base_table_id) REFERENCES public.domain_users(id);
--
-- Name: domain_posts_ib_aux fk_rails_3762390d41; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.domain_posts_ib_aux
ADD CONSTRAINT fk_rails_3762390d41 FOREIGN KEY (shallow_update_log_entry_id) REFERENCES public.http_log_entries(id);
--
-- Name: http_log_entries fk_rails_42f35e9da0; Type: FK CONSTRAINT; Schema: public; Owner: -
--
@@ -5135,6 +5218,14 @@ 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_ib_aux fk_rails_5ee2c344bd; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.domain_posts_ib_aux
ADD CONSTRAINT fk_rails_5ee2c344bd FOREIGN KEY (deep_update_log_entry_id) REFERENCES public.http_log_entries(id);
--
-- Name: domain_twitter_medias fk_rails_5fffa41fa6; Type: FK CONSTRAINT; Schema: public; Owner: -
--
@@ -5239,6 +5330,14 @@ ALTER TABLE ONLY public.domain_users_e621_aux
ADD CONSTRAINT fk_rails_b5bacbced6 FOREIGN KEY (base_table_id) REFERENCES public.domain_users(id);
--
-- Name: domain_posts_ib_aux fk_rails_b94b311254; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.domain_posts_ib_aux
ADD CONSTRAINT fk_rails_b94b311254 FOREIGN KEY (base_table_id) REFERENCES public.domain_posts(id);
--
-- Name: domain_posts_fa_aux fk_rails_be2be2e955; Type: FK CONSTRAINT; Schema: public; Owner: -
--
@@ -5350,6 +5449,7 @@ ALTER TABLE ONLY public.domain_twitter_tweets
SET search_path TO "$user", public;
INSERT INTO "schema_migrations" (version) VALUES
('20250727173100'),
('20250726051748'),
('20250726051451'),
('20250725192431'),