Class: ZeroRailsAdapter::Storage::ZeroSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/zero_rails_adapter/storage/zero_schema.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request:, transaction_class:) ⇒ ZeroSchema

Returns a new instance of ZeroSchema.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/zero_rails_adapter/storage/zero_schema.rb', line 32

def initialize(request:, transaction_class:)
  @request = request
  @transaction_class = transaction_class
  @client_model = self.class.model(
    base_class: transaction_class,
    table_name: "#{request.schema}.clients",
    primary_key: %w[clientGroupID clientID]
  )
  @mutation_result_model = self.class.model(
    base_class: transaction_class,
    table_name: "#{request.schema}.mutations",
    primary_key: %w[clientGroupID clientID mutationID]
  )
end

Instance Attribute Details

#client_modelObject (readonly)

Returns the value of attribute client_model.



29
30
31
# File 'lib/zero_rails_adapter/storage/zero_schema.rb', line 29

def client_model
  @client_model
end

#mutation_result_modelObject (readonly)

Returns the value of attribute mutation_result_model.



29
30
31
# File 'lib/zero_rails_adapter/storage/zero_schema.rb', line 29

def mutation_result_model
  @mutation_result_model
end

#requestObject (readonly)

Returns the value of attribute request.



29
30
31
# File 'lib/zero_rails_adapter/storage/zero_schema.rb', line 29

def request
  @request
end

#transaction_classObject (readonly)

Returns the value of attribute transaction_class.



29
30
31
# File 'lib/zero_rails_adapter/storage/zero_schema.rb', line 29

def transaction_class
  @transaction_class
end

Class Method Details

.model(base_class:, table_name:, primary_key:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/zero_rails_adapter/storage/zero_schema.rb', line 7

def model(base_class:, table_name:, primary_key:)
  key = [base_class, table_name, primary_key]
  models_mutex.synchronize do
    models[key] ||= Class.new(base_class) do
      self.table_name = table_name
      self.primary_key = primary_key
      self.inheritance_column = :_zero_rails_adapter_type
    end
  end
end

Instance Method Details

#cleanup(args) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zero_rails_adapter/storage/zero_schema.rb', line 73

def cleanup(args)
  scope = mutation_result_model.where("clientGroupID" => args["clientGroupID"])

  if args["type"] == "bulk"
    scope.where("clientID" => Array(args["clientIDs"])).delete_all
  elsif args["clientID"].is_a?(String) && args["upToMutationID"].is_a?(Numeric)
    scope.where("clientID" => args["clientID"])
      .where('"mutationID" <= ?', args["upToMutationID"])
      .delete_all
  end
end

#increment_lmid!(client_id) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/zero_rails_adapter/storage/zero_schema.rb', line 51

def increment_lmid!(client_id)
  attributes = client_identity(client_id)
  record = client_model.create_or_find_by!(attributes) do |client|
    client["lastMutationID"] = 1
  end
  created = record.previously_new_record?
  record.lock!
  record.update!("lastMutationID" => record["lastMutationID"].to_i + 1) unless created
  record["lastMutationID"].to_i
rescue ActiveRecord::RecordNotUnique
  retry
end

#transaction(&block) ⇒ Object



47
48
49
# File 'lib/zero_rails_adapter/storage/zero_schema.rb', line 47

def transaction(&block)
  transaction_class.transaction(requires_new: true, &block)
end

#write_result(client_id:, mutation_id:, result:) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/zero_rails_adapter/storage/zero_schema.rb', line 64

def write_result(client_id:, mutation_id:, result:)
  mutation_result_model.create!(
    "clientGroupID" => request.client_group_id,
    "clientID" => client_id,
    "mutationID" => mutation_id,
    "result" => result
  )
end