Skip to content

Changing primary_key type for Thredded tables

Gleb Mazovetskiy edited this page May 11, 2017 · 1 revision

You may at some point need to change the primary keys of Thredded tables, for example if you are upgrading to Rails from < 5.1 (where the default primary key type has changed from 4-bit to 8-bit integers), or decide to use uuid primary keys.

To do this, you can create a migration like this one:

# frozen_string_literal: true

require 'thredded/base_migration'

class ChangeThreddedPkey < Thredded::BaseMigration
  def change # rubocop:disable Metrics/MethodLength
    key_type = :bigint
    id_type = connection.adapter_name =~ /sqlite/i ? :primary_key : key_type
    user_fkey_type = self.user_id_type

    # On MySQL, all foreign keys must be removed first
    if connection.adapter_name =~ /mysql/i
      remove_foreign_key :thredded_messageboard_users, :thredded_user_details
      remove_foreign_key :thredded_messageboard_users, :thredded_messageboards
      remove_foreign_key :thredded_user_post_notifications, Thredded.user_class.table_name
      remove_foreign_key :thredded_user_post_notifications, :thredded_posts
    end

    change_table :friendly_id_slugs do |t|
      t.change :sluggable_id, key_type
    end
    change_table :thredded_categories do |t|
      t.change :id, id_type
      t.change :messageboard_id, key_type
    end
    change_table :thredded_messageboards do |t|
      t.change :id, id_type
      t.change :last_topic_id, key_type
      t.change :messageboard_group_id, key_type
    end
    change_table :thredded_posts do |t|
      t.change :id, id_type
      t.change :user_id, user_fkey_type
      t.change :postable_id, key_type
      t.change :messageboard_id, key_type
    end
    change_table :thredded_private_posts do |t|
      t.change :id, id_type
      t.change :user_id, user_fkey_type
      t.change :postable_id, key_type
    end
    change_table :thredded_private_topics do |t|
      t.change :id, id_type
      t.change :user_id, user_fkey_type
      t.change :last_user_id, key_type
    end
    change_table :thredded_private_users do |t|
      t.change :id, id_type
      t.change :private_topic_id, key_type
      t.change :user_id, user_fkey_type
    end
    change_table :thredded_topic_categories do |t|
      t.change :id, id_type
      t.change :topic_id, key_type
      t.change :category_id, key_type
    end
    change_table :thredded_topics do |t|
      t.change :id, id_type
      t.change :user_id, user_fkey_type
      t.change :last_user_id, key_type
      t.change :messageboard_id, key_type
    end
    change_table :thredded_user_details do |t|
      t.change :id, id_type
      t.change :user_id, user_fkey_type
    end
    change_table :thredded_messageboard_users do |t|
      t.change :id, id_type
      t.change :thredded_user_detail_id, key_type
      t.change :thredded_messageboard_id, key_type
    end
    change_table :thredded_user_preferences do |t|
      t.change :id, id_type
      t.change :user_id, user_fkey_type
    end
    change_table :thredded_user_messageboard_preferences do |t|
      t.change :id, id_type
      t.change :user_id, user_fkey_type
      t.change :messageboard_id, key_type
    end
    change_table :thredded_user_topic_read_states do |t|
      t.change :id, id_type
      t.change :user_id, user_fkey_type
      t.change :postable_id, key_type
    end
    change_table :thredded_user_private_topic_read_states do |t|
      t.change :id, id_type
      t.change :user_id, user_fkey_type
      t.change :postable_id, key_type
    end
    change_table :thredded_messageboard_groups do |t|
      t.change :id, id_type
    end
    change_table :thredded_user_topic_follows do |t|
      t.change :id, id_type
      t.change :user_id, user_fkey_type
      t.change :topic_id, key_type
    end
    change_table :thredded_post_moderation_records do |t|
      t.change :id, id_type
      t.change :post_id, key_type
      t.change :messageboard_id, key_type
      t.change :post_user_id, key_type
      t.change :moderator_id, key_type
    end
    change_table :thredded_notifications_for_private_topics do |t|
      t.change :id, id_type
      t.change :user_id, user_fkey_type
    end
    change_table :thredded_notifications_for_followed_topics do |t|
      t.change :id, id_type
      t.change :user_id, user_fkey_type
    end
    change_table :thredded_messageboard_notifications_for_followed_topics do |t|
      t.change :id, id_type
      t.change :user_id, user_fkey_type
      t.change :messageboard_id, key_type
    end
    change_table :thredded_user_post_notifications do |t|
      t.change :id, id_type
      t.change :user_id, user_fkey_type
      t.change :post_id, key_type
    end

    if connection.adapter_name =~ /mysql/i
      add_foreign_key :thredded_messageboard_users, :thredded_user_details,
                      column: :thredded_user_detail_id, on_delete: :cascade
      add_foreign_key :thredded_messageboard_users, :thredded_messageboards,
                      column: :thredded_messageboard_id, on_delete: :cascade
      add_foreign_key :thredded_user_post_notifications,
                      Thredded.user_class.table_name, column: :user_id, on_delete: :cascade
      add_foreign_key :thredded_user_post_notifications,
                      :thredded_posts, column: :post_id, on_delete: :cascade
    end
  end
end