diff --git a/db/migrate/20180502015927_issue_1219_zhtw_locale_typo.rb b/db/migrate/20180502015927_issue_1219_zhtw_locale_typo.rb new file mode 100644 index 000000000..661a11e77 --- /dev/null +++ b/db/migrate/20180502015927_issue_1219_zhtw_locale_typo.rb @@ -0,0 +1,38 @@ +class Issue1219ZhtwLocaleTypo < ActiveRecord::Migration[5.1] + CURRENT_VERSION = Gem::Version.new(Version.get) + APPLICABLE_VERSION = Gem::Version.new('2.5.0') + + def up + return unless Setting.find_by(name: 'system_init_done') + return unless CURRENT_VERSION >= APPLICABLE_VERSION + + if Locale.exists?(locale: 'zh-tw') + Locale.find_by(locale: 'zj-tw')&.destroy + else + Locale.find_by(locale: 'zj-tw')&.update(locale: 'zh-tw') + end + + Translation.where(locale: 'zj-tw')&.update_all(locale: 'zh-tw') + User.where('preferences LIKE ?', "%\nlocale: zj-tw\n%").each do |u| + u.preferences[:locale] = 'zh-tw' + u.save + end + end + + def down + return unless Setting.find_by(name: 'system_init_done') + return unless CURRENT_VERSION < APPLICABLE_VERSION + + if Locale.exists?(locale: 'zj-tw') + Locale.find_by(locale: 'zh-tw')&.destroy + else + Locale.find_by(locale: 'zh-tw')&.update(locale: 'zj-tw') + end + + Translation.where(locale: 'zh-tw')&.update_all(locale: 'zj-tw') + User.where('preferences LIKE ?', "%\nlocale: zh-tw\n%").each do |u| + u.preferences[:locale] = 'zj-tw' + u.save + end + end +end diff --git a/lib/version.rb b/lib/version.rb index 847079ca1..c1cbbc853 100644 --- a/lib/version.rb +++ b/lib/version.rb @@ -15,18 +15,9 @@ returns =end def self.get - - begin - version = File.read(Rails.root.join('VERSION')) - version.strip! - rescue => e - message = e.to_s - Rails.logger.error "VERSION file could not be read: #{message}" - - version = '' - end - - version + File.read(Rails.root.join('VERSION')).strip + rescue => e + Rails.logger.error "VERSION file could not be read: #{e}" + return '' end - end diff --git a/spec/db/migrate/issue_1219_zhtw_locale_typo_spec.rb b/spec/db/migrate/issue_1219_zhtw_locale_typo_spec.rb new file mode 100644 index 000000000..b81bda651 --- /dev/null +++ b/spec/db/migrate/issue_1219_zhtw_locale_typo_spec.rb @@ -0,0 +1,58 @@ +require 'rails_helper' + +RSpec.describe Issue1219ZhtwLocaleTypo, type: :db_migration do + let(:locale) { create(:locale, locale: premigrate_locale, name: 'Chinese (Tradi.) (正體中文)') } + let(:translation) { create(:translation, locale: premigrate_locale) } + let(:user) { create(:user, preferences: { locale: premigrate_locale }) } + + before(:each) do + Locale.find_by(name: 'Chinese (Tradi.) (正體中文)')&.destroy + stub_const("#{described_class}::CURRENT_VERSION", version) + end + + context 'upgrading to version 2.5.0+' do + let(:premigrate_locale) { 'zj-tw' } + let(:version) { Gem::Version.new('2.5.0') } + + it 'corrects the zh-tw locale code' do + expect { migrate } + .to change { locale.reload.locale } + .from('zj-tw').to('zh-tw') + end + + it 'updates translation records' do + expect { migrate } + .to change { translation.reload.locale } + .from('zj-tw').to('zh-tw') + end + + it 'updates user records (preferences[:locale])' do + expect { migrate } + .to change { user.reload.preferences[:locale] } + .from('zj-tw').to('zh-tw') + end + end + + context 'downgrading to version <2.5.0' do + let(:premigrate_locale) { 'zh-tw' } + let(:version) { Gem::Version.new('2.4.99') } + + it 'reverts the zh-tw locale code back to zj-tw' do + expect { migrate(:down) } + .to change { locale.reload.locale } + .from('zh-tw').to('zj-tw') + end + + it 'reverts translation records' do + expect { migrate(:down) } + .to change { translation.reload.locale } + .from('zh-tw').to('zj-tw') + end + + it 'reverts user records (preferences[:locale])' do + expect { migrate(:down) } + .to change { user.reload.preferences[:locale] } + .from('zh-tw').to('zj-tw') + end + end +end diff --git a/spec/factories/locale.rb b/spec/factories/locale.rb new file mode 100644 index 000000000..ee110b12c --- /dev/null +++ b/spec/factories/locale.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :locale do + locale 'de-de' + name 'Deutsch' + end +end diff --git a/spec/factories/translation.rb b/spec/factories/translation.rb new file mode 100644 index 000000000..c1d65b86f --- /dev/null +++ b/spec/factories/translation.rb @@ -0,0 +1,9 @@ +FactoryBot.define do + factory :translation do + locale 'de-de' + source 'date' + target 'dd/mm/yyyy' + created_by_id 1 + updated_by_id 1 + end +end