mirror of
https://github.com/discourse/discourse.git
synced 2024-11-24 21:50:24 +08:00
9db8f00b3d
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
64 lines
1.7 KiB
Ruby
64 lines
1.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class UploadReference < ActiveRecord::Base
|
|
belongs_to :upload
|
|
belongs_to :target, polymorphic: true
|
|
|
|
def self.ensure_exist!(upload_ids: [], target: nil, target_type: nil, target_id: nil)
|
|
raise "target OR target_type and target_id are required" if !target && !(target_type && target_id)
|
|
|
|
if target.present?
|
|
target_type = target.class
|
|
target_id = target.id
|
|
end
|
|
|
|
upload_ids = upload_ids.uniq.reject(&:blank?)
|
|
target_type = target_type.to_s
|
|
|
|
if upload_ids.empty?
|
|
UploadReference
|
|
.where(target_type: target_type, target_id: target_id)
|
|
.delete_all
|
|
|
|
return
|
|
end
|
|
|
|
rows = upload_ids.map do |upload_id|
|
|
{
|
|
upload_id: upload_id,
|
|
target_type: target_type,
|
|
target_id: target_id,
|
|
created_at: Time.zone.now,
|
|
updated_at: Time.zone.now,
|
|
}
|
|
end
|
|
|
|
UploadReference.transaction do |transaction|
|
|
UploadReference
|
|
.where(target_type: target_type, target_id: target_id)
|
|
.where.not(upload_id: upload_ids)
|
|
.delete_all
|
|
|
|
UploadReference.insert_all(rows)
|
|
end
|
|
end
|
|
end
|
|
|
|
# == Schema Information
|
|
#
|
|
# Table name: upload_references
|
|
#
|
|
# id :bigint not null, primary key
|
|
# upload_id :bigint not null
|
|
# target_type :string not null
|
|
# target_id :bigint not null
|
|
# created_at :datetime not null
|
|
# updated_at :datetime not null
|
|
#
|
|
# Indexes
|
|
#
|
|
# index_upload_references_on_target (target_type,target_id)
|
|
# index_upload_references_on_upload_and_target (upload_id,target_type,target_id) UNIQUE
|
|
# index_upload_references_on_upload_id (upload_id)
|
|
#
|