Websites/app/jobs/table_import_job.rb

23 lines
1.4 KiB
Ruby

# frozen_string_literal: true
class TableImportJob < ApplicationJob
queue_as :default
def perform(table, file)
ApplicationRecord.connection.execute("TRUNCATE #{table}")
ActiveRecord::Base.connection_db_config.configuration_hash => { host:, user:, database: }
password = ENV.fetch("WEBSITES_DATABASE_PASSWORD", nil)
system("psql -h #{host} -U #{user} -d #{database} #{password.blank? ? '' : "-p #{password} "}-c \"\\copy #{table} (#{COLUMN_MAP[table.to_sym]}) FROM '#{file}' DELIMITER ',' CSV HEADER;\"", exception: true)
FileUtils.rm(file)
end
COLUMN_MAP = {
"e621.pools": "id,name,created_at,updated_at,creator_id,description,is_active,category,post_ids",
"e621.posts": "id,uploader_id,created_at,md5,source,rating,image_width,image_height,tag_string,locked_tags,fav_count,file_ext,parent_id,change_seq,approver_id,file_size,comment_count,description,duration,updated_at,is_deleted,is_pending,is_flagged,score,up_score,down_score,is_rating_locked,is_status_locked,is_note_locked",
"e621.tag_aliases": "id,antecedent_name,consequent_name,created_at,status",
"e621.tag_implications": "id,antecedent_name,consequent_name,created_at,status",
"e621.tags": "id,name,category,post_count",
"e621.wiki_pages": "id,created_at,updated_at,title,body,creator_id,updater_id,is_locked",
}.freeze
end