|
| 1 | +# Copyright 2016 Eugene Frolov <eugene@frolov.net.ru> |
| 2 | +# |
| 3 | +# All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | +# not use this file except in compliance with the License. You may obtain |
| 7 | +# a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | +# License for the specific language governing permissions and limitations |
| 15 | +# under the License. |
| 16 | + |
| 17 | +from restalchemy.storage.sql import migrations |
| 18 | + |
| 19 | + |
| 20 | +def _user_messages_view(reaction_users): |
| 21 | + return f""" |
| 22 | +CREATE OR REPLACE VIEW "m_workspace_user_messages_view" AS |
| 23 | +SELECT |
| 24 | + m.uuid AS uuid, |
| 25 | + m.stream_uuid, |
| 26 | + m.user_uuid AS author_uuid, |
| 27 | + m.topic_uuid, |
| 28 | + m.payload, |
| 29 | + m.created_at, |
| 30 | + m.updated_at, |
| 31 | + b.user_uuid AS user_uuid, |
| 32 | + m.project_id, |
| 33 | + COALESCE(f.read, FALSE) AS read, |
| 34 | + COALESCE(f.pinned, FALSE) AS pinned, |
| 35 | + COALESCE(f.starred, FALSE) AS starred, |
| 36 | + (m.user_uuid = b.user_uuid) AS is_own, |
| 37 | + COALESCE( |
| 38 | + ( |
| 39 | + SELECT jsonb_object_agg( |
| 40 | + reaction_counts.emoji_name, |
| 41 | + reaction_counts.reaction_count |
| 42 | + ) |
| 43 | + FROM ( |
| 44 | + SELECT |
| 45 | + r.emoji_name, |
| 46 | + COUNT(*) AS reaction_count |
| 47 | + FROM "m_workspace_message_reactions" AS r |
| 48 | + WHERE r.project_id = m.project_id |
| 49 | + AND r.message_uuid = m.uuid |
| 50 | + GROUP BY r.emoji_name |
| 51 | + ) AS reaction_counts |
| 52 | + ), |
| 53 | + '{{}}'::jsonb |
| 54 | + ) AS reactions, |
| 55 | + m.source_name, |
| 56 | + m.source, |
| 57 | + POSITION( |
| 58 | + '](' || 'urn:user:' || LOWER(b.user_uuid::text) || ')' |
| 59 | + IN LOWER(COALESCE(m.payload->>'content', '')) |
| 60 | + ) > 0 AS mentioned, |
| 61 | + {reaction_users} AS reaction_users |
| 62 | +FROM "m_workspace_messages" AS m |
| 63 | +JOIN "m_workspace_stream_bindings" AS b |
| 64 | + ON b.stream_uuid = m.stream_uuid |
| 65 | + AND b.project_id = m.project_id |
| 66 | +JOIN "m_workspace_streams" AS stream |
| 67 | + ON stream.uuid = m.stream_uuid |
| 68 | + AND stream.project_id = m.project_id |
| 69 | +LEFT JOIN "m_workspace_user_message_flags" AS f |
| 70 | + ON f.uuid = m.uuid |
| 71 | + AND f.user_uuid = b.user_uuid |
| 72 | + AND f.project_id = m.project_id |
| 73 | +LEFT JOIN "m_confirmed_external_stream_access" AS access |
| 74 | + ON access.project_id = m.project_id |
| 75 | + AND access.user_uuid = b.user_uuid |
| 76 | + AND access.stream_uuid = m.stream_uuid |
| 77 | +WHERE stream.source_name = 'native' |
| 78 | + OR access.user_uuid IS NOT NULL; |
| 79 | +""" |
| 80 | + |
| 81 | + |
| 82 | +class MigrationStep(migrations.AbstractMigrationStep): |
| 83 | + def __init__(self): |
| 84 | + self._depends = ["0126-index-topic-read-boundaries-20ae22.py"] |
| 85 | + |
| 86 | + @property |
| 87 | + def migration_id(self): |
| 88 | + return "547d747d-c9f1-4583-80d9-b932c1a5df2a" |
| 89 | + |
| 90 | + @property |
| 91 | + def is_manual(self): |
| 92 | + return False |
| 93 | + |
| 94 | + def upgrade(self, session): |
| 95 | + session.execute( |
| 96 | + """ |
| 97 | + ALTER TABLE "m_workspace_messages" |
| 98 | + ADD COLUMN "reaction_users" JSONB NOT NULL DEFAULT '{}'::jsonb; |
| 99 | + """ |
| 100 | + ) |
| 101 | + session.execute(_user_messages_view('m."reaction_users"')) |
| 102 | + |
| 103 | + def downgrade(self, session): |
| 104 | + # PostgreSQL cannot remove a trailing view column with CREATE OR |
| 105 | + # REPLACE. Keep a harmless constant column for old readers while |
| 106 | + # severing the dependency before dropping canonical storage. |
| 107 | + session.execute(_user_messages_view("'{}'::jsonb")) |
| 108 | + session.execute( |
| 109 | + """ |
| 110 | + ALTER TABLE "m_workspace_messages" |
| 111 | + DROP COLUMN "reaction_users"; |
| 112 | + """ |
| 113 | + ) |
| 114 | + |
| 115 | + |
| 116 | +migration_step = MigrationStep() |
0 commit comments