discourse/app/models/theme_settings_migration.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

54 lines
1.6 KiB
Ruby

# frozen_string_literal: true
class ThemeSettingsMigration < ActiveRecord::Base
belongs_to :theme
belongs_to :theme_field
validates :theme_id, presence: true, uniqueness: { scope: :version }
validates :theme_field_id, presence: true
validates :version, presence: true
validates :name, presence: true
validates :diff, presence: true
def calculate_diff(settings_before, settings_after)
diff = { additions: [], deletions: [] }
before_keys = settings_before.keys
after_keys = settings_after.keys
removed_keys = before_keys - after_keys
removed_keys.each { |key| diff[:deletions] << { key: key, val: settings_before[key] } }
added_keys = after_keys - before_keys
added_keys.each { |key| diff[:additions] << { key: key, val: settings_after[key] } }
common_keys = before_keys & after_keys
common_keys.each do |key|
if settings_before[key] != settings_after[key]
diff[:deletions] << { key: key, val: settings_before[key] }
diff[:additions] << { key: key, val: settings_after[key] }
end
end
self.diff = diff
end
end
# == Schema Information
#
# Table name: theme_settings_migrations
#
# id :bigint not null, primary key
# diff :json not null
# name :string(150) not null
# version :integer not null
# created_at :datetime not null
# theme_field_id :integer not null
# theme_id :integer not null
#
# Indexes
#
# index_theme_settings_migrations_on_theme_field_id (theme_field_id) UNIQUE
# index_theme_settings_migrations_on_theme_id_and_version (theme_id,version) UNIQUE
#