Skip to content

Commit

Permalink
Add a mix task for migrating news episodes over
Browse files Browse the repository at this point in the history
  • Loading branch information
jerodsanto committed Apr 9, 2023
1 parent 77f6464 commit f0f02d3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
20 changes: 14 additions & 6 deletions lib/changelog/schema/episode/episode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ defmodule Changelog.Episode do

field :transcript, {:array, :map}
# has_transcript is only used to know whether or not the episode has a
#transcript even though we're not `select`ing it to reduce query load times
# transcript even though we're not `select`ing it to reduce query load times
field :has_transcript, :boolean, virtual: true, default: false

# this exists merely to satisfy the compiler
Expand Down Expand Up @@ -133,14 +133,20 @@ defmodule Changelog.Episode do

def with_slug(query \\ __MODULE__, slug), do: from(q in query, where: q.slug == ^slug)

def with_slug_prefix(query \\ __MODULE__, prefix),
do: from(q in query, where: like(q.slug, ^"#{prefix}%"))

def with_podcast_slug(query \\ __MODULE__, slug)
def with_podcast_slug(query, nil), do: query

def with_podcast_slug(query, slug),
do: from(q in query, join: p in Podcast, where: q.podcast_id == p.id, where: p.slug == ^slug)

def with_transcript(query \\ __MODULE__), do: from(q in query, where: fragment("? != '{}'", q.transcript))
def sans_transcript(query \\ __MODULE__), do: from(q in query, where: fragment("? = '{}'", q.transcript))
def with_transcript(query \\ __MODULE__),
do: from(q in query, where: fragment("? != '{}'", q.transcript))

def sans_transcript(query \\ __MODULE__),
do: from(q in query, where: fragment("? = '{}'", q.transcript))

def full(query \\ __MODULE__), do: from(q in query, where: q.type == ^:full)
def bonus(query \\ __MODULE__), do: from(q in query, where: q.type == ^:bonus)
Expand Down Expand Up @@ -312,7 +318,7 @@ defmodule Changelog.Episode do
# don't, it's because they're only nominal sponsors, so have a 0 duration.
# When the episode doesn't have chapters, we guesstimate they're 60 secs
default_duration = if Enum.any?(episode.audio_chapters), do: 0, else: 60
sponsors |> Enum.map(fn(s) -> sponsor_duration(s, default_duration) end) |> Enum.sum()
sponsors |> Enum.map(fn s -> sponsor_duration(s, default_duration) end) |> Enum.sum()
end

defp sponsor_duration(sponsor, default) do
Expand Down Expand Up @@ -433,6 +439,7 @@ defmodule Changelog.Episode do
changeset
end
end

def prep_audio_file(changeset, _params), do: changeset

def prep_plusplus_file(changeset, %{"plusplus_file" => %Plug.Upload{path: path}}) do
Expand All @@ -447,6 +454,7 @@ defmodule Changelog.Episode do
changeset
end
end

def prep_plusplus_file(changeset, _params), do: changeset

def attrs_with_chapters_sorted_by_starts_at(attrs) do
Expand All @@ -464,8 +472,8 @@ defmodule Changelog.Episode do
|> Enum.with_index()
|> Map.new(fn {chapter, index} -> {"#{index}", chapter} end)
end)
rescue
KeyError -> attrs
rescue
KeyError -> attrs
end
end

Expand Down
49 changes: 49 additions & 0 deletions lib/mix/tasks/episodes/migrate_news.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
defmodule Mix.Tasks.Changelog.Episodes.MigrateNews do
use Mix.Task

alias Changelog.{Episode, Podcast, Repo}
alias ChangelogWeb.EpisodeView

import Ecto.Changeset, only: [change: 2]

@shortdoc "Migrates all news episodes of The Changelog to Software Update"

def run(_) do
Mix.Task.run("app.start")

su = Podcast.get_by_slug!("update")

Episode.published()
|> Episode.with_slug_prefix("news-")
|> Episode.newest_last()
|> Repo.all()
|> Enum.with_index(1)
|> Enum.each(fn {episode, index} ->
# Point the associated news item to the new pod
episode
|> Episode.get_news_item()
|> change(%{object_id: "#{su.id}:#{episode.id}"})
|> Repo.update!()

# Change relevant data on the episode itself
episode
|> change(%{
guid: EpisodeView.guid(episode),
podcast_id: su.id,
slug: "#{index}",
subtitle: ""
})
|> Repo.update!()

# Point all associated episode stat records to the new pod
episode
|> Ecto.assoc(:episode_stats)
|> Repo.all()
|> Enum.each(fn stat ->
stat
|> change(%{podcast_id: su.id})
|> Repo.update!()
end)
end)
end
end

0 comments on commit f0f02d3

Please sign in to comment.