A platform for community discussion. Free, open, simple.
DEV: rename topic_id to timerable_id for BaseTimer (#34667)
Lincaeeeb7d302f26
diff --git a/app/models/base_timer.rb b/app/models/base_timer.rbindex cb4cf40a9f2..d839ac6f0c9 100644--- a/app/models/base_timer.rb+++ b/app/models/base_timer.rb@@ -3,6 +3,8 @@class BaseTimer < ActiveRecord::Baseself.table_name = "topic_timers"+ self.ignored_columns = ["topic_id"]+MAX_DURATION_MINUTES = 20.years.to_i / 60include Trashable@@ -112,12 +114,14 @@ end# updated_at :datetime not null# category_id :integer# deleted_by_id :integer-# topic_id :integer not null+# timerable_id :integer not null# user_id :integer not null## Indexes#-# idx_topic_id_public_type_deleted_at (topic_id) UNIQUE WHERE ((public_type = true) AND (deleted_at IS NULL) AND ((type)::text = 'TopicTimer'::text))-# index_topic_timers_on_topic_id (topic_id) WHERE (deleted_at IS NULL)-# index_topic_timers_on_user_id (user_id)+# idx_timerable_id_public_type_deleted_at (timerable_id) UNIQUE WHERE ((public_type = true) AND (deleted_at IS NULL) AND ((type)::text = 'TopicTimer'::text))+# idx_topic_id_public_type_deleted_at (topic_id) UNIQUE WHERE ((public_type = true) AND (deleted_at IS NULL) AND ((type)::text = 'TopicTimer'::text))+# index_topic_timers_on_timerable_id (timerable_id) WHERE (deleted_at IS NULL)+# index_topic_timers_on_topic_id (topic_id) WHERE (deleted_at IS NULL)+# index_topic_timers_on_user_id (user_id)#diff --git a/app/models/topic.rb b/app/models/topic.rbindex 59c26aa1573..5d0ae74db72 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -303,7 +303,7 @@ class Topic < ActiveRecord::Basehas_many :topic_linkshas_many :topic_inviteshas_many :invites, through: :topic_invites, source: :invite- has_many :topic_timers, dependent: :destroy+ has_many :topic_timers, dependent: :destroy, foreign_key: :timerable_idhas_many :reviewableshas_many :user_profilesdiff --git a/app/models/topic_timer.rb b/app/models/topic_timer.rbindex 5a09aaa5364..c59d06577e5 100644--- a/app/models/topic_timer.rb+++ b/app/models/topic_timer.rb@@ -2,19 +2,23 @@class TopicTimer < BaseTimerbelongs_to :user- belongs_to :topic+ belongs_to :topic, foreign_key: :timerable_idbelongs_to :categoryvalidates :user_id, presence: true- validates :topic_id, presence: true+ validates :timerable_id, presence: truevalidates :execute_at, presence: true- validates :status_type, uniqueness: { scope: %i[topic_id deleted_at] }, if: :public_type?- validates :status_type, uniqueness: { scope: %i[topic_id deleted_at user_id] }, if: :private_type?+ validates :status_type, uniqueness: { scope: %i[timerable_id deleted_at] }, if: :public_type?+ validates :status_type,+ uniqueness: {+ scope: %i[timerable_id deleted_at user_id],+ },+ if: :private_type?validate :executed_at_in_future?scope :scheduled_bump_topics,- -> { where(status_type: TopicTimer.types[:bump], deleted_at: nil).pluck(:topic_id) }+ -> { where(status_type: TopicTimer.types[:bump], deleted_at: nil).pluck(:timerable_id) }scope :pending_timers,->(before_time = Time.now.utc) dowhere("execute_at <= :before_time AND deleted_at IS NULL", before_time: before_time)@@ -110,12 +114,14 @@ end# updated_at :datetime not null# category_id :integer# deleted_by_id :integer-# topic_id :integer not null+# timerable_id :integer not null# user_id :integer not null## Indexes#-# idx_topic_id_public_type_deleted_at (topic_id) UNIQUE WHERE ((public_type = true) AND (deleted_at IS NULL) AND ((type)::text = 'TopicTimer'::text))-# index_topic_timers_on_topic_id (topic_id) WHERE (deleted_at IS NULL)-# index_topic_timers_on_user_id (user_id)+# idx_timerable_id_public_type_deleted_at (timerable_id) UNIQUE WHERE ((public_type = true) AND (deleted_at IS NULL) AND ((type)::text = 'TopicTimer'::text))+# idx_topic_id_public_type_deleted_at (topic_id) UNIQUE WHERE ((public_type = true) AND (deleted_at IS NULL) AND ((type)::text = 'TopicTimer'::text))+# index_topic_timers_on_timerable_id (timerable_id) WHERE (deleted_at IS NULL)+# index_topic_timers_on_topic_id (topic_id) WHERE (deleted_at IS NULL)+# index_topic_timers_on_user_id (user_id)#diff --git a/app/services/user_merger.rb b/app/services/user_merger.rbindex d2e37dccc8b..a9dd145e433 100644--- a/app/services/user_merger.rb+++ b/app/services/user_merger.rb@@ -436,7 +436,7 @@ class UserMerger:topic_timers,conditions: ["x.status_type = y.status_type",- "x.topic_id = y.topic_id",+ "x.timerable_id = y.timerable_id","y.deleted_at IS NULL",],)diff --git a/db/migrate/20250902014817_add_timerable_id_to_topic_timer.rb b/db/migrate/20250902014817_add_timerable_id_to_topic_timer.rbnew file mode 100644index 00000000000..f6d34a7a738--- /dev/null+++ b/db/migrate/20250902014817_add_timerable_id_to_topic_timer.rb@@ -0,0 +1,38 @@+# frozen_string_literal: true+class AddTimerableIdToTopicTimer < ActiveRecord::Migration[8.0]+ def up+ add_column :topic_timers, :timerable_id, :integer+ add_index :topic_timers,+ :timerable_id,+ unique: true,+ name: :idx_timerable_id_public_type_deleted_at,+ where: "public_type = true AND deleted_at IS NULL AND type = 'TopicTimer'"+ add_index :topic_timers, :timerable_id, where: "deleted_at IS NULL"+ change_column_null :topic_timers, :topic_id, true+ Migration::ColumnDropper.mark_readonly(:topic_timers, :topic_id)++ # Mirror new `topic_id` values to `timerable_id`+ execute(<<~SQL)+ CREATE FUNCTION mirror_topic_timers_topic_id()+ RETURNS trigger AS+ $$+ BEGIN+ IF NEW.topic_id IS NOT NULL THEN+ NEW.timerable_id = NEW.topic_id;+ END IF;+ RETURN NEW;+ END;+ $$+ LANGUAGE plpgsql+ SQL++ execute(<<~SQL)+ CREATE TRIGGER topic_timers_topic_id_trigger BEFORE INSERT ON topic_timers+ FOR EACH ROW EXECUTE PROCEDURE mirror_topic_timers_topic_id()+ SQL+ end++ def down+ raise ActiveRecord::IrreversibleMigration+ end+enddiff --git a/db/migrate/20250902072941_sync_timerable_id_topic_id.rb b/db/migrate/20250902072941_sync_timerable_id_topic_id.rbnew file mode 100644index 00000000000..e82eaf6cb14--- /dev/null+++ b/db/migrate/20250902072941_sync_timerable_id_topic_id.rb@@ -0,0 +1,19 @@+# frozen_string_literal: true+class SyncTimerableIdTopicId < ActiveRecord::Migration[8.0]+ disable_ddl_transaction!++ def up+ min_id, max_id = execute("SELECT MIN(id), MAX(id) FROM topic_timers")[0].values+ batch_size = 10_000++ (min_id..max_id).step(batch_size) { |start_id| execute <<~SQL.squish } if min_id && max_id+ UPDATE topic_timers+ SET timerable_id = topic_id+ WHERE id >= #{start_id} AND id < #{start_id + batch_size} AND timerable_id != topic_id+ SQL+ end++ def down+ raise ActiveRecord::IrreversibleMigration+ end+enddiff --git a/db/migrate/20250916082012_mark_timerable_id_non_nullable.rb b/db/migrate/20250916082012_mark_timerable_id_non_nullable.rbnew file mode 100644index 00000000000..764f6fc3e99--- /dev/null+++ b/db/migrate/20250916082012_mark_timerable_id_non_nullable.rb@@ -0,0 +1,6 @@+# frozen_string_literal: true+class MarkTimerableIdNonNullable < ActiveRecord::Migration[8.0]+ def change+ change_column_null :topic_timers, :timerable_id, false+ end+enddiff --git a/lib/tasks/topics.rake b/lib/tasks/topics.rakeindex aab58ed6a0d..8629f5b28ac 100644--- a/lib/tasks/topics.rake+++ b/lib/tasks/topics.rake@@ -31,7 +31,7 @@ def apply_auto_close(category)NOT EXISTS (SELECT 1FROM topic_timers- WHERE topic_timers.topic_id = topics.id+ WHERE topic_timers.timerable_id = topics.idAND topic_timers.status_type = ?AND topic_timers.deleted_at IS NULL)diff --git a/plugins/discourse-calendar/jobs/regular/discourse_post_event/bump_topic.rb b/plugins/discourse-calendar/jobs/regular/discourse_post_event/bump_topic.rbindex 530baaec141..4f0b36d32a0 100644--- a/plugins/discourse-calendar/jobs/regular/discourse_post_event/bump_topic.rb+++ b/plugins/discourse-calendar/jobs/regular/discourse_post_event/bump_topic.rb@@ -9,7 +9,7 @@ module Jobsreturn if args[:date].blank?event_user = User.find_by(id: topic.user_id)- timer = TopicTimer.find_by(topic_id: args[:topic_id].to_i)+ timer = TopicTimer.find_by(timerable_id: args[:topic_id].to_i)topic.set_or_create_timer(TopicTimer.types[:bump], args[:date], by_user: event_user)enddiff --git a/spec/jobs/close_topic_spec.rb b/spec/jobs/close_topic_spec.rbindex 6a2913e9885..29e9d32f2ac 100644--- a/spec/jobs/close_topic_spec.rb+++ b/spec/jobs/close_topic_spec.rb@@ -31,7 +31,7 @@ RSpec.describe Jobs::CloseTopic doexpect dodescribed_class.new.execute(topic_timer_id: topic.public_topic_timer.id, state: true)- end.to change { TopicTimer.exists?(topic_id: topic.id) }.from(true).to(false)+ end.to change { TopicTimer.exists?(topic:) }.from(true).to(false)endend@@ -43,7 +43,7 @@ RSpec.describe Jobs::CloseTopic doexpect dodescribed_class.new.execute(topic_timer_id: topic.public_topic_timer.id, state: true)- end.to change { TopicTimer.exists?(topic_id: topic.id) }.from(true).to(false)+ end.to change { TopicTimer.exists?(topic:) }.from(true).to(false)endend@@ -57,7 +57,7 @@ RSpec.describe Jobs::CloseTopic doexpect dodescribed_class.new.execute(topic_timer_id: topic.public_topic_timer.id, state: true)- end.to change { TopicTimer.exists?(topic_id: topic.id) }.from(true).to(false)+ end.to change { TopicTimer.exists?(topic:) }.from(true).to(false)expect(topic.reload.closed).to eq(false)enddiff --git a/spec/jobs/open_topic_spec.rb b/spec/jobs/open_topic_spec.rbindex 11d5422819d..aaa355a371d 100644--- a/spec/jobs/open_topic_spec.rb+++ b/spec/jobs/open_topic_spec.rb@@ -59,7 +59,7 @@ RSpec.describe Jobs::OpenTopic doexpect dodescribed_class.new.execute(topic_timer_id: topic.public_topic_timer.id)- end.to change { TopicTimer.exists?(topic_id: topic.id) }.from(true).to(false)+ end.to change { TopicTimer.exists?(topic:) }.from(true).to(false)expect(topic.reload.open?).to eq(false)enddiff --git a/spec/jobs/toggle_topic_closed_spec.rb b/spec/jobs/toggle_topic_closed_spec.rbindex e9c06e17732..5164add32df 100644--- a/spec/jobs/toggle_topic_closed_spec.rb+++ b/spec/jobs/toggle_topic_closed_spec.rb@@ -62,7 +62,7 @@ RSpec.describe Jobs::ToggleTopicClosed doexpect dodescribed_class.new.execute(topic_timer_id: topic.public_topic_timer.id, state: true)- end.to change { TopicTimer.exists?(topic_id: topic.id) }.from(true).to(false)+ end.to change { TopicTimer.exists?(topic:) }.from(true).to(false)endend@@ -74,7 +74,7 @@ RSpec.describe Jobs::ToggleTopicClosed doexpect dodescribed_class.new.execute(topic_timer_id: topic.public_topic_timer.id, state: true)- end.to change { TopicTimer.exists?(topic_id: topic.id) }.from(true).to(false)+ end.to change { TopicTimer.exists?(topic:) }.from(true).to(false)endend@@ -88,7 +88,7 @@ RSpec.describe Jobs::ToggleTopicClosed doexpect dodescribed_class.new.execute(topic_timer_id: topic.public_topic_timer.id, state: true)- end.to change { TopicTimer.exists?(topic_id: topic.id) }.from(true).to(false)+ end.to change { TopicTimer.exists?(topic:) }.from(true).to(false)expect(topic.reload.closed).to eq(false)end