renlabs-dev / commune-ts

This is a monorepo for the AGIc typescript ecosystem. It uses Turborepo and contains the code for our web apps.
https://www.communeai.org/
5 stars 1 forks source link

Add `comment_digest` sql view to Drizzle migrations #181

Open steinerkelvin opened 1 month ago

steinerkelvin commented 1 month ago

We need the comment_digest PostgreSQL view to be automatically created by drizzle-kit.

CREATE VIEW
  comment_digest AS
SELECT
  pc.id,
  pc.proposal_id,
  pc.user_key,
  pc.governance_model,
  pc.user_name,
  pc.content,
  pc.created_at,
  COALESCE(
    SUM(
      CASE
        WHEN ci.vote_type = 'UP' THEN 1
        ELSE 0
      END
    ),
    0
  ) AS upvotes,
  COALESCE(
    SUM(
      CASE
        WHEN ci.vote_type = 'DOWN' THEN 1
        ELSE 0
      END
    ),
    0
  ) AS downvotes
FROM
  proposal_comment pc
  LEFT JOIN comment_interaction ci ON pc.id = ci.comment_id
WHERE
  pc.deleted_at IS NULL
GROUP BY
  pc.id,
  pc.proposal_id,
  pc.governance_model,
  pc.user_key,
  pc.content,
  pc.created_at
ORDER BY
  pc.created_at ASC;