shx/packages/api/hasura/migrations/default/1683385777576_run_sql_migra.../up.sql

17 lines
410 B
PL/PgSQL

create function unique_random(len int, _table text, _col text) returns text as $$
declare
result text;
numrows int;
begin
result = random_string(len);
loop
execute format('select 1 from %I where %I = %L', _table, _col, result);
get diagnostics numrows = row_count;
if numrows = 0 then
return result;
end if;
result = random_string(len);
end loop;
end;
$$ language plpgsql;