From d1be39d03ad665af25c1c71d84f557731958ac9a Mon Sep 17 00:00:00 2001 From: Brandon Dewitt Date: Tue, 17 Feb 2015 16:55:57 -0700 Subject: [PATCH] Make column mappings name/type exclusive to a thread We have only seen it happen once, but columns were not being mapped and it seems it was due to threading issues with the mapper. also updated .gitignore to igore .ruby-* --- .gitignore | 1 + lib/protobuf/active_record/columns.rb | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 20db3fc..a22ff2f 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ .config .rvmrc .yardoc +.ruby-* Gemfile.lock coverage pkg/* diff --git a/lib/protobuf/active_record/columns.rb b/lib/protobuf/active_record/columns.rb index 7b65b23..3d9f49d 100644 --- a/lib/protobuf/active_record/columns.rb +++ b/lib/protobuf/active_record/columns.rb @@ -39,20 +39,22 @@ def _protobuf_datetime_column?(key) # Map out the columns for future reference on type conversion # :nodoc: def _protobuf_map_columns(force = false) - return unless table_exists? + ::Thread.exclusive do + @_protobuf_mapped_columns = false if force - @_protobuf_mapped_columns = false if force - return if _protobuf_mapped_columns? + return unless table_exists? + return if _protobuf_mapped_columns? - @_protobuf_columns = {} - @_protobuf_column_types = Hash.new { |h,k| h[k] = [] } + @_protobuf_columns = {} + @_protobuf_column_types = Hash.new { |h,k| h[k] = [] } - columns.map do |column| - @_protobuf_columns[column.name.to_sym] = column - @_protobuf_column_types[column.type.to_sym] << column.name.to_sym - end + columns.map do |column| + @_protobuf_columns[column.name.to_sym] = column + @_protobuf_column_types[column.type.to_sym] << column.name.to_sym + end - @_protobuf_mapped_columns = true + @_protobuf_mapped_columns = true + end end def _protobuf_mapped_columns?