Browse Source

Maintenance: Speed up Translation.sync and the corresponding test.

Martin Gruner 2 years ago
parent
commit
fb85ac2579

+ 6 - 2
app/models/translation.rb

@@ -110,8 +110,12 @@ because it could produce wrong matches on case insensitive MySQL databases.
 =end
 
   def self.find_source(locale, string)
-    # MySQL might find the wrong record with find_by with case insensitive locales, so use a direct comparison.
-    where(locale: locale, source: string).find { |t| t.source.eql? string }
+    if ActiveRecord::Base.connection_db_config.configuration_hash[:adapter] == 'mysql2'
+      # MySQL might find the wrong record with find_by with case insensitive locales, so use a direct comparison.
+      where(locale: locale, source: string).find { |t| t.source.eql? string }
+    else
+      find_by(locale: locale, source: string)
+    end
   end
 
 =begin

+ 3 - 1
app/models/translation/synchronizes_from_po.rb

@@ -17,6 +17,8 @@ module Translation::SynchronizesFromPo
 
     def sync_locale_from_po(locale) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
 
+      previous_translation_map = Translation.where(locale: locale).index_by(&:source)
+
       previous_unmodified_translations = Translation.where(locale: locale, is_synchronized_from_codebase: true).select { |t| t.target.eql?(t.target_initial) }
       updated_translation_ids = Set[]
       importable_translations = []
@@ -28,7 +30,7 @@ module Translation::SynchronizesFromPo
           next
         end
 
-        t = Translation.find_source(locale, source)
+        t = previous_translation_map[source]
         # New string
         if !t
           importable_translations << Translation.new(

+ 4 - 4
spec/models/translation/translation_synchronizes_from_po_spec.rb → spec/models/translation/synchronizes_from_po_spec.rb

@@ -2,7 +2,7 @@
 
 require 'rails_helper'
 
-RSpec.describe Translation do
+RSpec.describe Translation, 'synchronizes_from_po' do
 
   context 'when getting the list of files for a locale' do
 
@@ -243,11 +243,11 @@ RSpec.describe Translation do
     end
   end
 
-  # Make sure that translation imports work really for all locales.
-  context 'when synchronizing strings for all locales' do
+  # Make sure that translation imports work really for some major locales.
+  context 'when synchronizing strings for some major locales' do
     before do
       # Only 'en-us' and 'de-de' are returned in test env - override.
-      allow(Locale).to receive(:to_sync).and_return(Locale.where(active: true))
+      allow(Locale).to receive(:to_sync).and_return(Locale.where(locale: %w[en-us de-de fr-fr ru zh-cn]))
       described_class.sync
     end