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

40 lines
1.2 KiB
Ruby

# frozen_string_literal: true
class TopicViewStat < ActiveRecord::Base
belongs_to :topic
def self.add(topic_id:, date:, anonymous_views:, logged_in_views:)
sql = <<~SQL
INSERT INTO topic_view_stats (topic_id, viewed_at, anonymous_views, logged_in_views)
VALUES (:topic_id, :viewed_at, :anon_views, :logged_in_views)
ON CONFLICT (topic_id, viewed_at)
DO UPDATE SET
anonymous_views = topic_view_stats.anonymous_views + :anon_views,
logged_in_views = topic_view_stats.logged_in_views + :logged_in_views
SQL
DB.exec(
sql,
topic_id: topic_id,
viewed_at: date,
anon_views: anonymous_views,
logged_in_views: logged_in_views,
)
end
end
# == Schema Information
#
# Table name: topic_view_stats
#
# id :bigint not null, primary key
# anonymous_views :integer default(0), not null
# logged_in_views :integer default(0), not null
# viewed_at :date not null
# topic_id :integer not null
#
# Indexes
#
# index_topic_view_stats_on_topic_id_and_viewed_at (topic_id,viewed_at) UNIQUE
# index_topic_view_stats_on_viewed_at_and_topic_id (viewed_at,topic_id)
#