Skip to content

Commit 9c945a3

Browse files
committed
Fix update of runtime module definitions
This also makes it more row efficient by re-using the existing records instead of recreating them on every update
1 parent 6eef996 commit 9c945a3

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

app/models/runtime_module_definition.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ class RuntimeModuleDefinition < ApplicationRecord
66
validates :host, presence: true, length: { maximum: 253 }
77
validates :endpoint, presence: true, length: { maximum: 2048 }
88
validates :port, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 65_535 }
9+
10+
def nilify_attributes!(except = %w[id runtime_module_id created_at updated_at])
11+
attribute_names.reject { |attr| except.include?(attr) }.each { |attr| self[attr] = nil }
12+
end
913
end

app/services/runtimes/grpc/modules/update_service.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,26 +112,31 @@ def update_definition_services(module_records, t)
112112
def update_module_definitions(module_records, t)
113113
modules.each do |grpc_module|
114114
runtime_module = module_records.fetch(grpc_module)
115-
runtime_module.runtime_module_definitions.delete_all
115+
db_module_definitions = runtime_module.runtime_module_definitions.first(grpc_module.definitions.length)
116116

117-
grpc_module.definitions.each do |definition|
117+
grpc_module.definitions.each_with_index do |definition, index|
118118
next unless definition.value == :endpoint
119119

120120
endpoint = definition.endpoint
121-
module_definition = runtime_module.runtime_module_definitions.build(
121+
db_module_definitions[index] ||= runtime_module.runtime_module_definitions.build
122+
db_module_definitions[index].nilify_attributes!
123+
124+
db_module_definitions[index].assign_attributes(
122125
host: endpoint.host,
123126
port: endpoint.port,
124127
endpoint: endpoint.endpoint
125128
)
126129

127-
next if module_definition.save
130+
next if db_module_definitions[index].save
128131

129132
t.rollback_and_return! ServiceResponse.error(
130133
message: 'Failed to update runtime module definition',
131134
error_code: :invalid_runtime_module_definition,
132-
details: module_definition.errors
135+
details: db_module_definitions[index].errors
133136
)
134137
end
138+
139+
runtime_module.runtime_module_definitions.excluding(db_module_definitions).delete_all
135140
end
136141
end
137142

0 commit comments

Comments
 (0)