discourse/app/models/upload_reference.rb
David Taylor 3332e9f4c3
DEV: Always pass --force to annotaterb and reorder annotations (#39977)
`.annotaterb.yml` has carried `classified_sort: true` since the project
switched from `annotate` to `annotaterb` (commit 0eab7daea4, July 2025),
but annotaterb's default behaviour is to compare the existing schema
block against what it would generate and skip the rewrite when the
column list matches — even when the *ordering* of those columns differs.
The result is that models which haven't had a schema change since the
config landed never get reordered, and `classified_sort` drift
accumulates indefinitely.

`--force` makes annotaterb always rewrite, so a single `bin/rake
annotate:clean` run brings every model into the canonical format and
keeps them there. Every schema block is now grouped primary-key →
regular columns → timestamps → foreign keys (alphabetical within each
group). Pure annotation comment change — no code modifications.

Also cleans up the rake task to avoid string interpolation for `system`
calls.
2026-05-13 14:12:48 +01:00

66 lines
1.7 KiB
Ruby

# frozen_string_literal: true
class UploadReference < ActiveRecord::Base
belongs_to :upload
belongs_to :target, polymorphic: true
delegate :to_markdown, to: :upload
def self.ensure_exist!(upload_ids: [], target: nil, target_type: nil, target_id: nil)
if !target && !(target_type && target_id)
raise "target OR target_type and target_id are required"
end
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
# target_type :string not null
# created_at :datetime not null
# updated_at :datetime not null
# target_id :bigint not null
# upload_id :bigint 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)
#