Module: RailsSync::ActiveRecord
- Defined in:
- lib/rails_sync/active_record.rb
Instance Method Summary collapse
-
#acts_as_sync(options = {}) ⇒ Object
source source_client dest_table.
- #analyze_diffs(type = 'update') ⇒ Object
- #cache_all_diffs(*types) ⇒ Object
- #cache_diffs(type = 'update') ⇒ Object
- #id_insert? ⇒ Boolean
- #prepare_sync ⇒ Object
- #server_id ⇒ Object
Instance Method Details
#acts_as_sync(options = {}) ⇒ Object
source source_client dest_table
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rails_sync/active_record.rb', line 9 def acts_as_sync( = {}) @syncs ||= [] [:dest_table] ||= self.table_name _mappings = Array(.delete(:mapping)).to_h if [:only] _filter_columns = self.column_names & Array(.delete(:only)) else _filter_columns = self.column_names - Array(.delete(:except)) end [:primary_key] = Array([:primary_key] || self.primary_key).map { |i| i.to_s } [:dest_primary_key] = Array([:dest_primary_key] || [:primary_key]).map { |i| i.to_s } [:dest_conditions] = [:dest_conditions] [:full_mappings] = _filter_columns.map { |column_name| next if [:primary_key].include?(column_name) if _mappings.key?(column_name) [column_name, _mappings[column_name]] else [column_name, column_name] end }.compact [:server_id] = self.server_id [:analyzer] = RailsSync::Analyzer.new(record: self, **) RailsSync.synchro_types << self.name @syncs << end |
#analyze_diffs(type = 'update') ⇒ Object
52 53 54 55 56 57 |
# File 'lib/rails_sync/active_record.rb', line 52 def analyze_diffs(type = 'update') @syncs.flat_map do || next if ![:primary_key].include?(self.primary_key) && type == 'delete' [:analyzer].analyze_diffs(type) end end |
#cache_all_diffs(*types) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/rails_sync/active_record.rb', line 66 def cache_all_diffs(*types) types = ['update', 'insert', 'delete'] if types.blank? types.each do |type| cache_diffs(type) end end |
#cache_diffs(type = 'update') ⇒ Object
59 60 61 62 63 64 |
# File 'lib/rails_sync/active_record.rb', line 59 def cache_diffs(type = 'update') @syncs.flat_map do || next if ![:primary_key].include?(self.primary_key) && type == 'delete' [:analyzer].cache_diffs(type) end end |
#id_insert? ⇒ Boolean
73 74 75 |
# File 'lib/rails_sync/active_record.rb', line 73 def id_insert? @syncs.find { |i| i[:primary_key] == ['id'] }.present? end |
#prepare_sync ⇒ Object
77 78 79 80 81 |
# File 'lib/rails_sync/active_record.rb', line 77 def prepare_sync @syncs.flat_map do || [:analyzer].reset_temp_table unless [:analyzer].same_server? end end |
#server_id ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rails_sync/active_record.rb', line 38 def server_id begin result = connection.query('select @@server_uuid') rescue result = connection.query('select @@server_id') end _id = result.to_a.flatten.first if _id.is_a?(Hash) _id.values.first else _id end end |