pref_family_name =
COALESCE(pref_family_name, (SELECT pref_family_name FROM susr)),
pref_suffix =
- COALESCE(pref_suffix, (SELECT pref_suffix FROM susr)),
- name_keywords =
- COALESCE(name_keywords, '') || ' ' ||
- COALESCE((SELECT name_keywords FROM susr), '')
+ COALESCE(pref_suffix, (SELECT pref_suffix FROM susr))
WHERE id = dest_usr;
+ -- Copy and deduplicate name keywords
+ -- String -> array -> rows -> DISTINCT -> array -> string
+ WITH susr AS (SELECT * FROM actor.usr WHERE id = src_usr),
+ dusr AS (SELECT * FROM actor.usr WHERE id = dest_usr)
+ UPDATE actor.usr SET name_keywords = (
+ WITH keywords AS (
+ SELECT DISTINCT UNNEST(
+ REGEXP_SPLIT_TO_ARRAY(
+ COALESCE((SELECT name_keywords FROM susr), '') || ' ' ||
+ COALESCE((SELECT name_keywords FROM dusr), ''), E'\\s+'
+ )
+ ) AS parts
+ ) SELECT ARRAY_TO_STRING(ARRAY_AGG(kw.parts), ' ') FROM keywords kw
+ ) WHERE id = dest_usr;
+
-- Finally, delete the source user
DELETE FROM actor.usr WHERE id = src_usr;
pref_family_name =
COALESCE(pref_family_name, (SELECT pref_family_name FROM susr)),
pref_suffix =
- COALESCE(pref_suffix, (SELECT pref_suffix FROM susr)),
- name_keywords =
- COALESCE(name_keywords, '') || ' ' ||
- COALESCE((SELECT name_keywords FROM susr), '')
+ COALESCE(pref_suffix, (SELECT pref_suffix FROM susr))
WHERE id = dest_usr;
+ -- Copy and deduplicate name keywords
+ -- String -> array -> rows -> DISTINCT -> array -> string
+ WITH susr AS (SELECT * FROM actor.usr WHERE id = src_usr),
+ dusr AS (SELECT * FROM actor.usr WHERE id = dest_usr)
+ UPDATE actor.usr SET name_keywords = (
+ WITH keywords AS (
+ SELECT DISTINCT UNNEST(
+ REGEXP_SPLIT_TO_ARRAY(
+ COALESCE((SELECT name_keywords FROM susr), '') || ' ' ||
+ COALESCE((SELECT name_keywords FROM dusr), ''), E'\\s+'
+ )
+ ) AS parts
+ ) SELECT ARRAY_TO_STRING(ARRAY_AGG(kw.parts), ' ') FROM keywords kw
+ ) WHERE id = dest_usr;
+
-- Finally, delete the source user
DELETE FROM actor.usr WHERE id = src_usr;