Guard against valid units

This commit is contained in:
Chris McCord 2022-01-17 14:13:59 -05:00
parent e4dd585e39
commit 545594f1e5
3 changed files with 4 additions and 4 deletions

View file

@ -26,7 +26,7 @@ defmodule LiveBeats.Application do
name: PresenceClient}, name: PresenceClient},
# Start the Endpoint (http/https) # Start the Endpoint (http/https)
LiveBeatsWeb.Endpoint, LiveBeatsWeb.Endpoint,
{LiveBeats.SongsCleaner, count: 1, interval: "month"} {LiveBeats.SongsCleaner, count: 1, interval: :month}
# Start a worker by calling: LiveBeats.Worker.start_link(arg) # Start a worker by calling: LiveBeats.Worker.start_link(arg)
# {LiveBeats.Worker, arg} # {LiveBeats.Worker, arg}

View file

@ -366,12 +366,12 @@ defmodule LiveBeats.MediaLibrary do
end end
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.new()
|> Ecto.Multi.delete_all( |> Ecto.Multi.delete_all(
:delete_expired_songs, :delete_expired_songs,
from(s in Song, 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} select: %{user_id: s.user_id, mp3_filepath: s.mp3_filepath}
) )
) )

View file

@ -86,7 +86,7 @@ defmodule LiveBeats.MediaLibraryTest do
active_song = song_fixture(user_id: user.id, title: "song3", inserted_at: one_month_ago) 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_1.id) end
assert_raise Ecto.NoResultsError, fn -> MediaLibrary.get_song!(expired_song_2.id) end assert_raise Ecto.NoResultsError, fn -> MediaLibrary.get_song!(expired_song_2.id) end