Added UUID backfill to a migration

This commit is contained in:
Kieran Eglin 2024-03-25 15:25:44 -07:00
parent ab47bdbdf5
commit 05e7fa3d2e
No known key found for this signature in database
GPG key ID: 193984967FCF432D
3 changed files with 13 additions and 15 deletions

View file

@ -36,7 +36,6 @@ defmodule Pinchflat.Boot.PreJobStartupTasks do
reset_executing_jobs()
create_blank_cookie_file()
apply_default_settings()
backfill_uuids()
{:ok, state}
end
@ -68,18 +67,4 @@ defmodule Pinchflat.Boot.PreJobStartupTasks do
Settings.fetch!(:onboarding, true)
Settings.fetch!(:pro_enabled, false)
end
# TODO: turn into a migration
defp backfill_uuids do
# This is a one-time backfill to ensure that all media items have a UUID
# This is important for the RSS feed and the streaming endpoint
source_query = from(m in Source, where: is_nil(m.uuid), update: [set: [uuid: fragment("gen_random_uuid()")]])
media_item_query = from(m in MediaItem, where: is_nil(m.uuid), update: [set: [uuid: fragment("gen_random_uuid()")]])
{source_count, _} = Repo.update_all(source_query, [])
{media_item_count, _} = Repo.update_all(media_item_query, [])
Logger.info("Backfilled UUIDs for #{source_count} sources.")
Logger.info("Backfilled UUIDs for #{media_item_count} media items.")
end
end

View file

@ -0,0 +1,13 @@
defmodule Pinchflat.Repo.Migrations.BackfillContentUuids do
use Ecto.Migration
def up do
execute("UPDATE sources SET uuid = gen_random_uuid() WHERE uuid IS NULL")
execute("UPDATE media_items SET uuid = gen_random_uuid() WHERE uuid IS NULL")
end
def down do
execute("UPDATE sources SET uuid = NULL")
execute("UPDATE media_items SET uuid = NULL")
end
end