Class: WatermelonDbSync::SyncPush

Inherits:
Sync
  • Object
show all
Defined in:
lib/watermelon_db_sync/sync_push.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Sync

last_global_seqs, next_global_seqs

Constructor Details

#initialize(params) ⇒ SyncPush



6
7
8
9
10
11
# File 'lib/watermelon_db_sync/sync_push.rb', line 6

def initialize(params)
  @models = WatermelonDbSync.configuration.sync_models
  @params = params
  @data = {last_global_seqs:0}
  @push_id = rand(1..1_000_000_000)
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/watermelon_db_sync/sync_push.rb', line 4

def data
  @data
end

#modelsObject

Returns the value of attribute models.



4
5
6
# File 'lib/watermelon_db_sync/sync_push.rb', line 4

def models
  @models
end

#paramsObject

Returns the value of attribute params.



4
5
6
# File 'lib/watermelon_db_sync/sync_push.rb', line 4

def params
  @params
end

Instance Method Details

#except_data(data) ⇒ Object



79
80
81
# File 'lib/watermelon_db_sync/sync_push.rb', line 79

def except_data(data)
  data.except("version", "version_created", "created_at_server", "updated_at_server", "deleted_at_server", "push_id", "_status", "_changed")
end

#has_conflict_version?Boolean



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/watermelon_db_sync/sync_push.rb', line 83

def has_conflict_version?
    conflict = false
    @models.each do |model|
    
    #keep original object @params
    params = Marshal.load(Marshal.dump(@params))
    table = params[model.tableize]
    if (params.keys.include? eval(model).table_name)
      collect_ids = table["created"]

      if table["updated"].present?
        updated = table["updated"]
        collect_ids.concat(updated)
      end

      collect_ids = collect_ids&.map{|d|d["id"]}
      collect_ids.concat(table["deleted"]) if table["deleted"].present? 
      conflict = eval(model).with_deleted
                          .where(id: collect_ids)
                          .where("version_created > #{@params["last_pulled_version"]} OR version > #{@params["last_pulled_version"]}").exists?
    end
      break if conflict
    end
    return conflict
end

#pushObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/watermelon_db_sync/sync_push.rb', line 13

def push
  begin
      if has_conflict_version?
        @data[:error_code] = "WDBS2"
        raise StandardError.new("the data has a conflict version, please pull first")
      else
        ActiveRecord::Base.transaction do
          @models.each do |model|
            self.submit_records!(model) if @params.keys.include? eval(model).table_name
          end
            @data[:success] = true
            sync_pull = SyncPull.new(last_pulled_version: @params["last_pulled_version"], push_id: @push_id)
            sync_pull.pull
            @data[:response] = sync_pull.data[:response]
            @data[:last_global_seqs] = sync_pull.data[:last_global_seqs]
        end
      end
  rescue StandardError => e
    @data[:success] = false
    @data[:message] = e.message
  end
end

#submit_records!(model) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/watermelon_db_sync/sync_push.rb', line 37

def submit_records!(model)
  table = @params[model.tableize]
  table["created"]&.each do |data|
    data = except_data(data)
    data["push_id"] = @push_id

    find_or_new = eval(model).find(data["id"]) rescue nil
    if find_or_new.blank?
      find_or_new = eval(model).new(data)
      find_or_new.id = data["id"]
      find_or_new.created_at = data["created_at"]
      find_or_new.updated_at = data["updated_at"]
      find_or_new.save!
    else
      find_or_new.update!(data)
    end
  end

  table["updated"]&.each do |data|
    data = except_data(data)
    data["push_id"] = @push_id

    find_record = eval(model).find(data["id"]) rescue nil

    if find_record.nil? && eval(model).only_deleted.find(data["id"])
      @data[:error_code] = "WDBS1"
      raise StandardError.new("id=#{data["id"]} of the #{model} was deleted, please pull first")
    end
    find_record.update!(data.except("id"))
  end

  table["deleted"]&.each do |data|
    find_record = eval(model).find(data) rescue nil

    if find_record.present?
      find_record.update_column(:push_id, @push_id)
      find_record.destroy
    end
  end
  
end