feat: more completemetadata_diff
This commit is contained in:
@@ -5,7 +5,9 @@ defmodule BDS.Maintenance do
|
|||||||
|
|
||||||
alias BDS.Frontmatter
|
alias BDS.Frontmatter
|
||||||
alias BDS.Media.Media
|
alias BDS.Media.Media
|
||||||
|
alias BDS.Media.Translation, as: MediaTranslation
|
||||||
alias BDS.Posts.Post
|
alias BDS.Posts.Post
|
||||||
|
alias BDS.Posts.Translation, as: PostTranslation
|
||||||
alias BDS.Projects
|
alias BDS.Projects
|
||||||
alias BDS.Repo
|
alias BDS.Repo
|
||||||
alias BDS.Scripts.Script
|
alias BDS.Scripts.Script
|
||||||
@@ -27,7 +29,9 @@ defmodule BDS.Maintenance do
|
|||||||
|
|
||||||
diff_reports =
|
diff_reports =
|
||||||
post_diff_reports(project_id, project) ++
|
post_diff_reports(project_id, project) ++
|
||||||
|
post_translation_diff_reports(project_id, project) ++
|
||||||
media_diff_reports(project_id, project) ++
|
media_diff_reports(project_id, project) ++
|
||||||
|
media_translation_diff_reports(project_id, project) ++
|
||||||
script_diff_reports(project_id, project) ++
|
script_diff_reports(project_id, project) ++
|
||||||
template_diff_reports(project_id, project)
|
template_diff_reports(project_id, project)
|
||||||
|
|
||||||
@@ -110,6 +114,65 @@ defmodule BDS.Maintenance do
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp post_translation_diff_reports(project_id, project) do
|
||||||
|
Repo.all(
|
||||||
|
from translation in PostTranslation,
|
||||||
|
where: translation.project_id == ^project_id and not is_nil(translation.file_path) and translation.file_path != ""
|
||||||
|
)
|
||||||
|
|> Enum.flat_map(fn translation ->
|
||||||
|
case read_frontmatter_document(project, translation.file_path) do
|
||||||
|
{:ok, %{fields: fields}} ->
|
||||||
|
differences =
|
||||||
|
[
|
||||||
|
diff_field("title", translation.title, Map.get(fields, "title")),
|
||||||
|
diff_field("excerpt", translation.excerpt, Map.get(fields, "excerpt")),
|
||||||
|
diff_field("language", translation.language, Map.get(fields, "language")),
|
||||||
|
diff_field("status", translation.status, Map.get(fields, "status")),
|
||||||
|
diff_field("translation_for", translation.translation_for, Map.get(fields, "translation_for"))
|
||||||
|
]
|
||||||
|
|> Enum.reject(&is_nil/1)
|
||||||
|
|
||||||
|
if differences == [] do
|
||||||
|
[]
|
||||||
|
else
|
||||||
|
[%{entity_type: "post_translation", entity_id: translation.id, differences: differences}]
|
||||||
|
end
|
||||||
|
|
||||||
|
{:error, _reason} ->
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp media_translation_diff_reports(project_id, project) do
|
||||||
|
Repo.all(from translation in MediaTranslation, where: translation.project_id == ^project_id)
|
||||||
|
|> Enum.flat_map(fn translation ->
|
||||||
|
sidecar_path = media_translation_sidecar_path(project_id, translation)
|
||||||
|
|
||||||
|
case sidecar_path && read_sidecar_document(project, sidecar_path) do
|
||||||
|
{:ok, fields} ->
|
||||||
|
differences =
|
||||||
|
[
|
||||||
|
diff_field("title", translation.title, Map.get(fields, "title")),
|
||||||
|
diff_field("alt", translation.alt, Map.get(fields, "alt")),
|
||||||
|
diff_field("caption", translation.caption, Map.get(fields, "caption")),
|
||||||
|
diff_field("language", translation.language, Map.get(fields, "language")),
|
||||||
|
diff_field("translation_for", translation.translation_for, Map.get(fields, "translation_for"))
|
||||||
|
]
|
||||||
|
|> Enum.reject(&is_nil/1)
|
||||||
|
|
||||||
|
if differences == [] do
|
||||||
|
[]
|
||||||
|
else
|
||||||
|
[%{entity_type: "media_translation", entity_id: translation.id, differences: differences}]
|
||||||
|
end
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
defp script_diff_reports(project_id, project) do
|
defp script_diff_reports(project_id, project) do
|
||||||
Repo.all(
|
Repo.all(
|
||||||
from script in Script,
|
from script in Script,
|
||||||
@@ -168,6 +231,8 @@ defmodule BDS.Maintenance do
|
|||||||
defp orphan_reports(project_id, project) do
|
defp orphan_reports(project_id, project) do
|
||||||
post_paths = MapSet.new(Repo.all(from post in Post, where: post.project_id == ^project_id, select: post.file_path))
|
post_paths = MapSet.new(Repo.all(from post in Post, where: post.project_id == ^project_id, select: post.file_path))
|
||||||
media_paths = MapSet.new(Repo.all(from media in Media, where: media.project_id == ^project_id, select: media.sidecar_path))
|
media_paths = MapSet.new(Repo.all(from media in Media, where: media.project_id == ^project_id, select: media.sidecar_path))
|
||||||
|
post_translation_paths = MapSet.new(Repo.all(from translation in PostTranslation, where: translation.project_id == ^project_id, select: translation.file_path))
|
||||||
|
media_translation_paths = MapSet.new(media_translation_sidecar_paths(project_id))
|
||||||
script_paths = MapSet.new(Repo.all(from script in Script, where: script.project_id == ^project_id, select: script.file_path))
|
script_paths = MapSet.new(Repo.all(from script in Script, where: script.project_id == ^project_id, select: script.file_path))
|
||||||
template_paths = MapSet.new(Repo.all(from template in Template, where: template.project_id == ^project_id, select: template.file_path))
|
template_paths = MapSet.new(Repo.all(from template in Template, where: template.project_id == ^project_id, select: template.file_path))
|
||||||
|
|
||||||
@@ -175,8 +240,16 @@ defmodule BDS.Maintenance do
|
|||||||
project
|
project
|
||||||
|> list_project_files("posts/**/*.md")
|
|> list_project_files("posts/**/*.md")
|
||||||
|> Enum.map(&Path.relative_to(&1, Projects.project_data_dir(project)))
|
|> Enum.map(&Path.relative_to(&1, Projects.project_data_dir(project)))
|
||||||
|
|> Enum.reject(&translation_post_file?/1)
|
||||||
|> Enum.reject(&MapSet.member?(post_paths, &1))
|
|> Enum.reject(&MapSet.member?(post_paths, &1))
|
||||||
|
|
||||||
|
post_translation_orphans =
|
||||||
|
project
|
||||||
|
|> list_project_files("posts/**/*.md")
|
||||||
|
|> Enum.map(&Path.relative_to(&1, Projects.project_data_dir(project)))
|
||||||
|
|> Enum.filter(&translation_post_file?/1)
|
||||||
|
|> Enum.reject(&MapSet.member?(post_translation_paths, &1))
|
||||||
|
|
||||||
media_orphans =
|
media_orphans =
|
||||||
project
|
project
|
||||||
|> list_project_files("media/**/*.meta")
|
|> list_project_files("media/**/*.meta")
|
||||||
@@ -184,6 +257,13 @@ defmodule BDS.Maintenance do
|
|||||||
|> Enum.filter(&canonical_media_sidecar?/1)
|
|> Enum.filter(&canonical_media_sidecar?/1)
|
||||||
|> Enum.reject(&MapSet.member?(media_paths, &1))
|
|> Enum.reject(&MapSet.member?(media_paths, &1))
|
||||||
|
|
||||||
|
media_translation_orphans =
|
||||||
|
project
|
||||||
|
|> list_project_files("media/**/*.meta")
|
||||||
|
|> Enum.map(&Path.relative_to(&1, Projects.project_data_dir(project)))
|
||||||
|
|> Enum.filter(&translation_media_sidecar?/1)
|
||||||
|
|> Enum.reject(&MapSet.member?(media_translation_paths, &1))
|
||||||
|
|
||||||
script_orphans =
|
script_orphans =
|
||||||
project
|
project
|
||||||
|> list_project_files("scripts/**/*.lua")
|
|> list_project_files("scripts/**/*.lua")
|
||||||
@@ -196,7 +276,7 @@ defmodule BDS.Maintenance do
|
|||||||
|> Enum.map(&Path.relative_to(&1, Projects.project_data_dir(project)))
|
|> Enum.map(&Path.relative_to(&1, Projects.project_data_dir(project)))
|
||||||
|> Enum.reject(&MapSet.member?(template_paths, &1))
|
|> Enum.reject(&MapSet.member?(template_paths, &1))
|
||||||
|
|
||||||
(post_orphans ++ media_orphans ++ script_orphans ++ template_orphans)
|
(post_orphans ++ post_translation_orphans ++ media_orphans ++ media_translation_orphans ++ script_orphans ++ template_orphans)
|
||||||
|> Enum.sort()
|
|> Enum.sort()
|
||||||
|> Enum.map(&%{file_path: &1})
|
|> Enum.map(&%{file_path: &1})
|
||||||
end
|
end
|
||||||
@@ -249,4 +329,25 @@ defmodule BDS.Maintenance do
|
|||||||
defp canonical_media_sidecar?(relative_path) do
|
defp canonical_media_sidecar?(relative_path) do
|
||||||
not Regex.match?(~r/\.[a-z]{2}\.meta$/i, relative_path)
|
not Regex.match?(~r/\.[a-z]{2}\.meta$/i, relative_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp translation_post_file?(relative_path) do
|
||||||
|
Regex.match?(~r/\.[a-z]{2}\.md$/i, relative_path)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp translation_media_sidecar?(relative_path) do
|
||||||
|
Regex.match?(~r/\.[a-z]{2}\.meta$/i, relative_path)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp media_translation_sidecar_paths(project_id) do
|
||||||
|
Repo.all(from translation in MediaTranslation, where: translation.project_id == ^project_id)
|
||||||
|
|> Enum.map(&media_translation_sidecar_path(project_id, &1))
|
||||||
|
|> Enum.reject(&is_nil/1)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp media_translation_sidecar_path(project_id, translation) do
|
||||||
|
case Repo.one(from media in Media, where: media.project_id == ^project_id and media.id == ^translation.translation_for, select: media.file_path) do
|
||||||
|
nil -> nil
|
||||||
|
file_path -> "#{file_path}.#{translation.language}.meta"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -141,6 +141,16 @@ defmodule BDS.MaintenanceTest do
|
|||||||
|
|
||||||
assert {:ok, published_post} = BDS.Posts.publish_post(post.id)
|
assert {:ok, published_post} = BDS.Posts.publish_post(post.id)
|
||||||
|
|
||||||
|
assert {:ok, post_translation} =
|
||||||
|
BDS.Posts.upsert_post_translation(published_post.id, "de", %{
|
||||||
|
title: "Ursprunglicher Beitrag",
|
||||||
|
excerpt: "Zusammenfassung",
|
||||||
|
content: "Ubersetzter Inhalt"
|
||||||
|
})
|
||||||
|
|
||||||
|
assert {:ok, _republished_post} = BDS.Posts.publish_post(published_post.id)
|
||||||
|
published_post_translation = Repo.get!(BDS.Posts.Translation, post_translation.id)
|
||||||
|
|
||||||
assert {:ok, media} =
|
assert {:ok, media} =
|
||||||
BDS.Media.import_media(%{
|
BDS.Media.import_media(%{
|
||||||
project_id: project.id,
|
project_id: project.id,
|
||||||
@@ -153,6 +163,13 @@ defmodule BDS.MaintenanceTest do
|
|||||||
tags: ["alpha"]
|
tags: ["alpha"]
|
||||||
})
|
})
|
||||||
|
|
||||||
|
assert {:ok, media_translation} =
|
||||||
|
BDS.Media.upsert_media_translation(media.id, "de", %{
|
||||||
|
title: "Ubersetzter Medientitel",
|
||||||
|
alt: "Ubersetzter Alt-Text",
|
||||||
|
caption: "Ubersetzte Beschriftung"
|
||||||
|
})
|
||||||
|
|
||||||
assert {:ok, script} =
|
assert {:ok, script} =
|
||||||
BDS.Scripts.create_script(%{
|
BDS.Scripts.create_script(%{
|
||||||
project_id: project.id,
|
project_id: project.id,
|
||||||
@@ -202,6 +219,27 @@ defmodule BDS.MaintenanceTest do
|
|||||||
|> Enum.join("\n")
|
|> Enum.join("\n")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
post_translation_path = Path.join(temp_dir, published_post_translation.file_path)
|
||||||
|
File.write!(
|
||||||
|
post_translation_path,
|
||||||
|
[
|
||||||
|
"---",
|
||||||
|
"id: #{published_post_translation.id}",
|
||||||
|
"translation_for: #{published_post_translation.translation_for}",
|
||||||
|
"language: #{published_post_translation.language}",
|
||||||
|
"title: Bearbeiteter Beitrag",
|
||||||
|
"excerpt: Bearbeitete Zusammenfassung",
|
||||||
|
"status: published",
|
||||||
|
"created_at: #{published_post_translation.created_at}",
|
||||||
|
"updated_at: #{published_post_translation.updated_at}",
|
||||||
|
"published_at: #{published_post_translation.published_at}",
|
||||||
|
"---",
|
||||||
|
"Bearbeiteter Inhalt",
|
||||||
|
""
|
||||||
|
]
|
||||||
|
|> Enum.join("\n")
|
||||||
|
)
|
||||||
|
|
||||||
media_sidecar_path = Path.join(temp_dir, media.sidecar_path)
|
media_sidecar_path = Path.join(temp_dir, media.sidecar_path)
|
||||||
File.write!(
|
File.write!(
|
||||||
media_sidecar_path,
|
media_sidecar_path,
|
||||||
@@ -224,6 +262,20 @@ defmodule BDS.MaintenanceTest do
|
|||||||
|> Enum.join("\n")
|
|> Enum.join("\n")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
media_translation_sidecar_path = Path.join(temp_dir, "#{media.file_path}.#{media_translation.language}.meta")
|
||||||
|
File.write!(
|
||||||
|
media_translation_sidecar_path,
|
||||||
|
[
|
||||||
|
"translation_for: #{media.id}",
|
||||||
|
"language: #{media_translation.language}",
|
||||||
|
"title: Bearbeiteter Medientitel",
|
||||||
|
"alt: Bearbeiteter Alt-Text",
|
||||||
|
"caption: Bearbeitete Beschriftung",
|
||||||
|
""
|
||||||
|
]
|
||||||
|
|> Enum.join("\n")
|
||||||
|
)
|
||||||
|
|
||||||
script_path = Path.join(temp_dir, published_script.file_path)
|
script_path = Path.join(temp_dir, published_script.file_path)
|
||||||
File.write!(
|
File.write!(
|
||||||
script_path,
|
script_path,
|
||||||
@@ -266,8 +318,10 @@ defmodule BDS.MaintenanceTest do
|
|||||||
)
|
)
|
||||||
|
|
||||||
File.write!(Path.join([temp_dir, "posts", "2026", "04", "orphan-post.md"]), "---\nid: orphan\ntitle: Orphan\nslug: orphan\nstatus: published\ncreated_at: 1\nupdated_at: 1\npublished_at: 1\ntags:\ncategories:\n---\nBody\n")
|
File.write!(Path.join([temp_dir, "posts", "2026", "04", "orphan-post.md"]), "---\nid: orphan\ntitle: Orphan\nslug: orphan\nstatus: published\ncreated_at: 1\nupdated_at: 1\npublished_at: 1\ntags:\ncategories:\n---\nBody\n")
|
||||||
|
File.write!(Path.join([temp_dir, "posts", "2026", "04", "orphan-post.es.md"]), "---\nid: orphan-post-translation\ntranslation_for: orphan\nlanguage: es\ntitle: Huerfano\nstatus: published\ncreated_at: 1\nupdated_at: 1\npublished_at: 1\n---\nCuerpo\n")
|
||||||
File.write!(Path.join([temp_dir, "media", "2026", "04", "orphan.txt"]), "orphan")
|
File.write!(Path.join([temp_dir, "media", "2026", "04", "orphan.txt"]), "orphan")
|
||||||
File.write!(Path.join([temp_dir, "media", "2026", "04", "orphan.txt.meta"]), "id: orphan-media\noriginal_name: orphan.txt\nmime_type: text/plain\nsize: 6\ncreated_at: 1\nupdated_at: 1\ntags:\n")
|
File.write!(Path.join([temp_dir, "media", "2026", "04", "orphan.txt.meta"]), "id: orphan-media\noriginal_name: orphan.txt\nmime_type: text/plain\nsize: 6\ncreated_at: 1\nupdated_at: 1\ntags:\n")
|
||||||
|
File.write!(Path.join([temp_dir, "media", "2026", "04", "orphan.txt.es.meta"]), "translation_for: orphan-media\nlanguage: es\ntitle: Huerfano\nalt: Texto\ncaption: Leyenda\n")
|
||||||
File.write!(Path.join([temp_dir, "scripts", "orphan.lua"]), "---\nid: orphan-script\nslug: orphan-script\ntitle: Orphan Script\nkind: utility\nentrypoint: main\nenabled: true\nversion: 1\ncreated_at: 1\nupdated_at: 1\n---\nfunction main() return true end\n")
|
File.write!(Path.join([temp_dir, "scripts", "orphan.lua"]), "---\nid: orphan-script\nslug: orphan-script\ntitle: Orphan Script\nkind: utility\nentrypoint: main\nenabled: true\nversion: 1\ncreated_at: 1\nupdated_at: 1\n---\nfunction main() return true end\n")
|
||||||
File.write!(Path.join([temp_dir, "templates", "orphan-view.liquid"]), "---\nid: orphan-template\nslug: orphan-view\ntitle: Orphan View\nkind: list\nenabled: true\nversion: 1\ncreated_at: 1\nupdated_at: 1\n---\n<section>Orphan</section>\n")
|
File.write!(Path.join([temp_dir, "templates", "orphan-view.liquid"]), "---\nid: orphan-template\nslug: orphan-view\ntitle: Orphan View\nkind: list\nenabled: true\nversion: 1\ncreated_at: 1\nupdated_at: 1\n---\n<section>Orphan</section>\n")
|
||||||
|
|
||||||
@@ -297,9 +351,23 @@ defmodule BDS.MaintenanceTest do
|
|||||||
Enum.any?(report.differences, &(&1.name == "enabled" and &1.file_value == "false"))
|
Enum.any?(report.differences, &(&1.name == "enabled" and &1.file_value == "false"))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
assert Enum.any?(diff_reports, fn report ->
|
||||||
|
report.entity_type == "post_translation" and report.entity_id == published_post_translation.id and
|
||||||
|
Enum.any?(report.differences, &(&1.name == "title" and &1.db_value == "Ursprunglicher Beitrag" and &1.file_value == "Bearbeiteter Beitrag")) and
|
||||||
|
Enum.any?(report.differences, &(&1.name == "excerpt" and &1.db_value == "Zusammenfassung" and &1.file_value == "Bearbeitete Zusammenfassung"))
|
||||||
|
end)
|
||||||
|
|
||||||
|
assert Enum.any?(diff_reports, fn report ->
|
||||||
|
report.entity_type == "media_translation" and report.entity_id == media_translation.id and
|
||||||
|
Enum.any?(report.differences, &(&1.name == "title" and &1.db_value == "Ubersetzter Medientitel" and &1.file_value == "Bearbeiteter Medientitel")) and
|
||||||
|
Enum.any?(report.differences, &(&1.name == "alt" and &1.db_value == "Ubersetzter Alt-Text" and &1.file_value == "Bearbeiteter Alt-Text"))
|
||||||
|
end)
|
||||||
|
|
||||||
orphan_paths = Enum.map(orphan_reports, & &1.file_path)
|
orphan_paths = Enum.map(orphan_reports, & &1.file_path)
|
||||||
assert "posts/2026/04/orphan-post.md" in orphan_paths
|
assert "posts/2026/04/orphan-post.md" in orphan_paths
|
||||||
|
assert "posts/2026/04/orphan-post.es.md" in orphan_paths
|
||||||
assert "media/2026/04/orphan.txt.meta" in orphan_paths
|
assert "media/2026/04/orphan.txt.meta" in orphan_paths
|
||||||
|
assert "media/2026/04/orphan.txt.es.meta" in orphan_paths
|
||||||
assert "scripts/orphan.lua" in orphan_paths
|
assert "scripts/orphan.lua" in orphan_paths
|
||||||
assert "templates/orphan-view.liquid" in orphan_paths
|
assert "templates/orphan-view.liquid" in orphan_paths
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user