diff --git a/resources/sql/fixtures/01-user.sql b/resources/sql/fixtures/01-user.sql new file mode 100644 index 0000000..91134b9 --- /dev/null +++ b/resources/sql/fixtures/01-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; +$$; + diff --git a/resources/sql/fixtures/02-citizen.sql b/resources/sql/fixtures/02-citizen.sql new file mode 100644 index 0000000..87e85b5 --- /dev/null +++ b/resources/sql/fixtures/02-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; +$$; + diff --git a/resources/sql/fixtures/03-workgroup.sql b/resources/sql/fixtures/03-workgroup.sql new file mode 100644 index 0000000..c0edb00 --- /dev/null +++ b/resources/sql/fixtures/03-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_id) + 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; +$$; + diff --git a/resources/sql/fixtures/04-article.sql b/resources/sql/fixtures/04-article.sql new file mode 100644 index 0000000..1c1024a --- /dev/null +++ b/resources/sql/fixtures/04-article.sql @@ -0,0 +1,37 @@ +do +$$ +declare + _tags text[] = $tags$ + { + "nature", "green", "sky", + "nuclear", "oil", "black", + "love", "human", "scuirel" + } + $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(), + 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; + + 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; +$$; + diff --git a/resources/sql/fixtures/05-constitution.sql b/resources/sql/fixtures/05-constitution.sql new file mode 100644 index 0000000..195fd7b --- /dev/null +++ b/resources/sql/fixtures/05-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; +$$; + diff --git a/resources/sql/fixtures/06-follow.sql b/resources/sql/fixtures/06-follow.sql new file mode 100644 index 0000000..7a0edc2 --- /dev/null +++ b/resources/sql/fixtures/06-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; +$$; + diff --git a/resources/sql/fixtures/07-comment.sql b/resources/sql/fixtures/07-comment.sql new file mode 100644 index 0000000..dbc4e27 --- /dev/null +++ b/resources/sql/fixtures/07-comment.sql @@ -0,0 +1,43 @@ +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 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 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; +$$; + diff --git a/resources/sql/fixtures/08-vote.sql b/resources/sql/fixtures/08-vote.sql new file mode 100644 index 0000000..d2fe62a --- /dev/null +++ b/resources/sql/fixtures/08-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; +$$; + 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 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