From d7e0171b2f8e5bbf9c03fa0e01a25f665ce92ffc Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Thu, 23 Jul 2026 23:55:34 +0200 Subject: [PATCH] Enforcing profile ID in actors_avatars. --- ...3120000_actors_avatars_profile_not_null.js | 24 +++++++++++++++++++ src/actors.js | 11 +++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 migrations/20260723120000_actors_avatars_profile_not_null.js diff --git a/migrations/20260723120000_actors_avatars_profile_not_null.js b/migrations/20260723120000_actors_avatars_profile_not_null.js new file mode 100644 index 00000000..577dd297 --- /dev/null +++ b/migrations/20260723120000_actors_avatars_profile_not_null.js @@ -0,0 +1,24 @@ +exports.up = async function(knex) { + const orphanedAvatars = await knex('actors_avatars') + .whereNull('profile_id'); + + if (orphanedAvatars.length > 0) { + console.log(`Deleting ${orphanedAvatars.length} actors_avatars rows with no profile_id`); + } + + await knex('actors_avatars') + .whereNull('profile_id') + .delete(); + + await knex.raw(` + DROP INDEX IF EXISTS unique_main_avatars; + ALTER TABLE actors_avatars ALTER COLUMN profile_id SET NOT NULL; + `); +}; + +exports.down = async function(knex) { + await knex.raw(` + ALTER TABLE actors_avatars ALTER COLUMN profile_id DROP NOT NULL; + CREATE UNIQUE INDEX unique_main_avatars ON actors_avatars (actor_id) WHERE (profile_id IS NULL); + `); +}; diff --git a/src/actors.js b/src/actors.js index 0cd58f83..88573691 100755 --- a/src/actors.js +++ b/src/actors.js @@ -611,8 +611,15 @@ async function upsertProfiles(profiles) { media_id: profile.avatarMediaId, })); - if (avatars.length > 0) { - await batchInsert('actors_avatars', avatars, { conflict: false }); + const resolvedAvatars = avatars.filter((avatar) => !!avatar.profile_id); + const unresolvedAvatars = avatars.filter((avatar) => !avatar.profile_id); + + unresolvedAvatars.forEach((avatar) => { + logger.warn(`Skipping avatar for actor ${avatar.actor_id} (${avatar.media_id}), no profile could be resolved`); + }); + + if (resolvedAvatars.length > 0) { + await batchInsert('actors_avatars', resolvedAvatars, { conflict: false }); } } }