[MM-45444] Denormalize Reactions to add ChannelId for top reactions insights query (#20572)

* Denormalize Reactions to add ChannelId for top reactions insights query

* Remove hardcoded timestamps

* Fix store, api4 tests for reactions

* Fix tests

* Fix integrity tests, allow reaction to have ChannelId populated before calling store function

* Lint fixes

* Add ChannelId field to BulkGetForPosts, Delete store handlers

* Add index to mysql migration, add not null characteristic without a separate command

* Select channelId instead of fetching post via store.GetPost, add if exists to drop column

* Make updating of Reactions conditional to support pre-migration

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Shivashis Padhi
2022-07-11 18:55:03 +05:30
коммит произвёл GitHub
родитель 7bbf30d6bc
Коммит 20d690b412
9 изменённых файлов: 148 добавлений и 29 удалений

Просмотреть файл

@@ -176,6 +176,8 @@ db/migrations/mysql/000087_sidebar_categories_index.down.sql
db/migrations/mysql/000087_sidebar_categories_index.up.sql
db/migrations/mysql/000088_remaining_migrations.down.sql
db/migrations/mysql/000088_remaining_migrations.up.sql
db/migrations/mysql/000089_add-channelid-to-reaction.down.sql
db/migrations/mysql/000089_add-channelid-to-reaction.up.sql
db/migrations/postgres/000001_create_teams.down.sql
db/migrations/postgres/000001_create_teams.up.sql
db/migrations/postgres/000002_create_team_members.down.sql
@@ -352,3 +354,5 @@ db/migrations/postgres/000087_sidebar_categories_index.down.sql
db/migrations/postgres/000087_sidebar_categories_index.up.sql
db/migrations/postgres/000088_remaining_migrations.down.sql
db/migrations/postgres/000088_remaining_migrations.up.sql
db/migrations/postgres/000089_add-channelid-to-reaction.down.sql
db/migrations/postgres/000089_add-channelid-to-reaction.up.sql

Просмотреть файл

@@ -0,0 +1,14 @@
SET @preparedStatement = (SELECT IF(
EXISTS(
SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS
WHERE table_name = 'Reactions'
AND table_schema = DATABASE()
AND column_name = 'ChannelId'
) > 0,
'ALTER TABLE Reactions DROP COLUMN ChannelId;',
'SELECT 1;'
));
PREPARE removeColumnIfExists FROM @preparedStatement;
EXECUTE removeColumnIfExists;
DEALLOCATE PREPARE removeColumnIfExists;

Просмотреть файл

@@ -0,0 +1,33 @@
SET @preparedStatement = (SELECT IF(
NOT EXISTS(
SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'Reactions'
AND table_schema = DATABASE()
AND column_name = 'ChannelId'
),
'ALTER TABLE Reactions ADD COLUMN ChannelId varchar(26) NOT NULL DEFAULT "";',
'SELECT 1;'
));
PREPARE addColumnIfNotExists FROM @preparedStatement;
EXECUTE addColumnIfNotExists;
DEALLOCATE PREPARE addColumnIfNotExists;
UPDATE Reactions SET ChannelId = (select ChannelId from Posts where Posts.Id = Reactions.PostId) WHERE ChannelId="";
SET @preparedStatement = (SELECT IF(
(
SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS
WHERE table_name = 'Reactions'
AND table_schema = DATABASE()
AND index_name = 'idx_reactions_channel_id'
) > 0,
'SELECT 1',
'CREATE INDEX idx_reactions_channel_id ON Reactions(ChannelId);'
));
PREPARE createIndexIfNotExists FROM @preparedStatement;
EXECUTE createIndexIfNotExists;
DEALLOCATE PREPARE createIndexIfNotExists;

Просмотреть файл

@@ -0,0 +1 @@
ALTER TABLE reactions DROP COLUMN IF EXISTS channelid;

Просмотреть файл

@@ -0,0 +1,3 @@
ALTER TABLE reactions ADD COLUMN IF NOT EXISTS channelid varchar(26) NOT NULL DEFAULT '';
UPDATE reactions SET channelid = (select channelid from posts where posts.id = reactions.postid) WHERE channelid='';
CREATE INDEX IF NOT EXISTS idx_reactions_channel_id on reactions (channelid);