text: DO $$
  
    DECLARE NEW_USER_USERNAME VARCHAR := 'brutskov';
    DECLARE OLD_USER_USERNAME VARCHAR := 'bagdi';
  
    DECLARE NEW_USER_ID zafira.USERS.id%TYPE;
    DECLARE OLD_USER_ID zafira.USERS.id%TYPE;
  
    DECLARE TEST_CASE_PRIMARY_OWNER_IDS INTEGER[];
    DECLARE TEST_CASE_SECONDARY_OWNER_IDS INTEGER[];
    DECLARE TEST_SUITE_IDS INTEGER[];
    DECLARE TEST_SUITE_NAMES VARCHAR[];
    DECLARE TEST_SUITE_FILE_NAMES VARCHAR[];
  
    DECLARE EXISTING_TEST_SUITE_ID INTEGER;
  
    DECLARE USER_PREFERENCES_IDS INTEGER[];
    DECLARE USER_GROUPS_IDS INTEGER[];
  
  BEGIN
  
    SELECT U.ID INTO NEW_USER_ID FROM zafira.USERS U WHERE U.USERNAME = NEW_USER_USERNAME;
    SELECT U.ID INTO OLD_USER_ID FROM zafira.USERS U WHERE U.USERNAME = OLD_USER_USERNAME;
  
    TEST_CASE_PRIMARY_OWNER_IDS := ARRAY(SELECT TC.ID FROM zafira.TEST_CASES TC WHERE TC.PRIMARY_OWNER_ID = OLD_USER_ID);
    TEST_CASE_SECONDARY_OWNER_IDS := ARRAY(SELECT TC.ID FROM zafira.TEST_CASES TC WHERE TC.SECONDARY_OWNER_ID = OLD_USER_ID);
    TEST_SUITE_IDS := ARRAY(SELECT TS.ID FROM zafira.TEST_SUITES TS WHERE TS.USER_ID = OLD_USER_ID);
    TEST_SUITE_NAMES := ARRAY(SELECT TS.NAME FROM zafira.TEST_SUITES TS WHERE TS.USER_ID = OLD_USER_ID);
    TEST_SUITE_FILE_NAMES := ARRAY(SELECT TS.FILE_NAME FROM zafira.TEST_SUITES TS WHERE TS.USER_ID = OLD_USER_ID);
  
    USER_PREFERENCES_IDS := ARRAY(SELECT UP.ID FROM zafira.USER_PREFERENCES UP WHERE UP.USER_ID = OLD_USER_ID);
    USER_GROUPS_IDS := ARRAY(SELECT UG.ID FROM zafira.USER_GROUPS UG WHERE UG.USER_ID = OLD_USER_ID);
  
    FOR INDEX IN 1 .. COALESCE(ARRAY_UPPER(TEST_CASE_PRIMARY_OWNER_IDS, 1), 1)
      LOOP
      UPDATE zafira.TEST_CASES SET PRIMARY_OWNER_ID = NEW_USER_ID WHERE ID = TEST_CASE_PRIMARY_OWNER_IDS[INDEX];
    END LOOP;
  
    FOR INDEX IN 1 .. COALESCE(ARRAY_UPPER(TEST_CASE_SECONDARY_OWNER_IDS, 1), 1)
    LOOP
      UPDATE zafira.TEST_CASES SET SECONDARY_OWNER_ID = NEW_USER_ID WHERE ID = TEST_CASE_SECONDARY_OWNER_IDS[INDEX];
    END LOOP;
  
    FOR INDEX IN 1 .. COALESCE(ARRAY_UPPER(TEST_SUITE_IDS, 1), 1)
    LOOP
      EXISTING_TEST_SUITE_ID := NULL;
      EXISTING_TEST_SUITE_ID := (SELECT ID FROM zafira.TEST_SUITES WHERE ID != TEST_SUITE_IDS[INDEX] AND NAME = TEST_SUITE_NAMES[INDEX] AND FILE_NAME = TEST_SUITE_FILE_NAMES[INDEX] AND USER_ID = NEW_USER_ID);
      IF EXISTING_TEST_SUITE_ID IS NULL OR EXISTING_TEST_SUITE_ID = 0
        THEN
          UPDATE zafira.TEST_SUITES SET USER_ID = NEW_USER_ID WHERE ID = TEST_SUITE_IDS[INDEX];
        ELSE
          UPDATE zafira.TEST_RUNS SET TEST_SUITE_ID = EXISTING_TEST_SUITE_ID WHERE TEST_SUITE_ID = TEST_SUITE_IDS[INDEX];
          DELETE FROM zafira.TEST_SUITES WHERE ID = TEST_SUITE_IDS[INDEX];
        END IF;
    END LOOP;
  
    FOR INDEX IN 1 .. COALESCE(ARRAY_UPPER(USER_PREFERENCES_IDS, 1), 1)
    LOOP
      DELETE FROM zafira.USER_PREFERENCES WHERE ID = USER_PREFERENCES_IDS[INDEX];
    END LOOP;
  
    FOR INDEX IN 1 .. COALESCE(ARRAY_UPPER(USER_GROUPS_IDS, 1), 1)
    LOOP
      DELETE FROM zafira.USER_GROUPS WHERE ID = USER_GROUPS_IDS[INDEX];
    END LOOP;
  
  END $$;