1
0
Fork 0

Add PostgreSQL function "is_guest_token"

This commit is contained in:
Alex Kotov 2019-07-23 01:48:36 +05:00
parent a6b450366a
commit 3da668394d
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
2 changed files with 24 additions and 2 deletions

View file

@ -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

View file

@ -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)))
);