Skip to content

Instantly share code, notes, and snippets.

@cadecairos
Last active October 16, 2015 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cadecairos/a8c44ef236969d51a09c to your computer and use it in GitHub Desktop.
Save cadecairos/a8c44ef236969d51a09c to your computer and use it in GitHub Desktop.
Generate uuid v4 reset codes using a plpgsql function and a trigger
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE OR REPLACE FUNCTION generate_reset_code()
RETURNS TRIGGER AS $$
BEGIN
NEW.code = gen_random_uuid();
RETURN NEW;
END;
$$ language 'plpgsql';
CREATE TRIGGER trigger_generate_reset_code BEFORE CREATE ON reset_codes
FOR EACH ROW EXECUTE PROCEDURE generate_reset_code();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment