FIX: Job to clean up old URL settings when new setting has been set.

Related to 44391ee8ab
This commit is contained in:
Guo Xiang Tan 2018-11-16 09:19:58 +08:00
parent 9157fdd69c
commit 9e86b425bc
3 changed files with 54 additions and 12 deletions

View File

@ -1,18 +1,20 @@
module Jobs
class MigrateUrlSiteSettings < Jobs::Onceoff
SETTINGS = [
['logo_url', 'logo'],
['logo_small_url', 'logo_small'],
['digest_logo_url', 'digest_logo'],
['mobile_logo_url', 'mobile_logo'],
['large_icon_url', 'large_icon'],
['favicon_url', 'favicon'],
['apple_touch_icon_url', 'apple_touch_icon'],
['default_opengraph_image_url', 'opengraph_image'],
['twitter_summary_large_image_url', 'twitter_summary_large_image'],
['push_notifications_icon_url', 'push_notifications_icon'],
]
def execute_onceoff(args)
[
['logo_url', 'logo'],
['logo_small_url', 'logo_small'],
['digest_logo_url', 'digest_logo'],
['mobile_logo_url', 'mobile_logo'],
['large_icon_url', 'large_icon'],
['favicon_url', 'favicon'],
['apple_touch_icon_url', 'apple_touch_icon'],
['default_opengraph_image_url', 'opengraph_image'],
['twitter_summary_large_image_url', 'twitter_summary_large_image'],
['push_notifications_icon_url', 'push_notifications_icon'],
].each do |old_setting, new_setting|
SETTINGS.each do |old_setting, new_setting|
old_url = DB.query_single(
"SELECT value FROM site_settings WHERE name = '#{old_setting}'"
).first

View File

@ -0,0 +1,13 @@
module Jobs
class CleanUpDeprecatedUrlSiteSettings < Jobs::Scheduled
every 1.day
def execute(args)
Jobs::MigrateUrlSiteSettings::SETTINGS.each do |old_setting, new_setting|
if SiteSetting.where("name = ? AND value IS NOT NULL", new_setting).exists?
SiteSetting.public_send("#{old_setting}=", nil, warn: false)
end
end
end
end
end

View File

@ -0,0 +1,27 @@
require 'rails_helper'
RSpec.describe Jobs::CleanUpDeprecatedUrlSiteSettings do
before do
@original_provider = SiteSetting.provider
SiteSetting.provider = SiteSettings::DbProvider.new(SiteSetting)
end
after do
SiteSetting.delete_all
SiteSetting.provider = @original_provider
end
it 'should clean up the old deprecated site settings correctly' do
logo_upload = Fabricate(:upload)
SiteSetting.logo = logo_upload
SiteSetting.public_send("logo_url=", '/test/some/url', warn: false)
SiteSetting.public_send("logo_small_url=", '/test/another/url', warn: false)
expect do
described_class.new.execute({})
end.to change { SiteSetting.logo_url }.from("/test/some/url").to("")
expect(SiteSetting.logo).to eq(logo_upload)
expect(SiteSetting.logo_small_url).to eq('/test/another/url')
end
end