From 401dbbea6fdf0e2fb44582e4c4a03a276c70c38e Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Mon, 29 Jul 2019 23:53:06 +0200 Subject: [PATCH 01/12] feature #19: add user fixtures --- resources/sql/fixtures/user.sql | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 resources/sql/fixtures/user.sql diff --git a/resources/sql/fixtures/user.sql b/resources/sql/fixtures/user.sql new file mode 100644 index 0000000..91134b9 --- /dev/null +++ b/resources/sql/fixtures/user.sql @@ -0,0 +1,17 @@ +do +$$ +declare + _password text := crypt('azerty', gen_salt('bf', 8)); +begin + delete from "user"; + insert into "user" (username, password, blocked_at) + select + 'username' || s, + _password, + case when s % 10 = 0 then now() else null end + from generate_series(1, 1000) s; + + raise notice 'user fixtures done'; +end; +$$; + From 055aa30120c506782c5c846cba0db55a65e6502a Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Mon, 29 Jul 2019 23:53:15 +0200 Subject: [PATCH 02/12] feature #19: add citizen fixtures --- resources/sql/fixtures/citizen.sql | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 resources/sql/fixtures/citizen.sql diff --git a/resources/sql/fixtures/citizen.sql b/resources/sql/fixtures/citizen.sql new file mode 100644 index 0000000..87e85b5 --- /dev/null +++ b/resources/sql/fixtures/citizen.sql @@ -0,0 +1,21 @@ +do +$$ +begin + delete from citizen; + insert into citizen (name, birthday, user_id, vote_annonymous, follow_annonymous) + select + jsonb_build_object( + 'first_name', 'first name' || row_number() over (), + 'last_name', 'LAST NAME' || row_number() over (), + 'civility', 'm' + ), + now() - interval '25 years', + u.id, + row_number() over () % 3 = 0, + row_number() over () % 5 = 1 + from "user" u; + + raise notice 'citizen fixtures done'; +end; +$$; + From fbf2bc5c668ed3d691f279d32e8753f45b7d5ba8 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Mon, 29 Jul 2019 23:53:24 +0200 Subject: [PATCH 03/12] feature #19: add article fixtures --- resources/sql/fixtures/article.sql | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 resources/sql/fixtures/article.sql diff --git a/resources/sql/fixtures/article.sql b/resources/sql/fixtures/article.sql new file mode 100644 index 0000000..ed58816 --- /dev/null +++ b/resources/sql/fixtures/article.sql @@ -0,0 +1,27 @@ +do +$$ +declare + _tags text[] = $tags$ + { + "nature", "green", "sky", + "nuclear", "oil", "black", + "love", "human", "scuirel" + } + $tags$; +begin + delete from article; + insert into article (version_id, created_by_id, title, annonymous, content, description, tags) + select + uuid_generate_v4(), + z.id, + 'title' || row_number() over (), + row_number() over () % 3 = 0, + 'content' || row_number() over (), + 'description' || row_number() over (), + _tags[(row_number() over () % 5):(row_number() over () % 9)] + from citizen z; + + raise notice 'article fixtures done'; +end; +$$; + From 107aeca017a516c253e5d86b897afad31cccdde5 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Tue, 30 Jul 2019 00:20:56 +0200 Subject: [PATCH 04/12] feature #19: add constitution fixtures --- resources/sql/fixtures/constitution.sql | 38 +++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 resources/sql/fixtures/constitution.sql diff --git a/resources/sql/fixtures/constitution.sql b/resources/sql/fixtures/constitution.sql new file mode 100644 index 0000000..195fd7b --- /dev/null +++ b/resources/sql/fixtures/constitution.sql @@ -0,0 +1,38 @@ +do +$$ +begin + delete from article_in_title; + delete from title; + delete from constitution; + + insert into constitution (version_id, created_by_id, title, annonymous) + select + uuid_generate_v4(), + z.id, + 'title' || row_number() over (), + row_number() over () % 3 = 0 + from citizen z; + + insert into title (created_by_id, name, rank, constitution_id) + select + c.created_by_id, + 'name' || row_number() over (), + row_number() over (), + c.id + from constitution c, + lateral generate_series(1, 5) g; + + insert into article_in_title (created_by_id, rank, title_id, article_id, constitution_id) + select + ti.created_by_id, + row_number() over (), + ti.id, + a.id, + ti.constitution_id + from (select *, (row_number() over () % 1005) rn from title, lateral generate_series(1, 3) g) ti + join (select *, row_number() over () rn from article) a using (rn); + + raise notice 'constitution fixtures done'; +end; +$$; + From c5aabf22b20313093a8f4ebf93c5edb02f8b1882 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Tue, 30 Jul 2019 00:29:00 +0200 Subject: [PATCH 05/12] feature #19: add follow fixtures --- resources/sql/fixtures/follow.sql | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 resources/sql/fixtures/follow.sql diff --git a/resources/sql/fixtures/follow.sql b/resources/sql/fixtures/follow.sql new file mode 100644 index 0000000..7a0edc2 --- /dev/null +++ b/resources/sql/fixtures/follow.sql @@ -0,0 +1,30 @@ +do +$$ +begin + delete from follow; + + insert into follow_article (citizen_id, target_id) + select + z.id, + a.id + from (select *, row_number() over () % 995 rn from citizen, lateral generate_series(1, 5)) z + join (select *, row_number() over () rn from article) a using (rn); + + insert into follow_constitution (citizen_id, target_id) + select + z.id, + a.id + from (select *, row_number() over () % 995 rn from citizen, lateral generate_series(1, 5)) z + join (select *, row_number() over () rn from constitution) a using (rn); + + insert into follow_citizen (citizen_id, target_id) + select + z.id, + a.id + from (select *, row_number() over () % 995 rn from citizen, lateral generate_series(1, 5)) z + join (select *, row_number() over () rn from citizen) a using (rn); + + raise notice 'follow fixtures done'; +end; +$$; + From 3aa775242044cea268269dcd8c947516dba881ef Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Tue, 30 Jul 2019 00:33:39 +0200 Subject: [PATCH 06/12] feature #19: add comment fixtures --- resources/sql/fixtures/comment.sql | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 resources/sql/fixtures/comment.sql diff --git a/resources/sql/fixtures/comment.sql b/resources/sql/fixtures/comment.sql new file mode 100644 index 0000000..2262155 --- /dev/null +++ b/resources/sql/fixtures/comment.sql @@ -0,0 +1,25 @@ +do +$$ +begin + delete from comment; + + insert into comment_on_article (citizen_id, target_id, content) + select + z.id, + a.id, + 'content' || row_number() over () * g + from (select *, row_number() over () % 995 rn, g from citizen, lateral generate_series(1, 5) g) z + join (select *, row_number() over () rn from article) a using (rn); + + insert into comment_on_constitution (citizen_id, target_id, content) + select + z.id, + a.id, + 'content' || row_number() over () * g + from (select *, row_number() over () % 995 rn, g from citizen, lateral generate_series(1, 5) g) z + join (select *, row_number() over () rn from constitution) a using (rn); + + raise notice 'comment fixtures done'; +end; +$$; + From 7edd698e1f5c331829b33f320b429ab6c4045d79 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Tue, 30 Jul 2019 09:25:27 +0200 Subject: [PATCH 07/12] feature #19: add vote fixtures --- resources/sql/fixtures/vote.sql | 48 +++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 resources/sql/fixtures/vote.sql diff --git a/resources/sql/fixtures/vote.sql b/resources/sql/fixtures/vote.sql new file mode 100644 index 0000000..d2fe62a --- /dev/null +++ b/resources/sql/fixtures/vote.sql @@ -0,0 +1,48 @@ +do +$$ +begin + delete from vote_for_article; + delete from vote_for_constitution; + delete from vote_for_comment_on_article; + delete from vote_for_comment_on_constitution; + + insert into vote_for_article (citizen_id, target_id, note, anonymous) + select + z.id, + a.id, + (row_number() over () % 3) -1, + (row_number() over () % 3 = 1) + from (select *, row_number() over () % 995 rn, g from citizen, lateral generate_series(1, 10) g) z + join (select *, row_number() over () rn from article) a using (rn); + + insert into vote_for_constitution (citizen_id, target_id, note, anonymous) + select + z.id, + a.id, + (row_number() over () % 3) -1, + (row_number() over () % 3 = 1) + from (select *, row_number() over () % 995 rn, g from citizen, lateral generate_series(1, 5) g) z + join (select *, row_number() over () rn from constitution) a using (rn); + + insert into vote_for_comment_on_article (citizen_id, target_id, note, anonymous) + select + z.id, + a.id, + (row_number() over () % 3) -1, + (row_number() over () % 3 = 1) + from (select *, row_number() over () % 995 rn, g from citizen, lateral generate_series(1, 3) g) z + join (select *, row_number() over () rn from comment_on_article) a using (rn); + + insert into vote_for_comment_on_constitution (citizen_id, target_id, note, anonymous) + select + z.id, + a.id, + (row_number() over () % 3) -1, + (row_number() over () % 3 = 1) + from (select *, row_number() over () % 995 rn, g from citizen, lateral generate_series(1, 2) g) z + join (select *, row_number() over () rn from comment_on_constitution) a using (rn); + + raise notice 'vote fixtures done'; +end; +$$; + From 90e6137bc4315bd1ac8665166801bf7690ab0d9f Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Tue, 30 Jul 2019 09:34:42 +0200 Subject: [PATCH 08/12] feature #19: improve comment fixtures and add sub comments --- resources/sql/fixtures/comment.sql | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/resources/sql/fixtures/comment.sql b/resources/sql/fixtures/comment.sql index 2262155..dbc4e27 100644 --- a/resources/sql/fixtures/comment.sql +++ b/resources/sql/fixtures/comment.sql @@ -7,16 +7,34 @@ begin select z.id, a.id, - 'content' || row_number() over () * g - from (select *, row_number() over () % 995 rn, g from citizen, lateral generate_series(1, 5) g) z + 'content' || (row_number() over () * g) + from (select *, row_number() over () % 995 rn from citizen, lateral generate_series(1, 5) g) z join (select *, row_number() over () rn from article) a using (rn); + insert into comment_on_article (citizen_id, target_id, content, parent_id) + select + z.id, + a.target_id, + 'content' || row_number() over () * g, + a.id + from (select *, row_number() over () % 995 rn from citizen, lateral generate_series(1, 5) g) z + join (select *, row_number() over () rn from comment_on_article) a using (rn); + + insert into comment_on_article (citizen_id, target_id, content, parent_id) + select + z.id, + a.target_id, + 'content' || row_number() over () * g, + a.id + from (select *, row_number() over () % 995 rn from citizen, lateral generate_series(1, 5) g) z + join (select *, row_number() over () rn from comment_on_article where parent_id is not null) a using (rn); + insert into comment_on_constitution (citizen_id, target_id, content) select z.id, a.id, 'content' || row_number() over () * g - from (select *, row_number() over () % 995 rn, g from citizen, lateral generate_series(1, 5) g) z + from (select *, row_number() over () % 995 rn from citizen, lateral generate_series(1, 5) g) z join (select *, row_number() over () rn from constitution) a using (rn); raise notice 'comment fixtures done'; From dba76602fba2407dcc26529d4495fe5d9b725d05 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Tue, 30 Jul 2019 09:40:20 +0200 Subject: [PATCH 09/12] feature #19: improve article fixtures and add relations --- resources/sql/fixtures/article.sql | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/resources/sql/fixtures/article.sql b/resources/sql/fixtures/article.sql index ed58816..1c1024a 100644 --- a/resources/sql/fixtures/article.sql +++ b/resources/sql/fixtures/article.sql @@ -9,7 +9,9 @@ declare } $tags$; begin + delete from article_relations; delete from article; + insert into article (version_id, created_by_id, title, annonymous, content, description, tags) select uuid_generate_v4(), @@ -21,6 +23,14 @@ begin _tags[(row_number() over () % 5):(row_number() over () % 9)] from citizen z; + insert into article_relations (source_id, target_id, created_by_id) + select + src.id, + dest.id, + src.created_by_id + from (select *, row_number() over () rn from article, lateral generate_series(1, 5) g) src + join (select *, row_number() over () +5 rn from article) dest using (rn); + raise notice 'article fixtures done'; end; $$; From a897333533221a9240e5630b968dd9ade5572f5d Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Tue, 30 Jul 2019 09:48:11 +0200 Subject: [PATCH 10/12] feature #19: add workgroup fixtures --- resources/sql/fixtures/workgroup.sql | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 resources/sql/fixtures/workgroup.sql diff --git a/resources/sql/fixtures/workgroup.sql b/resources/sql/fixtures/workgroup.sql new file mode 100644 index 0000000..f1eeb3c --- /dev/null +++ b/resources/sql/fixtures/workgroup.sql @@ -0,0 +1,26 @@ +do +$$ +begin + delete from citizen_in_workgroup; + delete from workgroup; + + insert into workgroup (created_by_id, name, description, annonymous, owner_id) + select + z.id, + 'name' || rn, + 'description' || rn, + rn % 3 = 1, + z.id + from (select *, row_number() over () rn from citizen) z; + + insert into citizen_in_workgroup (citizen_id, workgroup) + select + z.id, + w.id + from (select *, row_number() over ()+5 % 1000 rn from citizen) z + join (select *, row_number() over () rn from workgroup) w using (rn); + + raise notice 'workgroup fixtures done'; +end; +$$; + From 6eb3991a52962b86a154cdfc425a7aad4a678d93 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Tue, 30 Jul 2019 09:50:29 +0200 Subject: [PATCH 11/12] refactoring #19: rename fixture files --- resources/sql/fixtures/{user.sql => 01-user.sql} | 0 resources/sql/fixtures/{citizen.sql => 02-citizen.sql} | 0 resources/sql/fixtures/{workgroup.sql => 03-workgroup.sql} | 0 resources/sql/fixtures/{article.sql => 04-article.sql} | 0 .../sql/fixtures/{constitution.sql => 05-constitution.sql} | 0 resources/sql/fixtures/{follow.sql => 06-follow.sql} | 0 resources/sql/fixtures/{comment.sql => 07-comment.sql} | 0 resources/sql/fixtures/{vote.sql => 08-vote.sql} | 0 resources/sql/functions/helpers/random_between.sql | 7 +++++++ 9 files changed, 7 insertions(+) rename resources/sql/fixtures/{user.sql => 01-user.sql} (100%) rename resources/sql/fixtures/{citizen.sql => 02-citizen.sql} (100%) rename resources/sql/fixtures/{workgroup.sql => 03-workgroup.sql} (100%) rename resources/sql/fixtures/{article.sql => 04-article.sql} (100%) rename resources/sql/fixtures/{constitution.sql => 05-constitution.sql} (100%) rename resources/sql/fixtures/{follow.sql => 06-follow.sql} (100%) rename resources/sql/fixtures/{comment.sql => 07-comment.sql} (100%) rename resources/sql/fixtures/{vote.sql => 08-vote.sql} (100%) create mode 100644 resources/sql/functions/helpers/random_between.sql diff --git a/resources/sql/fixtures/user.sql b/resources/sql/fixtures/01-user.sql similarity index 100% rename from resources/sql/fixtures/user.sql rename to resources/sql/fixtures/01-user.sql diff --git a/resources/sql/fixtures/citizen.sql b/resources/sql/fixtures/02-citizen.sql similarity index 100% rename from resources/sql/fixtures/citizen.sql rename to resources/sql/fixtures/02-citizen.sql diff --git a/resources/sql/fixtures/workgroup.sql b/resources/sql/fixtures/03-workgroup.sql similarity index 100% rename from resources/sql/fixtures/workgroup.sql rename to resources/sql/fixtures/03-workgroup.sql diff --git a/resources/sql/fixtures/article.sql b/resources/sql/fixtures/04-article.sql similarity index 100% rename from resources/sql/fixtures/article.sql rename to resources/sql/fixtures/04-article.sql diff --git a/resources/sql/fixtures/constitution.sql b/resources/sql/fixtures/05-constitution.sql similarity index 100% rename from resources/sql/fixtures/constitution.sql rename to resources/sql/fixtures/05-constitution.sql diff --git a/resources/sql/fixtures/follow.sql b/resources/sql/fixtures/06-follow.sql similarity index 100% rename from resources/sql/fixtures/follow.sql rename to resources/sql/fixtures/06-follow.sql diff --git a/resources/sql/fixtures/comment.sql b/resources/sql/fixtures/07-comment.sql similarity index 100% rename from resources/sql/fixtures/comment.sql rename to resources/sql/fixtures/07-comment.sql diff --git a/resources/sql/fixtures/vote.sql b/resources/sql/fixtures/08-vote.sql similarity index 100% rename from resources/sql/fixtures/vote.sql rename to resources/sql/fixtures/08-vote.sql diff --git a/resources/sql/functions/helpers/random_between.sql b/resources/sql/functions/helpers/random_between.sql new file mode 100644 index 0000000..76e81dd --- /dev/null +++ b/resources/sql/functions/helpers/random_between.sql @@ -0,0 +1,7 @@ +CREATE OR REPLACE FUNCTION random_between(low INT ,high INT) + RETURNS INT AS +$$ +BEGIN + RETURN floor(random()* (high-low + 1) + low); +END; +$$ language 'plpgsql' STRICT; \ No newline at end of file From 6385e010a2b054a765abee2c8a7368ed55728008 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Tue, 30 Jul 2019 09:53:33 +0200 Subject: [PATCH 12/12] refactoring #19: rename column workgroup to workgroup_id --- resources/sql/fixtures/03-workgroup.sql | 2 +- resources/sql/migrations/0000-init_schema.up.sql | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/resources/sql/fixtures/03-workgroup.sql b/resources/sql/fixtures/03-workgroup.sql index f1eeb3c..c0edb00 100644 --- a/resources/sql/fixtures/03-workgroup.sql +++ b/resources/sql/fixtures/03-workgroup.sql @@ -13,7 +13,7 @@ begin z.id from (select *, row_number() over () rn from citizen) z; - insert into citizen_in_workgroup (citizen_id, workgroup) + insert into citizen_in_workgroup (citizen_id, workgroup_id) select z.id, w.id diff --git a/resources/sql/migrations/0000-init_schema.up.sql b/resources/sql/migrations/0000-init_schema.up.sql index 0642b6f..ac4cde6 100644 --- a/resources/sql/migrations/0000-init_schema.up.sql +++ b/resources/sql/migrations/0000-init_schema.up.sql @@ -37,10 +37,10 @@ create table workgroup create table citizen_in_workgroup ( - citizen_id uuid not null references citizen (id), - workgroup uuid not null references workgroup (id), - created_at timestamptz default now() not null, - primary key (citizen_id, workgroup) + citizen_id uuid not null references citizen (id), + workgroup_id uuid not null references workgroup (id), + created_at timestamptz default now() not null, + primary key (citizen_id, workgroup_id) ); create table moderator