-- Debut du scenarion pg_bench

-- Ajout de commentaire
SELECT id FROM posts WHERE random() < 0.01 LIMIT 1 \gset post_
SELECT id FROM users WHERE random() < 0.01 LIMIT 1 \gset user_
INSERT INTO comments (postid, text, creationdate, userid) VALUES (:post_id, get_random_text(), now(), :user_id);

-- Ajout de users
INSERT INTO users (creationdate, displayname, age) VALUES (now(), get_full_name(), random_between(12,99));

-- Ajout de vote pour un post
SELECT id FROM posts WHERE random() < 0.01 LIMIT 1 \gset post_
SELECT id FROM users WHERE random() < 0.01 LIMIT 1 \gset user_
INSERT INTO votes (postid, votetypeid, userid, creationdate) VALUES (:post_id, random_between(1,12), :user_id, now());

-- Modification de tags pour un post
SELECT id FROM posts WHERE random() < 0.01 LIMIT 1 \gset post_
SELECT id FROM tags WHERE random() < 0.01 LIMIT 1 \gset tag_
SELECT COUNT(*) AS nb FROM posttags WHERE postid = :post_id AND tagid = :tag_id \gset post_tag_
\if :post_tag_nb= 0
    INSERT INTO posttags (postid, tagid) VALUES (:post_id, :tag_id);
\endif

-- Modification de badge
SELECT id FROM badges WHERE random() < 0.0001 LIMIT 1 \gset badge_
SELECT id FROM users WHERE random() < 0.01 LIMIT 1 \gset user_
UPDATE badges SET userid = :user_id WHERE id = :badge_id;

-- Ajout de post history
SELECT id FROM posts WHERE random() < 0.01 LIMIT 1 \gset post_
SELECT id FROM users WHERE random() < 0.01 LIMIT 1 \gset user_
INSERT INTO posthistory (posthistorytypeid, postid, creationdate, userid, posttext) VALUES (random_between(1,20), :post_id, now(), :user_id, get_random_text());

-- Mise à jour de users
SELECT id FROM users WHERE random() < 0.01 LIMIT 1 \gset user_
UPDATE users SET lAStaccessdate = now() WHERE id = :user_id;

-- Modification de comment
SELECT id FROM users WHERE random() < 0.01 LIMIT 1 \gset user_
UPDATE comments SET text = get_random_text() WHERE id = :user_id;

-- Modification de post
SELECT id FROM posts WHERE random() < 0.01 LIMIT 1 \gset post_
UPDATE posts SET closeddate = now() WHERE id = :post_id;

-- Ajout de post
SELECT id FROM users WHERE random() < 0.01 LIMIT 1 \gset user_
INSERT INTO posts (posttypeid, creationdate, body, owneruserid, jsonfield) VALUES (random_between(1,8), now(), get_random_text(), :user_id, '
{
    "_id": "61851f134baf25710bdb42d4",
    "index": 0,
    "guid": "9ddc5962-7d9f-4a3b-9fbb-d1604ad9ce7b",
    "isActive": false,
    "balance": "$1,335.80",
    "picture": "http://placehold.it/32x32",
    "age": 33,
    "eyeColor": "green",
    "name": "Renee Mccray",
    "gender": "female",
    "company": "CUJO",
    "email": "reneemccray@cujo.com",
    "phone": "+1 (883) 502-2078",
    "address": "892 Ferris Street, Grazierville, Kentucky, 9437",
    "about": "Nostrud sint adipisicing ullamco sunt eu velit et mollit pariatur labore anim eiusmod qui. Adipisicing id id sint sint commodo do deserunt commodo cupidatat veniam consectetur officia. Irure qui ullamco ex eiusmod labore magna sunt irure. In incididunt et eu aute aliquip Lorem.\r\n",
    "registered": "2017-08-16T03:42:20 -02:00",
    "latitude": 30.850001,
    "longitude": 169.726364,
    "tags": [
      "aliqua",
      "veniam",
      "eiusmod",
      "et",
      "ea",
      "sunt",
      "mollit"
    ],
    "friends": [
      {
        "id": 0,
        "name": "Landry Bell"
      },
      {
        "id": 1,
        "name": "Lena Bonner"
      },
      {
        "id": 2,
        "name": "Taylor Acosta"
      }
    ],
    "greeting": "Hello, Renee Mccray! You have 9 unread messages.",
    "favoriteFruit": "strawberry"
}
');