@@ -242,18 +242,7 @@ defmodule Reencodarr.Media.VideoUpsert do
242242 { :replace_all_except , [ atom ( ) ] } | Ecto.Query . t ( )
243243 ) :: { :ok , Video . t ( ) } | { :error , Ecto.Changeset . t ( ) }
244244 defp perform_video_upsert ( attrs , on_conflict_query ) do
245- result =
246- % Video { }
247- |> Video . changeset ( attrs )
248- |> Repo . insert (
249- on_conflict: on_conflict_query ,
250- conflict_target: :path ,
251- stale_error_field: :updated_at ,
252- returning: true
253- )
254-
255- # Return the result directly, don't wrap in transaction
256- result
245+ do_insert ( attrs , on_conflict_query )
257246 end
258247
259248 @ spec perform_single_upsert_in_batch ( % { String . t ( ) => any ( ) } ) ::
@@ -262,22 +251,12 @@ defmodule Reencodarr.Media.VideoUpsert do
262251 conflict_except = determine_conflict_except_fields ( attrs )
263252 on_conflict_query = build_on_conflict_query ( attrs , conflict_except )
264253
265- result =
266- % Video { }
267- |> Video . changeset ( attrs )
268- |> Repo . insert (
269- on_conflict: on_conflict_query ,
270- conflict_target: :path ,
271- stale_error_field: :updated_at ,
272- returning: true
273- )
274-
275- case result do
254+ case do_insert ( attrs , on_conflict_query ) do
276255 { :ok , video } ->
277256 handle_successful_upsert ( video )
278257
279258 { :error , % Ecto.Changeset { errors: [ updated_at: { "is stale" , _ } ] } = changeset } ->
280- handle_stale_update_error_in_batch ( changeset , attrs )
259+ handle_stale_update_error ( changeset , attrs )
281260
282261 { :error , changeset } ->
283262 path = Map . get ( attrs , "path" , "unknown" )
@@ -286,23 +265,6 @@ defmodule Reencodarr.Media.VideoUpsert do
286265 end
287266 end
288267
289- @ spec handle_stale_update_error_in_batch (
290- Ecto.Changeset . t ( ) ,
291- % { String . t ( ) => any ( ) }
292- ) :: { :ok , Video . t ( ) } | { :error , Ecto.Changeset . t ( ) }
293- defp handle_stale_update_error_in_batch ( changeset , attrs ) do
294- # This is expected when dateAdded is not newer than updated_at - treat as success (skip)
295- path = Map . get ( attrs , "path" )
296- Logger . debug ( "Skipping update for #{ path } in batch - dateAdded not newer than updated_at" )
297-
298- # Return the existing video instead of an error
299- case Repo . get_by ( Video , path: path ) do
300- # Shouldn't happen, but handle gracefully
301- nil -> { :error , changeset }
302- existing_video -> { :ok , existing_video }
303- end
304- end
305-
306268 @ spec handle_upsert_result (
307269 { :ok , Video . t ( ) } | { :error , Ecto.Changeset . t ( ) } | { :error , any ( ) } ,
308270 % { String . t ( ) => any ( ) }
@@ -334,14 +296,22 @@ defmodule Reencodarr.Media.VideoUpsert do
334296 { :ok , video }
335297 end
336298
299+ defp do_insert ( attrs , on_conflict_query ) do
300+ % Video { }
301+ |> Video . changeset ( attrs )
302+ |> Repo . insert (
303+ on_conflict: on_conflict_query ,
304+ conflict_target: :path ,
305+ stale_error_field: :updated_at ,
306+ returning: true
307+ )
308+ end
309+
337310 defp handle_stale_update_error ( changeset , attrs ) do
338- # This is expected when dateAdded is not newer than updated_at - treat as success (skip)
339311 path = Map . get ( attrs , "path" )
340- Logger . debug ( "Skipping update for #{ path } - dateAdded not newer than updated_at" )
312+ Logger . debug ( "Skipping update for #{ path } — dateAdded not newer than updated_at" )
341313
342- # Return the existing video instead of an error
343314 case Repo . get_by ( Video , path: path ) do
344- # Shouldn't happen, but handle gracefully
345315 nil -> { :error , changeset }
346316 existing_video -> { :ok , existing_video }
347317 end
0 commit comments