pleroma.database fill_old_hashtags: Add month_limit argument
This commit is contained in:
parent
18b536c176
commit
d0c2479710
2 changed files with 43 additions and 33 deletions
|
@ -93,12 +93,14 @@ Can be safely re-run
|
||||||
|
|
||||||
## Fill hashtags for old objects
|
## Fill hashtags for old objects
|
||||||
|
|
||||||
|
Migrate hashags fields for old objects, from now to `months_limit` months.
|
||||||
|
|
||||||
```sh tab="OTP"
|
```sh tab="OTP"
|
||||||
./bin/pleroma_ctl database fill_old_hashtags
|
./bin/pleroma_ctl database fill_old_hashtags <months_limit>
|
||||||
```
|
```
|
||||||
|
|
||||||
```sh tab="From Source"
|
```sh tab="From Source"
|
||||||
mix pleroma.database fill_old_hashtags
|
mix pleroma.database fill_old_hashtags <months_limit>
|
||||||
```
|
```
|
||||||
|
|
||||||
## Vacuum the database
|
## Vacuum the database
|
||||||
|
|
|
@ -128,47 +128,55 @@ defmodule Mix.Tasks.Pleroma.Database do
|
||||||
|> Stream.run()
|
|> Stream.run()
|
||||||
end
|
end
|
||||||
|
|
||||||
def run(["fill_old_hashtags"]) do
|
def run(["fill_old_hashtags", month_limit]) do
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
|
||||||
start_pleroma()
|
start_pleroma()
|
||||||
|
|
||||||
from(
|
month_limit = String.to_integer(month_limit)
|
||||||
o in Object,
|
|
||||||
where: fragment("(?)->>'hashtags' is null", o.data),
|
|
||||||
where: fragment("(?)->>'tag' != '[]'", o.data),
|
|
||||||
select: %{id: o.id, tag: fragment("(?)->>'tag'", o.data)},
|
|
||||||
order_by: [:desc, o.id]
|
|
||||||
)
|
|
||||||
|> Pleroma.Repo.chunk_stream(200, :batches)
|
|
||||||
|> Stream.each(fn objects ->
|
|
||||||
Repo.transaction(fn ->
|
|
||||||
objects_first = objects |> List.first()
|
|
||||||
objects_last = objects |> List.last()
|
|
||||||
|
|
||||||
Logger.info(
|
if month_limit < 1 do
|
||||||
"fill_old_hashtags: #{objects_first.id} (#{objects_first.inserted_at}) -- #{
|
shell_error("Invalid `month_limit` argument, needs to be greater than 1")
|
||||||
objects_last.id
|
else
|
||||||
} (#{objects_last.inserted_at})"
|
time_limit = DateTime.utc_now() |> Timex.shift(months: -month_limit)
|
||||||
)
|
|
||||||
|
|
||||||
objects
|
from(
|
||||||
|> Enum.map(fn object ->
|
o in Object,
|
||||||
tags =
|
where: fragment("(?)->>'hashtags' is null", o.data),
|
||||||
object.tag
|
where: fragment("(?)->>'tag' != '[]'", o.data),
|
||||||
|> Jason.decode!()
|
where: o.inserted_at < ^time_limit,
|
||||||
|> Enum.filter(&is_bitstring(&1))
|
select: %{id: o.id, tag: fragment("(?)->>'tag'", o.data)}
|
||||||
|
)
|
||||||
|
|> Pleroma.Repo.chunk_stream(200, :batches)
|
||||||
|
|> Stream.each(fn objects ->
|
||||||
|
Repo.transaction(fn ->
|
||||||
|
objects_first = objects |> List.first()
|
||||||
|
objects_last = objects |> List.last()
|
||||||
|
|
||||||
Object
|
Logger.info(
|
||||||
|> where([o], o.id == ^object.id)
|
"fill_old_hashtags: #{objects_first.id} (#{objects_first.inserted_at}) -- #{
|
||||||
|> update([o],
|
objects_last.id
|
||||||
set: [data: fragment("safe_jsonb_set(?, '{hashtags}', ?, true)", o.data, ^tags)]
|
} (#{objects_last.inserted_at})"
|
||||||
)
|
)
|
||||||
|> Repo.update_all([], timeout: :infinity)
|
|
||||||
|
objects
|
||||||
|
|> Enum.map(fn object ->
|
||||||
|
tags =
|
||||||
|
object.tag
|
||||||
|
|> Jason.decode!()
|
||||||
|
|> Enum.filter(&is_bitstring(&1))
|
||||||
|
|
||||||
|
Object
|
||||||
|
|> where([o], o.id == ^object.id)
|
||||||
|
|> update([o],
|
||||||
|
set: [data: fragment("safe_jsonb_set(?, '{hashtags}', ?, true)", o.data, ^tags)]
|
||||||
|
)
|
||||||
|
|> Repo.update_all([], timeout: :infinity)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
end)
|
|> Stream.run()
|
||||||
|> Stream.run()
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def run(["vacuum", args]) do
|
def run(["vacuum", args]) do
|
||||||
|
|
Loading…
Reference in a new issue