mirror of
https://gitlab.com/depesz/explain.depesz.com.git
synced 2024-11-28 08:58:52 +02:00
b381985e43
Functionality is there, I'm just waiting for Metys to help me with styling
35 lines
1.3 KiB
PL/PgSQL
35 lines
1.3 KiB
PL/PgSQL
CREATE TABLE users (
|
|
username TEXT PRIMARY KEY,
|
|
password TEXT,
|
|
registered timestamptz
|
|
);
|
|
|
|
ALTER TABLE plans add column added_by TEXT references users ( username );
|
|
|
|
create unique index concurrently plan_paging on plans (added_by, entered_on, id) where is_deleted = false;
|
|
|
|
DROP FUNCTION register_plan(in_title text, in_plan text, in_is_public boolean, in_is_anonymized boolean);
|
|
CREATE FUNCTION register_plan(in_title text, in_plan text, in_is_public boolean, in_is_anonymized boolean, in_username text) RETURNS register_plan_return
|
|
LANGUAGE plpgsql
|
|
AS $$
|
|
DECLARE
|
|
use_hash_length int4 := 2;
|
|
reply register_plan_return;
|
|
BEGIN
|
|
reply.delete_key := get_random_string( 50 );
|
|
LOOP
|
|
reply.id := get_random_string(use_hash_length);
|
|
BEGIN
|
|
INSERT INTO plans (id, title, plan, is_public, entered_on, is_anonymized, delete_key, added_by) values (reply.id, in_title, in_plan, in_is_public, now(), in_is_anonymized, reply.delete_key, in_username );
|
|
RETURN reply;
|
|
EXCEPTION WHEN unique_violation THEN
|
|
-- do nothing
|
|
END;
|
|
use_hash_length := use_hash_length + 1;
|
|
IF use_hash_length >= 30 THEN
|
|
raise exception 'Random string of length == 30 requested. something''s wrong.';
|
|
END IF;
|
|
END LOOP;
|
|
END;
|
|
$$;
|