Advertisement
Guest User

Untitled

a guest
Jun 7th, 2015
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.31 KB | None | 0 0
  1. DROP VIEW user_accessible_emails;
  2. DROP TABLE email_participants;
  3. DROP TABLE emails;
  4. DROP TABLE email_addresses;
  5. DROP TABLE contacts;
  6. DROP TABLE users;
  7. DROP TABLE accounts;
  8.  
  9. CREATE TABLE accounts
  10. (
  11. id integer NOT NULL,
  12. name text NOT NULL,
  13. CONSTRAINT accounts_pkey PRIMARY KEY (id)
  14. );
  15.  
  16. INSERT INTO Accounts (id, name)
  17. VALUES (1, 'DUMMY ACCOUNT');
  18.  
  19. CREATE TABLE users
  20. (
  21. id integer NOT NULL,
  22. first_name text,
  23. last_name text,
  24. account_id integer NOT NULL,
  25. CONSTRAINT users_pkey PRIMARY KEY (id),
  26. CONSTRAINT users_account_id_fkey FOREIGN KEY (account_id)
  27. REFERENCES accounts (id) MATCH SIMPLE
  28. ON UPDATE NO ACTION ON DELETE CASCADE
  29. );
  30.  
  31. INSERT INTO users (id, first_name, last_name, account_id)
  32. values (1, 'Paul', 'Cowan', 1);
  33.  
  34. INSERT INTO users (id, first_name, last_name, account_id)
  35. values (2, 'Other', 'User', 1);
  36.  
  37. -- Table: contacts
  38.  
  39. -- DROP TABLE contacts;
  40.  
  41. CREATE TABLE contacts
  42. (
  43. id integer NOT NULL,
  44. name text,
  45. assigned_to_id integer,
  46. ignored boolean DEFAULT false,
  47. account_id integer NOT NULL,
  48. CONSTRAINT contacts_pkey PRIMARY KEY (id),
  49. CONSTRAINT contacts_account_id_fkey FOREIGN KEY (account_id)
  50. REFERENCES accounts (id) MATCH SIMPLE
  51. ON UPDATE NO ACTION ON DELETE CASCADE,
  52. CONSTRAINT contacts_assigned_to_id_fkey FOREIGN KEY (assigned_to_id)
  53. REFERENCES users (id) MATCH SIMPLE
  54. ON UPDATE NO ACTION ON DELETE SET NULL
  55. );
  56.  
  57. CREATE INDEX contacts_account_id_index
  58. ON contacts
  59. USING btree
  60. (account_id);
  61.  
  62. CREATE INDEX contacts_assigned_to_id_index
  63. ON contacts
  64. USING btree
  65. (assigned_to_id);
  66.  
  67. INSERT INTO Contacts (id, name, assigned_to_id, account_id)
  68. VALUES (1, 'Bob Champion', 1, 1);
  69.  
  70. INSERT INTO Contacts (id, name, assigned_to_id, account_id)
  71. VALUES (2, 'Michael Collins', 1, 1);
  72.  
  73. INSERT INTO Contacts (id, name, assigned_to_id, account_id)
  74. VALUES (3, 'Susan Cutter', 1, 1);
  75.  
  76. INSERT INTO Contacts (id, name, assigned_to_id, account_id)
  77. VALUES (4, 'Patrick Bateman', 1, 1);
  78.  
  79. INSERT INTO Contacts (id, name, assigned_to_id, account_id)
  80. VALUES (5, 'Colin Pascoe', 1, 1);
  81.  
  82. CREATE TABLE email_addresses
  83. (
  84. id integer NOT NULL,
  85. name text,
  86. address text NOT NULL,
  87. "primary" boolean,
  88. contact_id integer,
  89. CONSTRAINT email_addresses_pkey PRIMARY KEY (id),
  90. CONSTRAINT email_addresses_contact_id_fkey FOREIGN KEY (contact_id)
  91. REFERENCES contacts (id) MATCH SIMPLE
  92. ON UPDATE NO ACTION ON DELETE CASCADE
  93. );
  94.  
  95. CREATE INDEX email_addresses_contact_id_index
  96. ON email_addresses
  97. USING btree
  98. (contact_id);
  99.  
  100. INSERT INTO email_addresses(id, name, address, "primary", contact_id)
  101. VALUES(1, 'work', 'bob.champion@gmail.com', true, 1);
  102.  
  103. INSERT INTO email_addresses(id, name, address, "primary", contact_id)
  104. VALUES(2, 'work', 'michael.collins@gmail.com', true, 1);
  105.  
  106. INSERT INTO email_addresses(id, name, address, "primary", contact_id)
  107. VALUES(3, 'work', 'susan.cutter@gmail.com', true, 1);
  108.  
  109. INSERT INTO email_addresses(id, name, address, "primary", contact_id)
  110. VALUES(4, 'work', 'patrick.bateman@gmail.com', true, 1);
  111.  
  112. INSERT INTO email_addresses(id, name, address, "primary", contact_id)
  113. VALUES(5, 'work', 'colin.pascoe@gmail.com', true, 1);
  114.  
  115. CREATE TABLE emails
  116. (
  117. id integer NOT NULL,
  118. subject text,
  119. html text,
  120. folder text,
  121. personal boolean DEFAULT false,
  122. sent_at timestamp without time zone,
  123. account_id integer NOT NULL,
  124. sender_user_id integer,
  125. sender_contact_id integer,
  126. CONSTRAINT emails_pkey PRIMARY KEY (id),
  127. CONSTRAINT emails_account_id_fkey FOREIGN KEY (account_id)
  128. REFERENCES accounts (id) MATCH SIMPLE
  129. ON UPDATE NO ACTION ON DELETE CASCADE,
  130. CONSTRAINT emails_sender_contact_id_fkey FOREIGN KEY (sender_contact_id)
  131. REFERENCES contacts (id) MATCH SIMPLE
  132. ON UPDATE NO ACTION ON DELETE CASCADE,
  133. CONSTRAINT emails_sender_user_id_fkey FOREIGN KEY (sender_user_id)
  134. REFERENCES users (id) MATCH SIMPLE
  135. ON UPDATE NO ACTION ON DELETE CASCADE
  136. );
  137.  
  138. CREATE INDEX emails_account_id_index
  139. ON emails
  140. USING btree
  141. (account_id);
  142.  
  143. CREATE INDEX emails_sender_contact_id_index
  144. ON emails
  145. USING btree
  146. (sender_contact_id);
  147.  
  148. CREATE INDEX emails_sender_user_id_index
  149. ON emails
  150. USING btree
  151. (sender_user_id);
  152.  
  153. INSERT INTO emails (id, subject, html, folder, sent_at, account_id, sender_user_id, sender_contact_id)
  154. VALUES (1, 'bob champion', '<b>Html</b>', 'INBOX', now(), 1, null, 1);
  155.  
  156. INSERT INTO emails (id, subject, html, folder, sent_at, account_id, sender_user_id, sender_contact_id)
  157. VALUES (2, 'michael collins', '<b>Html</b>', 'INBOX', now(), 1, null, 2);
  158.  
  159. INSERT INTO emails (id, subject, html, folder, sent_at, account_id, sender_user_id, sender_contact_id)
  160. VALUES (3, 'susan cutter', '<b>Html</b>', 'INBOX', now(), 1, null, 3);
  161.  
  162. INSERT INTO emails (id, subject, html, folder, sent_at, account_id, sender_user_id, sender_contact_id)
  163. VALUES (4, 'patrick bateman', '<b>Html</b>', 'INBOX', now(), 1, null, 4);
  164.  
  165. INSERT INTO emails (id, subject, html, folder, sent_at, account_id, sender_user_id, sender_contact_id)
  166. VALUES (5, 'colin pascoe', '<b>Html</b>', 'INBOX', now(), 1, null, 5);
  167.  
  168. INSERT INTO emails (id, subject, html, folder, sent_at, account_id, sender_user_id, sender_contact_id)
  169. VALUES (6, 'other user', '<b>Html</b>', 'INBOX', now(), 1, 2, null);
  170.  
  171. CREATE TABLE email_participants
  172. (
  173. id integer NOT NULL,
  174. kind text NOT NULL,
  175. email_id integer NOT NULL,
  176. user_id integer,
  177. contact_id integer,
  178. CONSTRAINT email_participants_pkey PRIMARY KEY (id),
  179. CONSTRAINT email_participants_contact_id_fkey FOREIGN KEY (contact_id)
  180. REFERENCES contacts (id) MATCH SIMPLE
  181. ON UPDATE NO ACTION ON DELETE CASCADE,
  182. CONSTRAINT email_participants_email_id_fkey FOREIGN KEY (email_id)
  183. REFERENCES emails (id) MATCH SIMPLE
  184. ON UPDATE NO ACTION ON DELETE CASCADE,
  185. CONSTRAINT email_participants_user_id_fkey FOREIGN KEY (user_id)
  186. REFERENCES users (id) MATCH SIMPLE
  187. ON UPDATE NO ACTION ON DELETE CASCADE
  188. );
  189.  
  190. INSERT INTO email_participants(id, kind, email_id, user_id, contact_id)
  191. VALUES(1, 'to', 1, 1, null);
  192. INSERT INTO email_participants(id, kind, email_id, user_id, contact_id)
  193. VALUES(2, 'sent', 1, null, 1);
  194.  
  195. INSERT INTO email_participants(id, kind, email_id, user_id, contact_id)
  196. VALUES(3, 'to', 2, 1, null);
  197. INSERT INTO email_participants(id, kind, email_id, user_id, contact_id)
  198. VALUES(4, 'sent', 2, null, 2);
  199.  
  200. INSERT INTO email_participants(id, kind, email_id, user_id, contact_id)
  201. VALUES(5, 'to', 3, 1, null);
  202. INSERT INTO email_participants(id, kind, email_id, user_id, contact_id)
  203. VALUES(6, 'sent', 3, null, 3);
  204.  
  205. INSERT INTO email_participants(id, kind, email_id, user_id, contact_id)
  206. VALUES(7, 'to', 4, 1, null);
  207. INSERT INTO email_participants(id, kind, email_id, user_id, contact_id)
  208. VALUES(8, 'sent', 4, null, 4);
  209.  
  210. INSERT INTO email_participants(id, kind, email_id, user_id, contact_id)
  211. VALUES(9, 'to', 5, 1, null);
  212. INSERT INTO email_participants(id, kind, email_id, user_id, contact_id)
  213. VALUES(10, 'sent', 5, null, 5);
  214.  
  215. INSERT INTO email_participants(id, kind, email_id, user_id, contact_id)
  216. VALUES(11, 'to', 6, 1, null);
  217. INSERT INTO email_participants(id, kind, email_id, user_id, contact_id)
  218. VALUES(12, 'sent', 6, 2, null);
  219.  
  220. CREATE OR REPLACE VIEW user_accessible_emails AS
  221. SELECT emails.id AS email_id,
  222. users.id AS user_id
  223. FROM emails
  224. JOIN accounts ON emails.account_id = accounts.id
  225. JOIN users ON users.account_id = accounts.id
  226. WHERE emails.personal = false
  227. UNION
  228. SELECT emails.id AS email_id,
  229. email_participants.user_id
  230. FROM emails
  231. JOIN email_participants ON email_participants.email_id = emails.id
  232. WHERE emails.personal = true AND email_participants.user_id IS NOT NULL;
  233.  
  234. WITH "contacts_conv" AS
  235. (
  236. SELECT * FROM
  237. (
  238. SELECT "emails"."id", "email_participants"."contact_id", "emails"."sent_at", "emails"."subject",
  239. rank() OVER (PARTITION BY "email_participants"."contact_id" ORDER BY "sent_at" DESC) AS "contact_rnk"
  240. FROM "emails"
  241. INNER JOIN "email_participants"
  242. ON ("email_participants"."email_id" = "emails"."id")
  243. INNER JOIN "user_accessible_emails"
  244. ON ("user_accessible_emails"."email_id" = "emails"."id")
  245. INNER JOIN "contacts" ON ("emails"."sender_contact_id" = "contacts"."id")
  246. WHERE
  247. (
  248. ("emails"."sent_at" IS NOT NULL) AND
  249. ("ignored" IS NOT TRUE) AND
  250. ("emails"."folder" != 'archived') AND
  251. ("user_accessible_emails"."user_id" = 1) AND
  252. ("email_participants"."contact_id" IS NOT NULL) AND
  253. ("email_participants"."kind" = 'sent')
  254. )
  255. )
  256. AS "e" WHERE ("contact_rnk" = 1)
  257. ), "users_conv" AS
  258. (
  259. SELECT * FROM
  260. (
  261. SELECT "emails"."id", "email_participants"."contact_id", "emails"."sent_at", "emails"."subject",
  262. rank() OVER (PARTITION BY "email_participants"."contact_id" ORDER BY "sent_at" DESC) AS "contact_rnk"
  263. FROM "emails"
  264. INNER JOIN "email_participants"
  265. ON ("email_participants"."email_id" = "emails"."id")
  266. INNER JOIN "user_accessible_emails"
  267. ON ("user_accessible_emails"."email_id" = "emails"."id")
  268. WHERE
  269. (
  270. ("emails"."folder" != 'archived') AND ("emails"."sent_at" IS NOT NULL) AND
  271. ("user_accessible_emails"."user_id" = 1) AND
  272. ("email_participants"."contact_id" IS NOT NULL) AND
  273. ("emails"."sender_user_id" IS NOT NULL)
  274. )
  275. )
  276. AS "e" WHERE ("contact_rnk" = 1)
  277. )
  278. SELECT DISTINCT "emails".* FROM "emails"
  279. INNER JOIN "contacts_conv" ON ("emails"."id" = "contacts_conv"."id")
  280. LEFT JOIN "users_conv" ON ("contacts_conv"."contact_id" = "users_conv"."contact_id")
  281. WHERE
  282. (
  283. ("users_conv"."sent_at" IS NULL) OR
  284. ("contacts_conv"."sent_at" > "users_conv"."sent_at")
  285. ) ORDER BY "emails"."sent_at" DESC;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement