From 29be2b48afd59f089cdba23d995ec335174cea2a Mon Sep 17 00:00:00 2001 From: Raymond Jacobson Date: Thu, 8 Aug 2024 18:03:57 -0700 Subject: [PATCH 1/2] [QA-1459] Fix 0087_fix_slugs_with_slash.sql migration --- .../migrations/0087_fix_slugs_with_slash.sql | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/packages/discovery-provider/ddl/migrations/0087_fix_slugs_with_slash.sql b/packages/discovery-provider/ddl/migrations/0087_fix_slugs_with_slash.sql index ece82901b7b..408c1973aff 100644 --- a/packages/discovery-provider/ddl/migrations/0087_fix_slugs_with_slash.sql +++ b/packages/discovery-provider/ddl/migrations/0087_fix_slugs_with_slash.sql @@ -1,8 +1,40 @@ begin; -update playlist_routes -set slug = replace(slug, '/', ''), - title_slug = replace(title_slug, '/', '') -where slug like '%/%' or title_slug like '%/%'; +-- Update playlists with slashes in their routes, creating rows without slashes. +-- It may be trickier than it seems at a first glance. + +-- Create a CTE for all of the rows we'd like to update +with updatable_routes as ( + -- We need a unique set of playlists, if we don't distinct here, if two routes exist + -- my/best-track and my//best-track, they would otherwise produce the same update, which + -- would be a constraint violation + select distinct on (replace(slug, '/', ''), replace(title_slug, '/', '')) * + from playlist_routes pr1 + where + (pr1.slug like '%/%' or pr1.title_slug like '%/%') and + -- We also don't want to grab any rows to update if there already is + -- a value in the table without the '/'. That would also be a constraint violation. + not exists ( + select 1 + from playlist_routes pr2 + where + (pr1.slug like '%/%' or pr1.title_slug like '%/%') and + pr2.slug = replace(pr1.slug, '/', '') and + pr2.title_slug = replace(pr2.title_slug, '/', '') and + pr2.playlist_id = pr1.playlist_id and + pr2.owner_id = pr1.owner_id + ) +) +-- Actually do the update here. Replace title slug and slug where matching. +update playlist_routes pr +set + slug = replace(pr.slug, '/', ''), + title_slug = replace(pr.title_slug, '/', '') +from updatable_routes ur +where + ur.playlist_id = pr.playlist_id and + ur.owner_id = pr.owner_id and + ur.slug = pr.slug and + ur.title_slug = pr.title_slug commit; From d0ea706bd5e60b4041e7ce0502c295ad8f4307fa Mon Sep 17 00:00:00 2001 From: Raymond Jacobson Date: Thu, 8 Aug 2024 19:02:23 -0700 Subject: [PATCH 2/2] Forgot the damn semicolon --- .../ddl/migrations/0087_fix_slugs_with_slash.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/discovery-provider/ddl/migrations/0087_fix_slugs_with_slash.sql b/packages/discovery-provider/ddl/migrations/0087_fix_slugs_with_slash.sql index 408c1973aff..58aad9d92a8 100644 --- a/packages/discovery-provider/ddl/migrations/0087_fix_slugs_with_slash.sql +++ b/packages/discovery-provider/ddl/migrations/0087_fix_slugs_with_slash.sql @@ -35,6 +35,6 @@ where ur.playlist_id = pr.playlist_id and ur.owner_id = pr.owner_id and ur.slug = pr.slug and - ur.title_slug = pr.title_slug + ur.title_slug = pr.title_slug; commit;