Files
redux-scraper/db/migrate/20250727173100_migrate_ib_post_data_to_aux.rb
2025-07-27 17:54:29 +00:00

73 lines
1.9 KiB
Ruby

# 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