| | 900 | -- ------------------------------------------------------------- |
| | 901 | -- Notification (~20 M rows) |
| | 902 | -- ------------------------------------------------------------- |
| | 903 | INSERT INTO Notification |
| | 904 | (title, body, created_at, user_id, notification_type_id, |
| | 905 | task_id, offer_id, payment_id) |
| | 906 | SELECT |
| | 907 | CASE (nt_id % 20) + 1 |
| | 908 | WHEN 1 THEN 'New Task Created' |
| | 909 | WHEN 2 THEN 'Task Accepted' |
| | 910 | WHEN 3 THEN 'Task Started' |
| | 911 | WHEN 4 THEN 'Task Completed' |
| | 912 | WHEN 5 THEN 'Task Cancelled' |
| | 913 | WHEN 6 THEN 'Offer Received' |
| | 914 | WHEN 7 THEN 'Offer Accepted' |
| | 915 | WHEN 8 THEN 'Offer Rejected' |
| | 916 | WHEN 9 THEN 'Offer Expired' |
| | 917 | WHEN 10 THEN 'Payment Pending' |
| | 918 | WHEN 11 THEN 'Payment Successful' |
| | 919 | WHEN 12 THEN 'Payment Failed' |
| | 920 | WHEN 13 THEN 'Review Received' |
| | 921 | WHEN 14 THEN 'Complaint Opened' |
| | 922 | WHEN 15 THEN 'Complaint Resolved' |
| | 923 | WHEN 16 THEN 'Complaint Dismissed' |
| | 924 | WHEN 17 THEN 'Message Received' |
| | 925 | WHEN 18 THEN 'Badge Earned' |
| | 926 | WHEN 19 THEN 'Profile Viewed' |
| | 927 | ELSE 'System Announcement' |
| | 928 | END AS title, |
| | 929 | CASE (nt_id % 20) + 1 |
| | 930 | WHEN 1 THEN 'A new task has been created and is awaiting offers.' |
| | 931 | WHEN 2 THEN 'Your task has been accepted by a worker.' |
| | 932 | WHEN 3 THEN 'The worker has started working on your task.' |
| | 933 | WHEN 4 THEN 'Your task has been marked as completed.' |
| | 934 | WHEN 5 THEN 'The task has been cancelled.' |
| | 935 | WHEN 6 THEN 'You have received a new offer on your task.' |
| | 936 | WHEN 7 THEN 'Your offer has been accepted by the client.' |
| | 937 | WHEN 8 THEN 'Your offer has been rejected.' |
| | 938 | WHEN 9 THEN 'Your offer has expired without a response.' |
| | 939 | WHEN 10 THEN 'Your payment is being processed.' |
| | 940 | WHEN 11 THEN 'Your payment was completed successfully.' |
| | 941 | WHEN 12 THEN 'Your payment attempt failed. Please try again.' |
| | 942 | WHEN 13 THEN 'You have received a new review.' |
| | 943 | WHEN 14 THEN 'A complaint has been filed regarding your task.' |
| | 944 | WHEN 15 THEN 'Your complaint has been resolved.' |
| | 945 | WHEN 16 THEN 'Your complaint has been dismissed.' |
| | 946 | WHEN 17 THEN 'You have a new message.' |
| | 947 | WHEN 18 THEN 'Congratulations! You earned a new badge.' |
| | 948 | WHEN 19 THEN 'Someone viewed your profile.' |
| | 949 | ELSE 'There is a new system announcement.' |
| | 950 | END AS body, |
| | 951 | -- generated after user_created_at so the trigger is always satisfied |
| | 952 | user_created_at + (random() * (NOW() - user_created_at)) AS created_at, |
| | 953 | user_id, |
| | 954 | (nt_id % 20) + 1 AS notification_type_id, |
| | 955 | CASE WHEN (nt_id % 20) + 1 IN (1,2,3,4,5,13,14,15,16,17) |
| | 956 | THEN (floor(random() * (SELECT MAX(id) FROM Task)) + 1)::INT |
| | 957 | ELSE NULL |
| | 958 | END AS task_id, |
| | 959 | CASE WHEN (nt_id % 20) + 1 IN (6,7,8,9) |
| | 960 | THEN (floor(random() * (SELECT MAX(id) FROM Offer)) + 1)::INT |
| | 961 | ELSE NULL |
| | 962 | END AS offer_id, |
| | 963 | CASE WHEN (nt_id % 20) + 1 IN (10,11,12) |
| | 964 | THEN (floor(random() * (SELECT MAX(id) FROM Payment)) + 1)::INT |
| | 965 | ELSE NULL |
| | 966 | END AS payment_id |
| | 967 | FROM ( |
| | 968 | SELECT |
| | 969 | nt_id, |
| | 970 | ua.id AS user_id, |
| | 971 | ua.created_at AS user_created_at |
| | 972 | FROM generate_series(0, 9999999) AS nt_id |
| | 973 | CROSS JOIN LATERAL ( |
| | 974 | SELECT id, created_at |
| | 975 | FROM UserAccount |
| | 976 | WHERE id = ( |
| | 977 | floor(random() * ((SELECT MAX(id) FROM UserAccount) - (SELECT MIN(id) FROM UserAccount) + 1)) |
| | 978 | + (SELECT MIN(id) FROM UserAccount) |
| | 979 | )::INT |
| | 980 | ) ua |
| | 981 | ) sub; |
| | 982 | |
| | 983 | |
| | 984 | |
| | 985 | |
| | 986 | select * from Notification; |
| | 987 | |
| | 988 | |
| | 989 | --Notification trigger-- |
| | 990 | CREATE OR REPLACE FUNCTION check_notification_created_at() |
| | 991 | RETURNS TRIGGER AS $$ |
| | 992 | BEGIN |
| | 993 | IF NEW.created_at <= (SELECT created_at FROM UserAccount WHERE id = NEW.user_id) THEN |
| | 994 | RAISE EXCEPTION 'notification created_at must be after user created_at'; |
| | 995 | END IF; |
| | 996 | RETURN NEW; |
| | 997 | END; |
| | 998 | $$ LANGUAGE plpgsql; |
| | 999 | |
| | 1000 | CREATE TRIGGER trg_notification_created_at |
| | 1001 | BEFORE INSERT OR UPDATE ON Notification |
| | 1002 | FOR EACH ROW |
| | 1003 | EXECUTE FUNCTION check_notification_created_at(); |
| | 1004 | |
| | 1005 | |
| | 1006 | |