Sha256: ac02abd3ac31e128fef3fafd409f568b75e4a3a548058a39a6165a9aa78f0bec

Contents?: true

Size: 1.69 KB

Versions: 19

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

require 'thredded/base_migration'

class UpgradeThreddedV015ToV016 < Thredded::BaseMigration
  def up
    %i[thredded_user_topic_read_states thredded_user_private_topic_read_states].each do |table_name|
      add_column table_name, :unread_posts_count, :integer, default: 0, null: false
      add_column table_name, :read_posts_count, :integer, default: 0, null: false
    end
    add_column :thredded_user_topic_read_states, :messageboard_id, column_type(:thredded_messageboards, :id)
    set_messageboard_ids
    change_column_null :thredded_user_topic_read_states, :messageboard_id, false
    add_index :thredded_user_topic_read_states, :messageboard_id
    add_index :thredded_user_topic_read_states, %i[user_id messageboard_id],
              name: :thredded_user_topic_read_states_user_messageboard
    [Thredded::UserTopicReadState, Thredded::UserPrivateTopicReadState].each do |klass|
      klass.reset_column_information
      klass.update_post_counts!
    end
  end

  def down
    remove_column :thredded_user_topic_read_states, :messageboard_id
    %i[thredded_user_topic_read_states thredded_user_private_topic_read_states].each do |table|
      remove_column table, :unread_posts_count
    end
  end

  private

  def set_messageboard_ids
    messageboard_topics = Thredded::Topic.pluck(:messageboard_id, :id).group_by(&:first)
    messageboard_topics.transform_values! { |v| v.map(&:second) }
    messageboard_topics.each do |messageboard_id, topic_ids|
      say "Setting messageboard_id #{messageboard_id} for postable_id IN (#{topic_ids.join(',')})"
      Thredded::UserTopicReadState.where(postable_id: topic_ids).update_all(messageboard_id: messageboard_id)
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
thredded-1.1.0 db/upgrade_migrations/20180930063614_upgrade_thredded_v0_15_to_v0_16.rb
thredded-1.0.1 db/upgrade_migrations/20180930063614_upgrade_thredded_v0_15_to_v0_16.rb
thredded-1.0.0 db/upgrade_migrations/20180930063614_upgrade_thredded_v0_15_to_v0_16.rb
thredded-0.16.16 db/upgrade_migrations/20180930063614_upgrade_thredded_v0_15_to_v0_16.rb
thredded-0.16.15 db/upgrade_migrations/20180930063614_upgrade_thredded_v0_15_to_v0_16.rb
thredded-0.16.14 db/upgrade_migrations/20180930063614_upgrade_thredded_v0_15_to_v0_16.rb
thredded-0.16.13 db/upgrade_migrations/20180930063614_upgrade_thredded_v0_15_to_v0_16.rb
thredded-0.16.12 db/upgrade_migrations/20180930063614_upgrade_thredded_v0_15_to_v0_16.rb
thredded-0.16.11 db/upgrade_migrations/20180930063614_upgrade_thredded_v0_15_to_v0_16.rb
thredded-0.16.10 db/upgrade_migrations/20180930063614_upgrade_thredded_v0_15_to_v0_16.rb
thredded-0.16.9 db/upgrade_migrations/20180930063614_upgrade_thredded_v0_15_to_v0_16.rb
thredded-0.16.8 db/upgrade_migrations/20180930063614_upgrade_thredded_v0_15_to_v0_16.rb
thredded-0.16.7 db/upgrade_migrations/20180930063614_upgrade_thredded_v0_15_to_v0_16.rb
thredded-0.16.6 db/upgrade_migrations/20180930063614_upgrade_thredded_v0_15_to_v0_16.rb
thredded-0.16.5 db/upgrade_migrations/20180930063614_upgrade_thredded_v0_15_to_v0_16.rb
thredded-0.16.4 db/upgrade_migrations/20180930063614_upgrade_thredded_v0_15_to_v0_16.rb
thredded-0.16.3 db/upgrade_migrations/20180930063614_upgrade_thredded_v0_15_to_v0_16.rb
thredded-0.16.1 db/upgrade_migrations/20180930063614_upgrade_thredded_v0_15_to_v0_16.rb
thredded-0.16.0 db/upgrade_migrations/20180930063614_upgrade_thredded_v0_15_to_v0_16.rb