78 lines
2.0 KiB
Ruby
78 lines
2.0 KiB
Ruby
# typed: strict
|
|
# frozen_string_literal: true
|
|
|
|
class MigrateFaPostsToAux < ActiveRecord::Migration[7.2]
|
|
sig { void }
|
|
def up
|
|
cols = [
|
|
[:state, :string, {}],
|
|
[:title, :string, {}],
|
|
[:fa_id, :integer, {}],
|
|
[:category, :string, {}],
|
|
[:theme, :string, {}],
|
|
[:species, :string, {}],
|
|
[:gender, :string, {}],
|
|
[:description, :string, {}],
|
|
[:keywords, :jsonb, { default: [] }],
|
|
[:num_favorites, :integer, {}],
|
|
[:num_comments, :integer, {}],
|
|
[:num_views, :integer, {}],
|
|
[:scanned_at, :timestamp, {}],
|
|
[:scan_file_error, :string, {}],
|
|
[: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_id, :integer, {}],
|
|
[:tried_from_fur_archiver, :boolean, { default: false }],
|
|
[:tried_from_tor, :boolean, { default: false }],
|
|
]
|
|
|
|
create_aux_table :domain_posts, :fa 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"
|
|
else
|
|
type =
|
|
case type
|
|
when :string
|
|
"text"
|
|
else
|
|
type.to_s
|
|
end
|
|
"(json_attributes->>'#{name}')::#{type} as #{name}"
|
|
end
|
|
end
|
|
|
|
sql = <<~SQL
|
|
INSERT INTO domain_posts_fa_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::FaPost'
|
|
SQL
|
|
|
|
execute sql
|
|
end
|
|
|
|
sig { void }
|
|
def down
|
|
drop_table :domain_posts_fa_aux
|
|
end
|
|
end
|