From 05e7fa3d2ee819cb0bde3c912c0182ff238c6adc Mon Sep 17 00:00:00 2001 From: Kieran Eglin Date: Mon, 25 Mar 2024 15:25:44 -0700 Subject: [PATCH] Added UUID backfill to a migration --- lib/pinchflat/boot/pre_job_startup_tasks.ex | 15 --------------- ...240325222242_add_uuid_to_source_and_media.exs} | 0 .../20240325222243_backfill_content_uuids.exs | 13 +++++++++++++ 3 files changed, 13 insertions(+), 15 deletions(-) rename priv/repo/migrations/{20240323165649_add_uuid_to_source_and_media.exs => 20240325222242_add_uuid_to_source_and_media.exs} (100%) create mode 100644 priv/repo/migrations/20240325222243_backfill_content_uuids.exs diff --git a/lib/pinchflat/boot/pre_job_startup_tasks.ex b/lib/pinchflat/boot/pre_job_startup_tasks.ex index e1bb4c0..83573c5 100644 --- a/lib/pinchflat/boot/pre_job_startup_tasks.ex +++ b/lib/pinchflat/boot/pre_job_startup_tasks.ex @@ -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 diff --git a/priv/repo/migrations/20240323165649_add_uuid_to_source_and_media.exs b/priv/repo/migrations/20240325222242_add_uuid_to_source_and_media.exs similarity index 100% rename from priv/repo/migrations/20240323165649_add_uuid_to_source_and_media.exs rename to priv/repo/migrations/20240325222242_add_uuid_to_source_and_media.exs diff --git a/priv/repo/migrations/20240325222243_backfill_content_uuids.exs b/priv/repo/migrations/20240325222243_backfill_content_uuids.exs new file mode 100644 index 0000000..dc20e51 --- /dev/null +++ b/priv/repo/migrations/20240325222243_backfill_content_uuids.exs @@ -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