|
| 1 | +import { MigrationInterface, QueryRunner } from 'typeorm'; |
| 2 | + |
| 3 | +export class AddParentandConversationToView1764236169455 implements MigrationInterface { |
| 4 | + name = 'AddParentandConversationToView1764236169455'; |
| 5 | + |
| 6 | + public async up(query_runner: QueryRunner): Promise<void> { |
| 7 | + await query_runner.query( |
| 8 | + `DELETE FROM "typeorm_metadata" WHERE "type" = $1 AND "name" = $2 AND "schema" = $3`, |
| 9 | + ['VIEW', 'user_posts_view', 'public'] |
| 10 | + ); |
| 11 | + await query_runner.query(`DROP VIEW "user_posts_view"`); |
| 12 | + |
| 13 | + await query_runner.query(`CREATE VIEW "user_posts_view" AS |
| 14 | + SELECT |
| 15 | + t.tweet_id::text AS id, |
| 16 | + t.user_id AS profile_user_id, |
| 17 | + t.user_id AS tweet_author_id, |
| 18 | + t.tweet_id, |
| 19 | + NULL::uuid AS repost_id, |
| 20 | + 'tweet' AS post_type, |
| 21 | + t.created_at AS post_date, |
| 22 | + t.type::text AS type, |
| 23 | + t.content, |
| 24 | + t.images, |
| 25 | + t.videos, |
| 26 | + t.num_likes, |
| 27 | + t.num_reposts, |
| 28 | + t.num_views, |
| 29 | + t.num_quotes, |
| 30 | + t.num_replies, |
| 31 | + t.created_at, |
| 32 | + t.updated_at, |
| 33 | + u.username, |
| 34 | + u.name, |
| 35 | + u.followers, |
| 36 | + u.following, |
| 37 | + u.avatar_url, |
| 38 | + u.cover_url, |
| 39 | + u.verified, |
| 40 | + u.bio, |
| 41 | + NULL::text AS reposted_by_name, |
| 42 | + COALESCE(tq.original_tweet_id, trep.original_tweet_id) AS parent_id, |
| 43 | + trep.conversation_id AS conversation_id |
| 44 | + FROM tweets t |
| 45 | + INNER JOIN "user" u ON t.user_id = u.id |
| 46 | + LEFT JOIN tweet_quotes tq ON t.tweet_id = tq.quote_tweet_id |
| 47 | + LEFT JOIN tweet_replies trep ON t.tweet_id = trep.reply_tweet_id |
| 48 | + |
| 49 | + UNION ALL |
| 50 | + |
| 51 | + SELECT |
| 52 | + (tr.tweet_id::text || '_' || tr.user_id::text) AS id, |
| 53 | + tr.user_id AS profile_user_id, |
| 54 | + t.user_id AS tweet_author_id, |
| 55 | + tr.tweet_id, |
| 56 | + tr.tweet_id AS repost_id, |
| 57 | + t.type::text AS post_type, |
| 58 | + tr.created_at AS post_date, |
| 59 | + 'repost' AS type, |
| 60 | + t.content, |
| 61 | + t.images, |
| 62 | + t.videos, |
| 63 | + t.num_likes, |
| 64 | + t.num_reposts, |
| 65 | + t.num_views, |
| 66 | + t.num_quotes, |
| 67 | + t.num_replies, |
| 68 | + t.created_at, |
| 69 | + t.updated_at, |
| 70 | + u.username, |
| 71 | + u.name, |
| 72 | + u.followers, |
| 73 | + u.following, |
| 74 | + u.avatar_url, |
| 75 | + u.cover_url, |
| 76 | + u.verified, |
| 77 | + u.bio, |
| 78 | + reposter.name AS reposted_by_name, |
| 79 | + COALESCE(tq.original_tweet_id, trep.original_tweet_id) AS parent_id, |
| 80 | + trep.conversation_id AS conversation_id |
| 81 | +
|
| 82 | + FROM tweet_reposts tr |
| 83 | + INNER JOIN tweets t ON tr.tweet_id = t.tweet_id |
| 84 | + INNER JOIN "user" u ON t.user_id = u.id |
| 85 | + INNER JOIN "user" reposter ON tr.user_id = reposter.id |
| 86 | + LEFT JOIN tweet_quotes tq ON t.tweet_id = tq.quote_tweet_id |
| 87 | + LEFT JOIN tweet_replies trep ON t.tweet_id = trep.reply_tweet_id |
| 88 | + `); |
| 89 | + await query_runner.query( |
| 90 | + `INSERT INTO "typeorm_metadata"("database", "schema", "table", "type", "name", "value") VALUES (DEFAULT, $1, DEFAULT, $2, $3, $4)`, |
| 91 | + [ |
| 92 | + 'public', |
| 93 | + 'VIEW', |
| 94 | + 'user_posts_view', |
| 95 | + 'SELECT \n t.tweet_id::text AS id,\n t.user_id AS profile_user_id,\n t.user_id AS tweet_author_id,\n t.tweet_id,\n NULL::uuid AS repost_id,\n \'tweet\' AS post_type,\n t.created_at AS post_date,\n t.type::text AS type,\n t.content,\n t.images,\n t.videos,\n t.num_likes,\n t.num_reposts,\n t.num_views,\n t.num_quotes,\n t.num_replies,\n t.created_at,\n t.updated_at,\n u.username,\n u.name,\n u.followers,\n u.following,\n u.avatar_url,\n u.cover_url,\n u.verified,\n u.bio,\n NULL::text AS reposted_by_name,\n COALESCE(tq.original_tweet_id, trep.original_tweet_id) AS parent_id,\n trep.conversation_id AS conversation_id\n FROM tweets t\n INNER JOIN "user" u ON t.user_id = u.id\n LEFT JOIN tweet_quotes tq ON t.tweet_id = tq.quote_tweet_id\n LEFT JOIN tweet_replies trep ON t.tweet_id = trep.reply_tweet_id\n \n UNION ALL\n \n SELECT \n (tr.tweet_id::text || \'_\' || tr.user_id::text) AS id,\n tr.user_id AS profile_user_id,\n t.user_id AS tweet_author_id,\n tr.tweet_id,\n tr.tweet_id AS repost_id,\n t.type::text AS post_type,\n tr.created_at AS post_date,\n \'repost\' AS type,\n t.content,\n t.images,\n t.videos,\n t.num_likes,\n t.num_reposts,\n t.num_views,\n t.num_quotes,\n t.num_replies,\n t.created_at,\n t.updated_at,\n u.username,\n u.name,\n u.followers,\n u.following,\n u.avatar_url,\n u.cover_url,\n u.verified,\n u.bio,\n reposter.name AS reposted_by_name,\n COALESCE(tq.original_tweet_id, trep.original_tweet_id) AS parent_id,\n trep.conversation_id AS conversation_id\n\n FROM tweet_reposts tr\n INNER JOIN tweets t ON tr.tweet_id = t.tweet_id\n INNER JOIN "user" u ON t.user_id = u.id\n INNER JOIN "user" reposter ON tr.user_id = reposter.id\n LEFT JOIN tweet_quotes tq ON t.tweet_id = tq.quote_tweet_id\n LEFT JOIN tweet_replies trep ON t.tweet_id = trep.reply_tweet_id', |
| 96 | + ] |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + public async down(query_runner: QueryRunner): Promise<void> { |
| 101 | + await query_runner.query( |
| 102 | + `DELETE FROM "typeorm_metadata" WHERE "type" = $1 AND "name" = $2 AND "schema" = $3`, |
| 103 | + ['VIEW', 'user_posts_view', 'public'] |
| 104 | + ); |
| 105 | + await query_runner.query(`DROP VIEW "user_posts_view"`); |
| 106 | + await query_runner.query(`CREATE VIEW "user_posts_view" AS SELECT |
| 107 | + t.tweet_id::text AS id, |
| 108 | + t.user_id AS profile_user_id, |
| 109 | + t.user_id AS tweet_author_id, |
| 110 | + t.tweet_id, |
| 111 | + NULL::uuid AS repost_id, |
| 112 | + 'tweet' AS post_type, |
| 113 | + t.created_at AS post_date, |
| 114 | + t.type::text AS type, |
| 115 | + t.content, |
| 116 | + t.images, |
| 117 | + t.videos, |
| 118 | + t.num_likes, |
| 119 | + t.num_reposts, |
| 120 | + t.num_views, |
| 121 | + t.num_quotes, |
| 122 | + t.num_replies, |
| 123 | + t.created_at, |
| 124 | + t.updated_at, |
| 125 | + u.username, |
| 126 | + u.name, |
| 127 | + u.followers, |
| 128 | + u.following, |
| 129 | + u.avatar_url, |
| 130 | + u.cover_url, |
| 131 | + u.verified, |
| 132 | + u.bio, |
| 133 | + NULL::text AS reposted_by_name |
| 134 | + FROM tweets t |
| 135 | + INNER JOIN "user" u ON t.user_id = u.id |
| 136 | + |
| 137 | + UNION ALL |
| 138 | + |
| 139 | + SELECT |
| 140 | + (tr.tweet_id::text || '_' || tr.user_id::text) AS id, |
| 141 | + tr.user_id AS profile_user_id, |
| 142 | + t.user_id AS tweet_author_id, |
| 143 | + tr.tweet_id, |
| 144 | + tr.tweet_id AS repost_id, |
| 145 | + t.type::text AS post_type, |
| 146 | + tr.created_at AS post_date, |
| 147 | + 'repost' AS type, |
| 148 | + t.content, |
| 149 | + t.images, |
| 150 | + t.videos, |
| 151 | + t.num_likes, |
| 152 | + t.num_reposts, |
| 153 | + t.num_views, |
| 154 | + t.num_quotes, |
| 155 | + t.num_replies, |
| 156 | + t.created_at, |
| 157 | + t.updated_at, |
| 158 | + u.username, |
| 159 | + u.name, |
| 160 | + u.followers, |
| 161 | + u.following, |
| 162 | + u.avatar_url, |
| 163 | + u.cover_url, |
| 164 | + u.verified, |
| 165 | + u.bio, |
| 166 | + reposter.name AS reposted_by_name |
| 167 | +
|
| 168 | + FROM tweet_reposts tr |
| 169 | + INNER JOIN tweets t ON tr.tweet_id = t.tweet_id |
| 170 | + INNER JOIN "user" u ON t.user_id = u.id |
| 171 | + INNER JOIN "user" reposter ON tr.user_id = reposter.id`); |
| 172 | + await query_runner.query( |
| 173 | + `INSERT INTO "typeorm_metadata"("database", "schema", "table", "type", "name", "value") VALUES (DEFAULT, $1, DEFAULT, $2, $3, $4)`, |
| 174 | + [ |
| 175 | + 'public', |
| 176 | + 'VIEW', |
| 177 | + 'user_posts_view', |
| 178 | + 'SELECT \n t.tweet_id::text AS id,\n t.user_id AS profile_user_id,\n t.user_id AS tweet_author_id,\n t.tweet_id,\n NULL::uuid AS repost_id,\n \'tweet\' AS post_type,\n t.created_at AS post_date,\n t.type::text AS type,\n t.content,\n t.images,\n t.videos,\n t.num_likes,\n t.num_reposts,\n t.num_views,\n t.num_quotes,\n t.num_replies,\n t.created_at,\n t.updated_at,\n u.username,\n u.name,\n u.followers,\n u.following,\n u.avatar_url,\n u.cover_url,\n u.verified,\n u.bio,\n NULL::text AS reposted_by_name\n FROM tweets t\n INNER JOIN "user" u ON t.user_id = u.id\n \n UNION ALL\n \n SELECT \n (tr.tweet_id::text || \'_\' || tr.user_id::text) AS id,\n tr.user_id AS profile_user_id,\n t.user_id AS tweet_author_id,\n tr.tweet_id,\n tr.tweet_id AS repost_id,\n t.type::text AS post_type,\n tr.created_at AS post_date,\n \'repost\' AS type,\n t.content,\n t.images,\n t.videos,\n t.num_likes,\n t.num_reposts,\n t.num_views,\n t.num_quotes,\n t.num_replies,\n t.created_at,\n t.updated_at,\n u.username,\n u.name,\n u.followers,\n u.following,\n u.avatar_url,\n u.cover_url,\n u.verified,\n u.bio,\n reposter.name AS reposted_by_name\n\n FROM tweet_reposts tr\n INNER JOIN tweets t ON tr.tweet_id = t.tweet_id\n INNER JOIN "user" u ON t.user_id = u.id\n INNER JOIN "user" reposter ON tr.user_id = reposter.id', |
| 179 | + ] |
| 180 | + ); |
| 181 | + } |
| 182 | +} |
0 commit comments