mirror of https://github.com/mastodon/mastodon
Change hashtag trends to be stored in the database instead of redis (#32837)
Co-authored-by: David Roetzel <david@roetzel.de>pull/33173/head
parent
b9b26490e7
commit
48ea7552dd
@ -0,0 +1,21 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: tag_trends
|
||||
#
|
||||
# id :bigint(8) not null, primary key
|
||||
# tag_id :bigint(8) not null
|
||||
# score :float default(0.0), not null
|
||||
# rank :integer default(0), not null
|
||||
# allowed :boolean default(FALSE), not null
|
||||
# language :string
|
||||
#
|
||||
class TagTrend < ApplicationRecord
|
||||
include RankedTrend
|
||||
|
||||
belongs_to :tag
|
||||
|
||||
scope :allowed, -> { where(allowed: true) }
|
||||
scope :not_allowed, -> { where(allowed: false) }
|
||||
end
|
@ -0,0 +1,15 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class CreateTagTrends < ActiveRecord::Migration[7.2]
|
||||
def change
|
||||
create_table :tag_trends do |t| # rubocop:disable Rails/CreateTableWithTimestamps
|
||||
t.references :tag, null: false, foreign_key: { on_delete: :cascade }, index: false
|
||||
t.float :score, null: false, default: 0
|
||||
t.integer :rank, null: false, default: 0
|
||||
t.boolean :allowed, null: false, default: false
|
||||
t.string :language, null: false, default: ''
|
||||
end
|
||||
|
||||
add_index :tag_trends, [:tag_id, :language], unique: true
|
||||
end
|
||||
end
|
@ -0,0 +1,25 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class MoveTagTrendsToTable < ActiveRecord::Migration[7.2]
|
||||
include Redisable
|
||||
|
||||
disable_ddl_transaction!
|
||||
|
||||
def up
|
||||
redis.zrange('trending_tags:all', 0, -1, with_scores: true).each do |(tag_id, score)|
|
||||
TagTrend.create(
|
||||
tag_id: tag_id,
|
||||
score: score,
|
||||
allowed: redis.zscore('trending_tags:allowed', tag_id).present?
|
||||
)
|
||||
end
|
||||
|
||||
TagTrend.recalculate_ordered_rank
|
||||
|
||||
redis.del('trending_tags:allowed', 'trending_tags:all')
|
||||
end
|
||||
|
||||
def down
|
||||
raise ActiveRecord::IrreversibleMigration
|
||||
end
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
Fabricator(:tag_trend) do
|
||||
tag
|
||||
end
|
@ -0,0 +1,11 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe TagTrend do
|
||||
include_examples 'RankedTrend'
|
||||
|
||||
describe 'Associations' do
|
||||
it { is_expected.to belong_to(:tag).required }
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue