From ebc552a431ba42e7642b8cc223ec73a5bb90b462 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Tue, 8 Oct 2019 14:13:31 +0200 Subject: [PATCH] add email to citizen table --- src/main/resources/sql/fixtures/02-citizen.sql | 5 +++-- .../sql/functions/citizen/insert_citizen_with_user.sql | 8 +++++--- .../resources/sql/functions/citizen/upsert_citizen.sql | 8 +++++--- src/main/resources/sql/migrations/0000-init_schema.up.sql | 3 ++- src/test/sql/article.sql | 2 +- src/test/sql/citizen.sql | 4 ++-- src/test/sql/comment.sql | 3 ++- src/test/sql/constitution.sql | 3 ++- src/test/sql/follow.sql | 6 ++++-- src/test/sql/vote.sql | 6 ++++-- 10 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/main/resources/sql/fixtures/02-citizen.sql b/src/main/resources/sql/fixtures/02-citizen.sql index aed2766..fafc9a8 100644 --- a/src/main/resources/sql/fixtures/02-citizen.sql +++ b/src/main/resources/sql/fixtures/02-citizen.sql @@ -2,7 +2,7 @@ do $$ begin delete from citizen; - insert into citizen (id, name, birthday, user_id, vote_anonymous, follow_anonymous) + insert into citizen (id, name, birthday, user_id, vote_anonymous, follow_anonymous, email) select uuid_in(md5('citizen'||row_number() over ()::text)::cstring), jsonb_build_object( @@ -13,7 +13,8 @@ begin now() - interval '25 years', u.id, row_number() over () % 3 = 0, - row_number() over () % 5 = 1 + row_number() over () % 5 = 1, + u.username || '@domain'|| (row_number() over () % 5 = 1) || '.com' from "user" u; raise notice 'citizen fixtures done'; diff --git a/src/main/resources/sql/functions/citizen/insert_citizen_with_user.sql b/src/main/resources/sql/functions/citizen/insert_citizen_with_user.sql index 2fad67c..bdf1bf3 100644 --- a/src/main/resources/sql/functions/citizen/insert_citizen_with_user.sql +++ b/src/main/resources/sql/functions/citizen/insert_citizen_with_user.sql @@ -7,21 +7,23 @@ declare begin select insert_user(resource->'user') into inserted_user; - insert into citizen (id, name, birthday, user_id, vote_anonymous, follow_anonymous) + insert into citizen (id, name, birthday, user_id, vote_anonymous, follow_anonymous, email) select coalesce(id, uuid_generate_v4()), name, birthday, (inserted_user->>'id')::uuid, coalesce(vote_anonymous, true), - coalesce(follow_anonymous, true) + coalesce(follow_anonymous, true), + email from json_populate_record(null::citizen, resource) on conflict (id) do update set name = excluded.name, birthday = excluded.birthday, user_id = excluded.user_id, vote_anonymous = excluded.vote_anonymous, - follow_anonymous = excluded.follow_anonymous + follow_anonymous = excluded.follow_anonymous, + email = excluded.email returning id into new_id; select find_citizen_by_id_with_user(new_id) into resource; diff --git a/src/main/resources/sql/functions/citizen/upsert_citizen.sql b/src/main/resources/sql/functions/citizen/upsert_citizen.sql index bc2a67c..7eec961 100644 --- a/src/main/resources/sql/functions/citizen/upsert_citizen.sql +++ b/src/main/resources/sql/functions/citizen/upsert_citizen.sql @@ -4,21 +4,23 @@ $$ declare new_id uuid; begin - insert into citizen (id, name, birthday, user_id, vote_anonymous, follow_anonymous) + insert into citizen (id, name, birthday, user_id, vote_anonymous, follow_anonymous, email) select coalesce(id, uuid_generate_v4()), name, birthday, (resource#>>'{user, id}')::uuid, coalesce(vote_anonymous, true), - coalesce(follow_anonymous, true) + coalesce(follow_anonymous, true), + email from json_populate_record(null::citizen, resource) on conflict (id) do update set name = excluded.name, birthday = excluded.birthday, user_id = excluded.user_id, vote_anonymous = excluded.vote_anonymous, - follow_anonymous = excluded.follow_anonymous + follow_anonymous = excluded.follow_anonymous, + email = excluded.email returning id into new_id; select find_citizen_by_id(new_id) into resource; diff --git a/src/main/resources/sql/migrations/0000-init_schema.up.sql b/src/main/resources/sql/migrations/0000-init_schema.up.sql index e6363b4..e0a0060 100644 --- a/src/main/resources/sql/migrations/0000-init_schema.up.sql +++ b/src/main/resources/sql/migrations/0000-init_schema.up.sql @@ -18,7 +18,8 @@ create table citizen birthday date not null, user_id uuid not null references "user" (id) unique, vote_anonymous boolean default true not null, - follow_anonymous boolean default true not null + follow_anonymous boolean default true not null, + email text not null check ( email ~* '.+@.+\..+' ) ); create table workgroup diff --git a/src/test/sql/article.sql b/src/test/sql/article.sql index 2f9a59a..75d5411 100644 --- a/src/test/sql/article.sql +++ b/src/test/sql/article.sql @@ -4,7 +4,7 @@ declare created_user json := '{"username": "george", "plain_password": "azerty", "roles": ["ROLE_USER"]}'; _user_id uuid; _citizen_id uuid; - created_citizen json := '{"name": {"first_name":"George", "last_name":"MICHEL"}, "birthday": "2001-01-01"}'; + created_citizen json := '{"name": {"first_name":"George", "last_name":"MICHEL"}, "birthday": "2001-01-01", "email":"george.michel@gmail.com"}'; created_article json := '{"version_id":"933b6a1b-50c9-42b6-989f-c02a57814ef9", "title": "Love the world", "anonymous": false, "content": "bla bal bla", "tags": ["love", "test"], "draft":false}'; created_article_v2 json; first_article_id uuid; diff --git a/src/test/sql/citizen.sql b/src/test/sql/citizen.sql index b1c21cb..0770bcf 100644 --- a/src/test/sql/citizen.sql +++ b/src/test/sql/citizen.sql @@ -4,8 +4,8 @@ declare wrong_citizen json; created_user json := '{"username": "george", "plain_password": "azerty", "roles": ["ROLE_USER"]}'; _user_id uuid; - created_citizen json := '{"name": {"first_name":"George", "last_name":"MICHEL"}, "birthday": "2001-01-01"}'; - created_citizen_with_user json := '{"name": {"first_name":"George", "last_name":"MICHEL"}, "birthday": "2001-01-01", "user":{"username": "george junior", "plain_password": "azerty", "roles": ["ROLE_USER"]}}'; + created_citizen json := '{"name": {"first_name":"George", "last_name":"MICHEL"}, "birthday": "2001-01-01", "email":"george.michel@gmail.com"}'; + created_citizen_with_user json := '{"name": {"first_name":"George", "last_name":"MICHEL"}, "birthday": "2001-01-01", "email":"george.michel@gmail.com", "user":{"username": "george junior", "plain_password": "azerty", "roles": ["ROLE_USER"]}}'; selected_citizen json; begin -- insert user for context diff --git a/src/test/sql/comment.sql b/src/test/sql/comment.sql index 613e919..15cef82 100644 --- a/src/test/sql/comment.sql +++ b/src/test/sql/comment.sql @@ -10,7 +10,8 @@ declare "first_name": "George", "last_name": "MICHEL" }, - "birthday": "2001-01-01" + "birthday": "2001-01-01", + "email":"george.michel@gmail.com" } $json$; created_article json := $json$ diff --git a/src/test/sql/constitution.sql b/src/test/sql/constitution.sql index a2e4944..2dbc453 100644 --- a/src/test/sql/constitution.sql +++ b/src/test/sql/constitution.sql @@ -10,7 +10,8 @@ declare "first_name": "George", "last_name": "MICHEL" }, - "birthday": "2001-01-01" + "birthday": "2001-01-01", + "email":"george.michel@gmail.com" } $json$; created_article json := $json$ diff --git a/src/test/sql/follow.sql b/src/test/sql/follow.sql index 11bc014..ae1062c 100644 --- a/src/test/sql/follow.sql +++ b/src/test/sql/follow.sql @@ -11,7 +11,8 @@ declare "first_name": "George", "last_name": "MICHEL" }, - "birthday": "2001-01-01" + "birthday": "2001-01-01", + "email":"george.michel@gmail.com" } $json$; created_citizen2 json := $json$ @@ -20,7 +21,8 @@ declare "first_name": "John", "last_name": "Doe" }, - "birthday": "2002-01-01" + "birthday": "2002-01-01", + "email":"george.michel@gmail.com" } $json$; begin diff --git a/src/test/sql/vote.sql b/src/test/sql/vote.sql index 498cbb5..82bfcbb 100644 --- a/src/test/sql/vote.sql +++ b/src/test/sql/vote.sql @@ -11,7 +11,8 @@ declare "first_name": "George", "last_name": "MICHEL" }, - "birthday": "2001-01-01" + "birthday": "2001-01-01", + "email":"george.michel@gmail.com" } $json$; created_citizen2 json := $json$ @@ -20,7 +21,8 @@ declare "first_name": "George2", "last_name": "MICHEL2" }, - "birthday": "2001-01-02" + "birthday": "2001-01-02", + "email":"george.michel@gmail.com" } $json$; created_article json := $json$