From 3da668394daa87d87b0b06c67f262854a314a6af Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Tue, 23 Jul 2019 01:48:36 +0500 Subject: [PATCH] Add PostgreSQL function "is_guest_token" --- db/migrate/20181129203927_initial_migration.rb | 11 ++++++++++- db/structure.sql | 15 ++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/db/migrate/20181129203927_initial_migration.rb b/db/migrate/20181129203927_initial_migration.rb index 118b566..a702d8c 100644 --- a/db/migrate/20181129203927_initial_migration.rb +++ b/db/migrate/20181129203927_initial_migration.rb @@ -2,6 +2,15 @@ class InitialMigration < ActiveRecord::Migration[6.0] def change + func :is_guest_token, <<~SQL + (str text) RETURNS boolean IMMUTABLE LANGUAGE plpgsql AS + $$ + BEGIN + RETURN str ~ '^[0-9a-f]{32}$'; + END; + $$; + SQL + func :is_nickname, <<~SQL (str text) RETURNS boolean IMMUTABLE LANGUAGE plpgsql AS $$ @@ -244,7 +253,7 @@ class InitialMigration < ActiveRecord::Migration[6.0] SQL constraint :accounts, :guest_token, <<~SQL - guest_token ~ '^[0-9a-f]{32}$' + is_guest_token(guest_token) SQL constraint :accounts, :nickname, <<~SQL diff --git a/db/structure.sql b/db/structure.sql index 97d7c3c..61ae549 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -95,6 +95,19 @@ END; $_$; +-- +-- Name: is_guest_token(text); Type: FUNCTION; Schema: public; Owner: - +-- + +CREATE FUNCTION public.is_guest_token(str text) RETURNS boolean + LANGUAGE plpgsql IMMUTABLE + AS $_$ +BEGIN + RETURN str ~ '^[0-9a-f]{32}$'; +END; +$_$; + + -- -- Name: is_nickname(text); Type: FUNCTION; Schema: public; Owner: - -- @@ -162,7 +175,7 @@ CREATE TABLE public.accounts ( person_id bigint, contacts_list_id bigint NOT NULL, CONSTRAINT biography CHECK (((biography IS NULL) OR public.is_good_limited_text(biography, 3, 10000))), - CONSTRAINT guest_token CHECK (((guest_token)::text ~ '^[0-9a-f]{32}$'::text)), + CONSTRAINT guest_token CHECK (public.is_guest_token((guest_token)::text)), CONSTRAINT nickname CHECK (public.is_nickname((nickname)::text)), CONSTRAINT public_name CHECK (((public_name IS NULL) OR public.is_good_limited_text((public_name)::text, 3, 255))) );