From 33a4ab13b5e6bce443f7465dfc2ec14247fc0c36 Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Thu, 10 Oct 2024 19:28:45 +0300 Subject: [PATCH] DEV: Set bigint sequences to start at MAX_INT (#28961) This helps uncover issues with bigint columns that are joined with int columns. It also introduces a temporary API for plugins to migrate int columns to bigint in test environment to make tests pass. --- ...10155139_alter_automation_ids_to_bigint.rb | 13 ++++++++ plugins/chat/lib/chat/message_mover.rb | 4 +-- plugins/chat/spec/plugin_helper.rb | 20 ++++++++++++ spec/rails_helper.rb | 32 ++++++++++++++++--- 4 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 plugins/automation/db/migrate/20241010155139_alter_automation_ids_to_bigint.rb diff --git a/plugins/automation/db/migrate/20241010155139_alter_automation_ids_to_bigint.rb b/plugins/automation/db/migrate/20241010155139_alter_automation_ids_to_bigint.rb new file mode 100644 index 00000000000..9b48ea49c62 --- /dev/null +++ b/plugins/automation/db/migrate/20241010155139_alter_automation_ids_to_bigint.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class AlterAutomationIdsToBigint < ActiveRecord::Migration[7.1] + def up + change_column :discourse_automation_fields, :automation_id, :bigint + change_column :discourse_automation_pending_automations, :automation_id, :bigint + change_column :discourse_automation_pending_pms, :automation_id, :bigint + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/plugins/chat/lib/chat/message_mover.rb b/plugins/chat/lib/chat/message_mover.rb index 3737d0a024d..fa7de0640a4 100644 --- a/plugins/chat/lib/chat/message_mover.rb +++ b/plugins/chat/lib/chat/message_mover.rb @@ -94,8 +94,8 @@ module Chat DB.exec <<~SQL CREATE TEMPORARY TABLE moved_chat_messages ( - old_chat_message_id INTEGER, - new_chat_message_id INTEGER + old_chat_message_id BIGINT, + new_chat_message_id BIGINT ) ON COMMIT DROP; CREATE INDEX moved_chat_messages_old_chat_message_id ON moved_chat_messages(old_chat_message_id); diff --git a/plugins/chat/spec/plugin_helper.rb b/plugins/chat/spec/plugin_helper.rb index 3011db23f8b..9c29fd94aec 100644 --- a/plugins/chat/spec/plugin_helper.rb +++ b/plugins/chat/spec/plugin_helper.rb @@ -139,4 +139,24 @@ RSpec.configure do |config| # Or a very large value, if you do want to truncate at some point c.max_formatted_output_length = nil end + + config.before(:suite) do + migrate_column_to_bigint(Chat::Channel, :chatable_id) + migrate_column_to_bigint(Chat::ChannelArchive, :chat_channel_id) + migrate_column_to_bigint(Chat::DirectMessageUser, :direct_message_channel_id) + migrate_column_to_bigint(Chat::Draft, :chat_channel_id) + migrate_column_to_bigint(Chat::IncomingWebhook, :chat_channel_id) + migrate_column_to_bigint(Chat::Mention, :chat_message_id) + migrate_column_to_bigint(Chat::MentionNotification, :chat_mention_id) + migrate_column_to_bigint(Chat::MentionNotification, :notification_id) + migrate_column_to_bigint(Chat::Message, :chat_channel_id) + migrate_column_to_bigint(Chat::Message, :in_reply_to_id) + migrate_column_to_bigint(Chat::MessageReaction, :chat_message_id) + migrate_column_to_bigint(Chat::MessageRevision, :chat_message_id) + migrate_column_to_bigint(Chat::UserChatChannelMembership, :chat_channel_id) + migrate_column_to_bigint(Chat::UserChatChannelMembership, :last_read_message_id) + migrate_column_to_bigint(Chat::UserChatChannelMembership, :last_unread_mention_when_emailed_id) + migrate_column_to_bigint(Chat::WebhookEvent, :chat_message_id) + migrate_column_to_bigint(Chat::WebhookEvent, :incoming_chat_webhook_id) + end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 9ecffa8e218..72df165de21 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -466,12 +466,32 @@ RSpec.configure do |config| Capybara::Selenium::Driver.new(app, **mobile_driver_options) end - if ENV["ELEVATED_UPLOADS_ID"] - DB.exec "SELECT setval('uploads_id_seq', 10000)" - else - DB.exec "SELECT setval('uploads_id_seq', 1)" + migrate_column_to_bigint(AllowedPmUser, :allowed_pm_user_id) + migrate_column_to_bigint(Bookmark, :bookmarkable_id) + migrate_column_to_bigint(IgnoredUser, :ignored_user_id) + migrate_column_to_bigint(PostAction, :post_action_type_id) + migrate_column_to_bigint(Reviewable, :target_id) + migrate_column_to_bigint(ReviewableHistory, :reviewable_id) + migrate_column_to_bigint(ReviewableScore, :reviewable_id) + migrate_column_to_bigint(ReviewableScore, :reviewable_score_type) + migrate_column_to_bigint(SidebarSectionLink, :linkable_id) + migrate_column_to_bigint(SidebarSectionLink, :sidebar_section_id) + migrate_column_to_bigint(User, :last_seen_reviewable_id) + migrate_column_to_bigint(User, :required_fields_version) + + $columns_to_migrate_to_bigint.each do |model, column| + if model.is_a?(String) + DB.exec("ALTER TABLE #{model} ALTER #{column} TYPE bigint") + else + DB.exec("ALTER TABLE #{model.table_name} ALTER #{column} TYPE bigint") + model.reset_column_information + end end + DB + .query("SELECT sequence_name FROM information_schema.sequences WHERE data_type = 'bigint'") + .each { |row| DB.exec "SELECT setval('#{row.sequence_name}', #{2**32})" } + # Prevents 500 errors for site setting URLs pointing to test.localhost in system specs. SiteIconManager.clear_cache! end @@ -1018,6 +1038,10 @@ def apply_base_chrome_options(options) end end +def migrate_column_to_bigint(model, column) + ($columns_to_migrate_to_bigint ||= []) << [model, column] +end + class SpecSecureRandom class << self attr_accessor :value