discourse/app/models/user_export.rb
Bianca Nenciu 9db8f00b3d
FEATURE: Create upload_references table (#16146)
This table holds associations between uploads and other models. This can be used to prevent removing uploads that are still in use.

* DEV: Create upload_references
* DEV: Use UploadReference instead of PostUpload
* DEV: Use UploadReference for SiteSetting
* DEV: Use UploadReference for Badge
* DEV: Use UploadReference for Category
* DEV: Use UploadReference for CustomEmoji
* DEV: Use UploadReference for Group
* DEV: Use UploadReference for ThemeField
* DEV: Use UploadReference for ThemeSetting
* DEV: Use UploadReference for User
* DEV: Use UploadReference for UserAvatar
* DEV: Use UploadReference for UserExport
* DEV: Use UploadReference for UserProfile
* DEV: Add method to extract uploads from raw text
* DEV: Use UploadReference for Draft
* DEV: Use UploadReference for ReviewableQueuedPost
* DEV: Use UploadReference for UserProfile's bio_raw
* DEV: Do not copy user uploads to upload references
* DEV: Copy post uploads again after deploy
* DEV: Use created_at and updated_at from uploads table
* FIX: Check if upload site setting is empty
* DEV: Copy user uploads to upload references
* DEV: Make upload extraction less strict
2022-06-09 09:24:30 +10:00

49 lines
1.3 KiB
Ruby

# frozen_string_literal: true
class UserExport < ActiveRecord::Base
belongs_to :user
belongs_to :upload, dependent: :destroy
belongs_to :topic, dependent: :destroy
has_many :upload_references, as: :target, dependent: :destroy
after_save do
if saved_change_to_upload_id?
UploadReference.ensure_exist!(upload_ids: [self.upload_id], target: self)
end
end
DESTROY_CREATED_BEFORE = 2.days.ago
def self.remove_old_exports
UserExport.where('created_at < ?', DESTROY_CREATED_BEFORE).find_each do |user_export|
UserExport.transaction do
begin
Post.where(topic_id: user_export.topic_id).find_each { |p| p.destroy! }
user_export.destroy!
rescue => e
Rails.logger.warn("Failed to remove user_export record with id #{user_export.id}: #{e.message}\n#{e.backtrace.join("\n")}")
end
end
end
end
def self.base_directory
File.join(Rails.root, "public", "uploads", "csv_exports", RailsMultisite::ConnectionManagement.current_db)
end
end
# == Schema Information
#
# Table name: user_exports
#
# id :integer not null, primary key
# file_name :string not null
# user_id :integer not null
# created_at :datetime not null
# updated_at :datetime not null
# upload_id :integer
# topic_id :integer
#