From deee3c6f02ecc679de8a143868385b28768fff7e Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Wed, 22 Jun 2022 00:15:05 +0530 Subject: [PATCH] DEV: drop unused column `flair_url` from groups table. (#17179) It's already included in the `ignored_columns` list in the group model. https://github.com/discourse/discourse/blob/03ffb0bf27d76a47ef5a3049ea76a4240b5dc3b8/app/models/group.rb#L9 Also, removed the `MigrateGroupFlairImages` onceoff job and spec. --- .../onceoff/migrate_group_flair_images.rb | 94 ------------------- ...220621164914_drop_flair_url_from_groups.rb | 17 ++++ spec/jobs/migrate_group_flair_images_spec.rb | 40 -------- 3 files changed, 17 insertions(+), 134 deletions(-) delete mode 100644 app/jobs/onceoff/migrate_group_flair_images.rb create mode 100644 db/post_migrate/20220621164914_drop_flair_url_from_groups.rb delete mode 100644 spec/jobs/migrate_group_flair_images_spec.rb diff --git a/app/jobs/onceoff/migrate_group_flair_images.rb b/app/jobs/onceoff/migrate_group_flair_images.rb deleted file mode 100644 index d613706065b..00000000000 --- a/app/jobs/onceoff/migrate_group_flair_images.rb +++ /dev/null @@ -1,94 +0,0 @@ -# frozen_string_literal: true -require 'uri' - -module Jobs - class MigrateGroupFlairImages < ::Jobs::Onceoff - def execute_onceoff(args) - column_exists = DB.exec(<<~SQL) == 1 - SELECT 1 - FROM INFORMATION_SCHEMA.COLUMNS - WHERE - table_schema = 'public' AND - table_name = 'groups' AND - column_name = 'flair_url' - SQL - return unless column_exists - - groups = Group.where.not(flair_url: nil).select(:id, :flair_url, :flair_upload_id, :name) - groups.each do |group| - if group.flair_upload.present? - DB.exec("UPDATE groups SET flair_url = NULL WHERE id = #{group.id}") - next - end - - old_url = group[:flair_url] - next if old_url.blank? || old_url !~ URI::regexp - - group_name = group.name - - count = 0 - file = nil - sleep_interval = 5 - - loop do - url = UrlHelper.absolute_without_cdn(old_url) - - begin - file = FileHelper.download( - url, - max_file_size: [ - SiteSetting.max_image_size_kb.kilobytes, - 20.megabytes - ].max, - tmp_file_name: 'tmp_group_flair_upload', - skip_rate_limit: true, - follow_redirect: true - ) - rescue OpenURI::HTTPError, - OpenSSL::SSL::SSLError, - Net::OpenTimeout, - Net::ReadTimeout, - Errno::ECONNREFUSED, - EOFError, - SocketError, - Discourse::InvalidParameters => e - - logger.warn( - "Error encountered when trying to download from URL '#{old_url}' " + - "for group '#{group_name}'.\n#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}" - ) - end - - count += 1 - break if file || (file.blank? && count >= 3) - - logger.info( - "Failed to download upload from #{url} for group '#{group_name}'. Retrying..." - ) - - sleep(count * sleep_interval) - end - - next if file.blank? - - upload = UploadCreator.new( - file, - "group_#{group_name}", - origin: UrlHelper.absolute(old_url) - ).create_for(Discourse.system_user.id) - - if upload.errors.count > 0 - logger.warn("Failed to create upload for '#{group_name}' group_flair: #{upload.errors.full_messages}") - else - DB.exec("UPDATE groups SET flair_url = NULL, flair_upload_id = #{upload.id} WHERE id = #{group.id}") if upload&.id.present? - end - end - end - - private - - def logger - Rails.logger - end - end -end diff --git a/db/post_migrate/20220621164914_drop_flair_url_from_groups.rb b/db/post_migrate/20220621164914_drop_flair_url_from_groups.rb new file mode 100644 index 00000000000..8f2f57f1147 --- /dev/null +++ b/db/post_migrate/20220621164914_drop_flair_url_from_groups.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class DropFlairUrlFromGroups < ActiveRecord::Migration[7.0] + DROPPED_COLUMNS ||= { + groups: %i{flair_url} + } + + def up + DROPPED_COLUMNS.each do |table, columns| + Migration::ColumnDropper.execute_drop(table, columns) + end + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/spec/jobs/migrate_group_flair_images_spec.rb b/spec/jobs/migrate_group_flair_images_spec.rb deleted file mode 100644 index e2c03802a5b..00000000000 --- a/spec/jobs/migrate_group_flair_images_spec.rb +++ /dev/null @@ -1,40 +0,0 @@ -# frozen_string_literal: true - -describe Jobs::MigrateGroupFlairImages do - let(:image_url) { "https://omg.aws.somestack/test.png" } - let(:group) { Fabricate(:group) } - - before do - stub_request(:get, image_url).to_return( - status: 200, body: file_from_fixtures("smallest.png").read - ) - @orig_logger = Rails.logger - Rails.logger = @fake_logger = FakeLogger.new - end - - after do - Rails.logger = @orig_logger - end - - it 'should migrate to the new group `flair_upload_id` column correctly' do - DB.exec(<<~SQL, flair_url: image_url) - UPDATE groups SET flair_url = :flair_url WHERE id = #{group.id} - SQL - - expect do - described_class.new.execute_onceoff({}) - end.to change { Upload.count }.by(1) - - group.reload - upload = Upload.last - expect(group.flair_upload).to eq(upload) - expect(group.flair_url).to eq(upload.url) - expect(group[:flair_url]).to eq(nil) - end - - it 'should skip groups with invalid flair URLs' do - DB.exec("UPDATE groups SET flair_url = 'abc' WHERE id = #{group.id}") - described_class.new.execute_onceoff({}) - expect(@fake_logger.warnings.count).to eq(0) - end -end