From 545594f1e52a0be021284e66cdf3edf70dcb4ee8 Mon Sep 17 00:00:00 2001 From: Chris McCord Date: Mon, 17 Jan 2022 14:13:59 -0500 Subject: [PATCH] Guard against valid units --- lib/live_beats/application.ex | 2 +- lib/live_beats/media_library.ex | 4 ++-- test/live_beats/media_library_test.exs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/live_beats/application.ex b/lib/live_beats/application.ex index c459eee..98a86c8 100644 --- a/lib/live_beats/application.ex +++ b/lib/live_beats/application.ex @@ -26,7 +26,7 @@ defmodule LiveBeats.Application do name: PresenceClient}, # Start the Endpoint (http/https) LiveBeatsWeb.Endpoint, - {LiveBeats.SongsCleaner, count: 1, interval: "month"} + {LiveBeats.SongsCleaner, count: 1, interval: :month} # Start a worker by calling: LiveBeats.Worker.start_link(arg) # {LiveBeats.Worker, arg} diff --git a/lib/live_beats/media_library.ex b/lib/live_beats/media_library.ex index 3456fc3..3d6101c 100644 --- a/lib/live_beats/media_library.ex +++ b/lib/live_beats/media_library.ex @@ -366,12 +366,12 @@ defmodule LiveBeats.MediaLibrary do end end - def expire_songs_older_than(count, interval) do + def expire_songs_older_than(count, interval) when interval in [:month, :day, :second] do Ecto.Multi.new() |> Ecto.Multi.delete_all( :delete_expired_songs, from(s in Song, - where: s.inserted_at < from_now(^(-count), ^interval), + where: s.inserted_at < from_now(^(-count), ^to_string(interval)), select: %{user_id: s.user_id, mp3_filepath: s.mp3_filepath} ) ) diff --git a/test/live_beats/media_library_test.exs b/test/live_beats/media_library_test.exs index 4c2680f..a6fb892 100644 --- a/test/live_beats/media_library_test.exs +++ b/test/live_beats/media_library_test.exs @@ -86,7 +86,7 @@ defmodule LiveBeats.MediaLibraryTest do active_song = song_fixture(user_id: user.id, title: "song3", inserted_at: one_month_ago) - MediaLibrary.expire_songs_older_than(2, "month") + MediaLibrary.expire_songs_older_than(2, :month) assert_raise Ecto.NoResultsError, fn -> MediaLibrary.get_song!(expired_song_1.id) end assert_raise Ecto.NoResultsError, fn -> MediaLibrary.get_song!(expired_song_2.id) end