mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: drop unused column flair_url from groups table. (#17179)
It's already included in the `ignored_columns` list in the group model. 03ffb0bf27/app/models/group.rb (L9)
Also, removed the `MigrateGroupFlairImages` onceoff job and spec.
This commit is contained in:
@@ -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
|
|
||||||
17
db/post_migrate/20220621164914_drop_flair_url_from_groups.rb
Normal file
17
db/post_migrate/20220621164914_drop_flair_url_from_groups.rb
Normal file
@@ -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
|
||||||
@@ -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
|
|
||||||
Reference in New Issue
Block a user