Skip to content

Commit 0733204

Browse files
migration to create inbox and root containers for all shows and episodes
and add all nodes to these containers
1 parent ff9ab64 commit 0733204

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
defmodule Radiator.Repo.Migrations.AddNodeContainersToExistingRecords do
2+
use Ecto.Migration
3+
4+
import Ecto.Query, warn: false
5+
alias Radiator.Repo
6+
alias Radiator.Outline.{Node, NodeContainer}
7+
alias Radiator.Podcast.{Episode, Show}
8+
9+
def up do
10+
# Disable automatic timestamps for this migration
11+
Application.put_env(:radiator, :disable_timestamps, true)
12+
13+
# Add node containers to existing shows
14+
Repo.all(Show)
15+
|> Enum.each(fn show ->
16+
# Create containers
17+
{:ok, inbox} = %NodeContainer{} |> Repo.insert()
18+
{:ok, outline} = %NodeContainer{} |> Repo.insert()
19+
20+
# Update show with containers
21+
show
22+
|> Ecto.Changeset.change(%{
23+
inbox_node_container_id: inbox.id,
24+
outline_node_container_id: outline.id
25+
})
26+
|> Repo.update!()
27+
28+
# Update all nodes belonging to this show
29+
from(n in Node, where: n.show_id == ^show.id)
30+
|> Repo.update_all(set: [outline_node_container_id: outline.id])
31+
end)
32+
33+
# Add node containers to existing episodes
34+
Repo.all(Episode)
35+
|> Enum.each(fn episode ->
36+
# Create containers
37+
{:ok, inbox} = %NodeContainer{} |> Repo.insert()
38+
{:ok, outline} = %NodeContainer{} |> Repo.insert()
39+
40+
# Update episode with containers
41+
episode
42+
|> Ecto.Changeset.change(%{
43+
inbox_node_container_id: inbox.id,
44+
outline_node_container_id: outline.id
45+
})
46+
|> Repo.update!()
47+
48+
# Update all nodes belonging to this episode
49+
from(n in Node, where: n.episode_id == ^episode.id)
50+
|> Repo.update_all(set: [outline_node_container_id: outline.id])
51+
end)
52+
53+
# Re-enable automatic timestamps
54+
Application.delete_env(:radiator, :disable_timestamps)
55+
end
56+
57+
def down do
58+
# In down migration, we'll remove the container associations but keep the containers
59+
# to prevent data loss
60+
61+
# Remove container associations from shows
62+
from(s in Show)
63+
|> Repo.update_all(
64+
set: [
65+
inbox_node_container_id: nil,
66+
outline_node_container_id: nil
67+
]
68+
)
69+
70+
# Remove container associations from episodes
71+
from(e in Episode)
72+
|> Repo.update_all(
73+
set: [
74+
inbox_node_container_id: nil,
75+
outline_node_container_id: nil
76+
]
77+
)
78+
79+
# Remove container associations from nodes
80+
from(n in Node)
81+
|> Repo.update_all(set: [outline_node_container_id: nil])
82+
end
83+
end

0 commit comments

Comments
 (0)