mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 21:47:59 +01:00
Add mass conversion task
This commit is contained in:
parent
e1e20c5159
commit
21e58b486f
1 changed files with 54 additions and 0 deletions
54
lib/mix/tasks/convert_to_markdown.ex
Normal file
54
lib/mix/tasks/convert_to_markdown.ex
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
defmodule Mix.Tasks.ConvertToMarkdown do
|
||||||
|
use Mix.Task
|
||||||
|
|
||||||
|
import Ecto.Query
|
||||||
|
alias Philomena.Repo
|
||||||
|
alias Philomena.Batch
|
||||||
|
alias PhilomenaWeb.TextileMarkdownRenderer
|
||||||
|
|
||||||
|
@modules [
|
||||||
|
{Philomena.Badges.Badge, [:description]},
|
||||||
|
{Philomena.Channels.Channel, [:description]},
|
||||||
|
{Philomena.Comments.Comment, [:body]},
|
||||||
|
{Philomena.Commissions.Commission, [:contact, :information, :will_create, :will_not_create]},
|
||||||
|
{Philomena.Commissions.Item, [:description, :add_ons]},
|
||||||
|
{Philomena.Conversations.Message, [:body]},
|
||||||
|
{Philomena.DnpEntries.DnpEntry, [:conditions, :reason, :instructions]},
|
||||||
|
{Philomena.Filters.Filter, [:description]},
|
||||||
|
{Philomena.Galleries.Gallery, [:description]},
|
||||||
|
{Philomena.Images.Image, [:description]},
|
||||||
|
{Philomena.ModNotes.ModNote, [:body]},
|
||||||
|
{Philomena.Posts.Post, [:body]},
|
||||||
|
{Philomena.Reports.Report, [:report]},
|
||||||
|
{Philomena.Tags.Tag, [:description]},
|
||||||
|
{Philomena.Users.User, [:description, :scratchpad]},
|
||||||
|
]
|
||||||
|
|
||||||
|
@shortdoc "Rewrites all database rows from Textile to Markdown."
|
||||||
|
@requirements ["app.start"]
|
||||||
|
@impl Mix.Task
|
||||||
|
def run(args) do
|
||||||
|
if Mix.env() == :prod and not Enum.member?(args, "--i-know-what-im-doing") do
|
||||||
|
raise "do not run this task in production unless you know what you're doing"
|
||||||
|
end
|
||||||
|
|
||||||
|
Enum.map(@modules, fn {mod, fields} ->
|
||||||
|
Batch.record_batches(mod, fn batch ->
|
||||||
|
Enum.map(batch, fn item ->
|
||||||
|
updates = Enum.reduce(fields, [], fn field, kwlist ->
|
||||||
|
fval = Map.fetch!(item, field)
|
||||||
|
[{:"#{field}_md", TextileMarkdownRenderer.render_one(%{body: fval})} | kwlist]
|
||||||
|
end)
|
||||||
|
|
||||||
|
(mod
|
||||||
|
|> where(id: ^item.id)
|
||||||
|
|> Repo.update_all(set: updates))
|
||||||
|
|
||||||
|
IO.write("\r#{mod}\t#{item.id}\t")
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
IO.puts("")
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue