Skip to content

Commit 14d21d1

Browse files
committed
feat: backfill existing decks with default tags
Existing decks created before the tag feature have no deck_tags. Seed the current default_deck_tags into every deck that has none. - raw-SQL/local-table migration (no app schemas) so future schema changes don't break replay - idempotent: only decks with zero tags are touched, so re-runs are no-ops
1 parent 0029d3a commit 14d21d1

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
defmodule Manavault.Repo.Migrations.BackfillDeckDefaultTags do
2+
use Ecto.Migration
3+
4+
import Ecto.Query
5+
6+
def up do
7+
flush()
8+
9+
now = DateTime.utc_now() |> DateTime.truncate(:second)
10+
11+
defaults =
12+
repo().all(
13+
from(t in "default_deck_tags",
14+
order_by: [asc: t.position],
15+
select: %{name: t.name, color: t.color, target_count: t.target_count, position: t.position}
16+
)
17+
)
18+
19+
tagged_deck_ids =
20+
repo().all(from(t in "deck_tags", distinct: true, select: t.deck_id))
21+
22+
untagged_deck_ids =
23+
repo().all(
24+
from(d in "decks", where: d.id not in ^tagged_deck_ids, select: d.id)
25+
)
26+
27+
rows =
28+
for deck_id <- untagged_deck_ids, default <- defaults do
29+
Map.merge(default, %{deck_id: deck_id, inserted_at: now, updated_at: now})
30+
end
31+
32+
if rows != [] do
33+
repo().insert_all("deck_tags", rows)
34+
end
35+
end
36+
37+
def down do
38+
# Data backfill only; cannot distinguish backfilled tags from user-created ones.
39+
:ok
40+
end
41+
end

0 commit comments

Comments
 (0)