@@ -21,8 +21,6 @@ defmodule Astarte.Pairing.Queries do
2121 This module is responsible for the interaction with the database.
2222 """
2323
24- alias CQEx.Client
25- alias CQEx.Query
2624 alias Astarte.Core.CQLUtils
2725 alias Astarte.Pairing.Config
2826 alias Astarte.Pairing.Astarte.Realm
@@ -103,9 +101,8 @@ defmodule Astarte.Pairing.Queries do
103101
104102 do_register_unconfirmed_device (
105103 realm_name ,
106- device_id ,
104+ device ,
107105 credentials_secret ,
108- device . first_registration ,
109106 opts
110107 )
111108 else
@@ -249,113 +246,52 @@ defmodule Astarte.Pairing.Queries do
249246 % DateTime { } = registration_timestamp ,
250247 opts
251248 ) do
252- statement = """
253- INSERT INTO devices
254- (device_id, first_registration, credentials_secret, inhibit_credentials_request,
255- protocol_revision, total_received_bytes, total_received_msgs, introspection,
256- introspection_minor)
257- VALUES
258- (:device_id, :first_registration, :credentials_secret, :inhibit_credentials_request,
259- :protocol_revision, :total_received_bytes, :total_received_msgs, :introspection,
260- :introspection_minor)
261- """
262-
263249 { introspection , introspection_minor } =
264250 opts
265251 |> Keyword . get ( :initial_introspection , [ ] )
266252 |> build_initial_introspection_maps ( )
267253
268- query =
269- Query . new ( )
270- |> Query . statement ( statement )
271- |> Query . put ( :device_id , device_id )
272- |> Query . put ( :first_registration , registration_timestamp |> DateTime . to_unix ( :millisecond ) )
273- |> Query . put ( :credentials_secret , credentials_secret )
274- |> Query . put ( :inhibit_credentials_request , false )
275- |> Query . put ( :protocol_revision , 0 )
276- |> Query . put ( :total_received_bytes , 0 )
277- |> Query . put ( :total_received_msgs , 0 )
278- |> Query . put ( :introspection , introspection )
279- |> Query . put ( :introspection_minor , introspection_minor )
280- |> Query . consistency ( :quorum )
281-
282254 keyspace_name =
283255 CQLUtils . realm_name_to_keyspace_name ( realm_name , Config . astarte_instance_id! ( ) )
284256
285- cqex_options =
286- Config . cqex_options! ( )
287- |> Keyword . put ( :keyspace , keyspace_name )
288-
289- with { :ok , client } <-
290- Client . new (
291- Config . cassandra_node! ( ) ,
292- cqex_options
293- ) ,
294- { :ok , _res } <- Query . call ( client , query ) do
295- :ok
296- else
297- error ->
298- Logger . warning ( "DB error: #{ inspect ( error ) } " )
299- { :error , :database_error }
300- end
257+ % Device { }
258+ |> Ecto.Changeset . change ( % {
259+ device_id: device_id ,
260+ first_registration: registration_timestamp ,
261+ credentials_secret: credentials_secret ,
262+ inhibit_credentials_request: false ,
263+ protocol_revision: 0 ,
264+ total_received_bytes: 0 ,
265+ total_received_msgs: 0 ,
266+ introspection: introspection ,
267+ introspection_minor: introspection_minor
268+ } )
269+ |> Repo . insert ( prefix: keyspace_name , consistency: :quorum )
301270 end
302271
303272 defp do_register_unconfirmed_device (
304273 realm_name ,
305- device_id ,
274+ % Device { } = device ,
306275 credentials_secret ,
307- % DateTime { } = registration_timestamp ,
308276 opts
309277 ) do
310- statement = """
311- UPDATE devices
312- SET
313- first_registration = :first_registration,
314- credentials_secret = :credentials_secret,
315- inhibit_credentials_request = :inhibit_credentials_request,
316- protocol_revision = :protocol_revision,
317- introspection = :introspection,
318- introspection_minor = :introspection_minor
319-
320- WHERE device_id = :device_id
321- """
322-
323278 { introspection , introspection_minor } =
324279 opts
325280 |> Keyword . get ( :initial_introspection , [ ] )
326281 |> build_initial_introspection_maps ( )
327282
328- query =
329- Query . new ( )
330- |> Query . statement ( statement )
331- |> Query . put ( :device_id , device_id )
332- |> Query . put ( :first_registration , registration_timestamp |> DateTime . to_unix ( :millisecond ) )
333- |> Query . put ( :credentials_secret , credentials_secret )
334- |> Query . put ( :inhibit_credentials_request , false )
335- |> Query . put ( :protocol_revision , 0 )
336- |> Query . put ( :introspection , introspection )
337- |> Query . put ( :introspection_minor , introspection_minor )
338- |> Query . consistency ( :quorum )
339-
340283 keyspace_name =
341284 CQLUtils . realm_name_to_keyspace_name ( realm_name , Config . astarte_instance_id! ( ) )
342285
343- cqex_options =
344- Config . cqex_options! ( )
345- |> Keyword . put ( :keyspace , keyspace_name )
346-
347- with { :ok , client } <-
348- Client . new (
349- Config . cassandra_node! ( ) ,
350- cqex_options
351- ) ,
352- { :ok , _res } <- Query . call ( client , query ) do
353- :ok
354- else
355- error ->
356- Logger . warning ( "DB error: #{ inspect ( error ) } " )
357- { :error , :database_error }
358- end
286+ device
287+ |> Ecto.Changeset . change ( % {
288+ credentials_secret: credentials_secret ,
289+ inhibit_credentials_request: false ,
290+ protocol_revision: 0 ,
291+ introspection: introspection ,
292+ introspection_minor: introspection_minor
293+ } )
294+ |> Repo . update ( prefix: keyspace_name , consistency: :quorum )
359295 end
360296
361297 defp build_initial_introspection_maps ( initial_introspection ) do
0 commit comments