From 96c8dc1281d6e6722dd01e9326f8b5fc09e2199a Mon Sep 17 00:00:00 2001 From: Shota Gvinepadze Date: Wed, 26 Oct 2022 19:50:37 +0400 Subject: [PATCH] [MM-45868] Replace TeamId column with ThreadTeamId (#21505) * Replace TeamId column with TreadTeamId * make migrations-extract --- db/migrations/migrations.list | 4 +++ .../mysql/000094_threads_teamid.down.sql | 15 +------- .../mysql/000094_threads_teamid.up.sql | 20 +---------- .../000096_threads_threadteamid.down.sql | 14 ++++++++ .../mysql/000096_threads_threadteamid.up.sql | 35 +++++++++++++++++++ .../postgres/000094_threads_teamid.down.sql | 2 +- .../postgres/000094_threads_teamid.up.sql | 3 +- .../000096_threads_threadteamid.down.sql | 1 + .../000096_threads_threadteamid.up.sql | 5 +++ model/thread.go | 3 +- store/sqlstore/integrity.go | 2 +- store/sqlstore/post_store.go | 2 +- store/sqlstore/thread_store.go | 30 ++++++++-------- 13 files changed, 82 insertions(+), 54 deletions(-) create mode 100644 db/migrations/mysql/000096_threads_threadteamid.down.sql create mode 100644 db/migrations/mysql/000096_threads_threadteamid.up.sql create mode 100644 db/migrations/postgres/000096_threads_threadteamid.down.sql create mode 100644 db/migrations/postgres/000096_threads_threadteamid.up.sql diff --git a/db/migrations/migrations.list b/db/migrations/migrations.list index 1bab52d768..641cd312af 100644 --- a/db/migrations/migrations.list +++ b/db/migrations/migrations.list @@ -190,6 +190,8 @@ db/migrations/mysql/000094_threads_teamid.down.sql db/migrations/mysql/000094_threads_teamid.up.sql db/migrations/mysql/000095_remove_posts_parentid.down.sql db/migrations/mysql/000095_remove_posts_parentid.up.sql +db/migrations/mysql/000096_threads_threadteamid.down.sql +db/migrations/mysql/000096_threads_threadteamid.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 @@ -380,3 +382,5 @@ db/migrations/postgres/000094_threads_teamid.down.sql db/migrations/postgres/000094_threads_teamid.up.sql db/migrations/postgres/000095_remove_posts_parentid.down.sql db/migrations/postgres/000095_remove_posts_parentid.up.sql +db/migrations/postgres/000096_threads_threadteamid.down.sql +db/migrations/postgres/000096_threads_threadteamid.up.sql diff --git a/db/migrations/mysql/000094_threads_teamid.down.sql b/db/migrations/mysql/000094_threads_teamid.down.sql index 2ac406dc39..3b8e4a306e 100644 --- a/db/migrations/mysql/000094_threads_teamid.down.sql +++ b/db/migrations/mysql/000094_threads_teamid.down.sql @@ -1,14 +1 @@ -SET @preparedStatement = (SELECT IF( - EXISTS( - SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS - WHERE table_name = 'Threads' - AND table_schema = DATABASE() - AND column_name = 'TeamId' - ), - 'ALTER TABLE Threads DROP COLUMN TeamId;', - 'SELECT 1;' -)); - -PREPARE removeColumnIfExists FROM @preparedStatement; -EXECUTE removeColumnIfExists; -DEALLOCATE PREPARE removeColumnIfExists; +-- Replaced by 000096_threads_threadteamid.down.sql diff --git a/db/migrations/mysql/000094_threads_teamid.up.sql b/db/migrations/mysql/000094_threads_teamid.up.sql index 392a2d58ae..eb5e0807da 100644 --- a/db/migrations/mysql/000094_threads_teamid.up.sql +++ b/db/migrations/mysql/000094_threads_teamid.up.sql @@ -1,19 +1 @@ -SET @preparedStatement = (SELECT IF( - NOT EXISTS( - SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS - WHERE table_name = 'Threads' - AND table_schema = DATABASE() - AND column_name = 'TeamId' - ), - 'ALTER TABLE Threads ADD COLUMN TeamId varchar(26) DEFAULT NULL;', - 'SELECT 1;' -)); - -PREPARE addColumnIfNotExists FROM @preparedStatement; -EXECUTE addColumnIfNotExists; -DEALLOCATE PREPARE addColumnIfNotExists; - -UPDATE Threads, Channels -SET Threads.TeamId = Channels.TeamId -WHERE Channels.Id = Threads.ChannelId -AND Threads.TeamId IS NULL; +-- Replaced by 000096_threads_threadteamid.up.sql diff --git a/db/migrations/mysql/000096_threads_threadteamid.down.sql b/db/migrations/mysql/000096_threads_threadteamid.down.sql new file mode 100644 index 0000000000..bcbd8a5705 --- /dev/null +++ b/db/migrations/mysql/000096_threads_threadteamid.down.sql @@ -0,0 +1,14 @@ +SET @preparedStatement = (SELECT IF( + EXISTS( + SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS + WHERE table_name = 'Threads' + AND table_schema = DATABASE() + AND column_name = 'ThreadTeamId' + ), + 'ALTER TABLE Threads DROP COLUMN ThreadTeamId;', + 'SELECT 1;' +)); + +PREPARE removeColumnIfExists FROM @preparedStatement; +EXECUTE removeColumnIfExists; +DEALLOCATE PREPARE removeColumnIfExists; diff --git a/db/migrations/mysql/000096_threads_threadteamid.up.sql b/db/migrations/mysql/000096_threads_threadteamid.up.sql new file mode 100644 index 0000000000..420c2cd2f3 --- /dev/null +++ b/db/migrations/mysql/000096_threads_threadteamid.up.sql @@ -0,0 +1,35 @@ +-- Drop any existing TeamId column from 000094_threads_teamid.up.sql +SET @preparedStatement = (SELECT IF( + EXISTS( + SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS + WHERE table_name = 'Threads' + AND table_schema = DATABASE() + AND column_name = 'TeamId' + ) > 0, + 'ALTER TABLE Threads DROP COLUMN TeamId;', + 'SELECT 1;' +)); + +PREPARE removeColumnIfExists FROM @preparedStatement; +EXECUTE removeColumnIfExists; +DEALLOCATE PREPARE removeColumnIfExists; + +SET @preparedStatement = (SELECT IF( + NOT EXISTS( + SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS + WHERE table_name = 'Threads' + AND table_schema = DATABASE() + AND column_name = 'ThreadTeamId' + ), + 'ALTER TABLE Threads ADD COLUMN ThreadTeamId varchar(26) DEFAULT NULL;', + 'SELECT 1;' +)); + +PREPARE addColumnIfNotExists FROM @preparedStatement; +EXECUTE addColumnIfNotExists; +DEALLOCATE PREPARE addColumnIfNotExists; + +UPDATE Threads, Channels +SET Threads.ThreadTeamId = Channels.TeamId +WHERE Channels.Id = Threads.ChannelId +AND Threads.ThreadTeamId IS NULL; diff --git a/db/migrations/postgres/000094_threads_teamid.down.sql b/db/migrations/postgres/000094_threads_teamid.down.sql index 9f4c061f39..3b8e4a306e 100644 --- a/db/migrations/postgres/000094_threads_teamid.down.sql +++ b/db/migrations/postgres/000094_threads_teamid.down.sql @@ -1 +1 @@ -ALTER TABLE threads DROP COLUMN IF EXISTS teamid; +-- Replaced by 000096_threads_threadteamid.down.sql diff --git a/db/migrations/postgres/000094_threads_teamid.up.sql b/db/migrations/postgres/000094_threads_teamid.up.sql index 5c66138f1e..eb5e0807da 100644 --- a/db/migrations/postgres/000094_threads_teamid.up.sql +++ b/db/migrations/postgres/000094_threads_teamid.up.sql @@ -1,2 +1 @@ -ALTER TABLE threads ADD COLUMN IF NOT EXISTS teamid VARCHAR(26); -UPDATE threads SET teamid = channels.teamid FROM channels WHERE threads.teamid IS NULL AND channels.id = threads.channelid; +-- Replaced by 000096_threads_threadteamid.up.sql diff --git a/db/migrations/postgres/000096_threads_threadteamid.down.sql b/db/migrations/postgres/000096_threads_threadteamid.down.sql new file mode 100644 index 0000000000..8722c0c781 --- /dev/null +++ b/db/migrations/postgres/000096_threads_threadteamid.down.sql @@ -0,0 +1 @@ +ALTER TABLE threads DROP COLUMN IF EXISTS threadteamid; diff --git a/db/migrations/postgres/000096_threads_threadteamid.up.sql b/db/migrations/postgres/000096_threads_threadteamid.up.sql new file mode 100644 index 0000000000..30f5443d41 --- /dev/null +++ b/db/migrations/postgres/000096_threads_threadteamid.up.sql @@ -0,0 +1,5 @@ +-- Drop any existing TeamId column from 000094_threads_teamid.up.sql + ALTER TABLE threads DROP COLUMN IF EXISTS teamid; + +ALTER TABLE threads ADD COLUMN IF NOT EXISTS threadteamid VARCHAR(26); +UPDATE threads SET threadteamid = channels.teamid FROM channels WHERE threads.threadteamid IS NULL AND channels.id = threads.channelid; diff --git a/model/thread.go b/model/thread.go index 5d7bdb70f3..6513f41b40 100644 --- a/model/thread.go +++ b/model/thread.go @@ -27,7 +27,8 @@ type Thread struct { // named ThreadDeleteAt to avoid introducing a query conflict with older server versions. DeleteAt int64 `json:"delete_at"` - // TeamId is a denormalized copy of the Channel's teamId. + // TeamId is a denormalized copy of the Channel's teamId. In the database, it's + // named ThreadTeamId to avoid introducing a query conflict with older server versions. TeamId string `json:"team_id"` } diff --git a/store/sqlstore/integrity.go b/store/sqlstore/integrity.go index 3ee486aa9c..c3d1a04280 100644 --- a/store/sqlstore/integrity.go +++ b/store/sqlstore/integrity.go @@ -515,7 +515,7 @@ func checkUsersIntegrity(ss *SqlStore, results chan<- model.IntegrityCheckResult func checkThreadsTeamsIntegrity(ss *SqlStore) model.IntegrityCheckResult { return checkParentChildIntegrity(ss, relationalCheckConfig{ parentName: "Teams", - parentIdAttr: "TeamId", + parentIdAttr: "ThreadTeamId", childName: "Threads", childIdAttr: "PostId", canParentIdBeEmpty: false, diff --git a/store/sqlstore/post_store.go b/store/sqlstore/post_store.go index 14cec75d2b..0be20aa958 100644 --- a/store/sqlstore/post_store.go +++ b/store/sqlstore/post_store.go @@ -3004,7 +3004,7 @@ func (s *SqlPostStore) updateThreadsFromPosts(transaction *sqlxTxWrapper, posts } // no metadata entry, create one if _, err := transaction.NamedExec(`INSERT INTO Threads - (PostId, ChannelId, ReplyCount, LastReplyAt, Participants, TeamId) + (PostId, ChannelId, ReplyCount, LastReplyAt, Participants, ThreadTeamId) VALUES (:PostId, :ChannelId, :ReplyCount, :LastReplyAt, :Participants, :TeamId)`, &model.Thread{ PostId: rootId, diff --git a/store/sqlstore/thread_store.go b/store/sqlstore/thread_store.go index 3306a2856d..7cb84099a3 100644 --- a/store/sqlstore/thread_store.go +++ b/store/sqlstore/thread_store.go @@ -51,7 +51,7 @@ func (s *SqlThreadStore) initializeQueries() { "Threads.LastReplyAt", "Threads.Participants", "COALESCE(Threads.ThreadDeleteAt, 0) AS DeleteAt", - "COALESCE(Threads.TeamId, '') AS TeamId", + "COALESCE(Threads.ThreadTeamId, '') AS TeamId", ). From("Threads") @@ -63,7 +63,7 @@ func (s *SqlThreadStore) initializeQueries() { "Threads.LastReplyAt", "Threads.Participants", "COALESCE(Threads.ThreadDeleteAt, 0) AS ThreadDeleteAt", - "COALESCE(Threads.TeamId, '') AS TeamId", + "COALESCE(Threads.ThreadTeamId, '') AS TeamId", ). From("Threads") } @@ -98,8 +98,8 @@ func (s *SqlThreadStore) getTotalThreadsQuery(userId, teamId string, opts model. if teamId != "" { query = query. Where(sq.Or{ - sq.Eq{"Threads.TeamId": teamId}, - sq.Eq{"Threads.TeamId": ""}, + sq.Eq{"Threads.ThreadTeamId": teamId}, + sq.Eq{"Threads.ThreadTeamId": ""}, }) } @@ -160,8 +160,8 @@ func (s *SqlThreadStore) GetTotalUnreadMentions(userId, teamId string, opts mode if teamId != "" { query = query. Where(sq.Or{ - sq.Eq{"Threads.TeamId": teamId}, - sq.Eq{"Threads.TeamId": ""}, + sq.Eq{"Threads.ThreadTeamId": teamId}, + sq.Eq{"Threads.ThreadTeamId": ""}, }) } @@ -225,8 +225,8 @@ func (s *SqlThreadStore) GetThreadsForUser(userId, teamId string, opts model.Get if teamId != "" { query = query. Where(sq.Or{ - sq.Eq{"Threads.TeamId": teamId}, - sq.Eq{"Threads.TeamId": ""}, + sq.Eq{"Threads.ThreadTeamId": teamId}, + sq.Eq{"Threads.ThreadTeamId": ""}, }) } @@ -322,7 +322,7 @@ func (s *SqlThreadStore) GetTeamsUnreadForUser(userID string, teamIDs []string) fetchConditions := sq.And{ sq.Eq{"ThreadMemberships.UserId": userID}, sq.Eq{"ThreadMemberships.Following": true}, - sq.Eq{"Threads.TeamId": teamIDs}, + sq.Eq{"Threads.ThreadTeamId": teamIDs}, sq.Eq{"COALESCE(Threads.ThreadDeleteAt, 0)": 0}, } @@ -345,12 +345,12 @@ func (s *SqlThreadStore) GetTeamsUnreadForUser(userID string, teamIDs []string) go func() { defer wg.Done() repliesQuery := s.getQueryBuilder(). - Select("COUNT(Threads.PostId) AS Count, TeamId"). + Select("COUNT(Threads.PostId) AS Count, ThreadTeamId AS TeamId"). From("Threads"). LeftJoin("ThreadMemberships ON Threads.PostId = ThreadMemberships.PostId"). Where(fetchConditions). Where("Threads.LastReplyAt > ThreadMemberships.LastViewed"). - GroupBy("Threads.TeamId") + GroupBy("Threads.ThreadTeamId") err := s.GetReplicaX().SelectBuilder(&unreadThreads, repliesQuery) if err != nil { @@ -362,11 +362,11 @@ func (s *SqlThreadStore) GetTeamsUnreadForUser(userID string, teamIDs []string) go func() { defer wg.Done() mentionsQuery := s.getQueryBuilder(). - Select("COALESCE(SUM(ThreadMemberships.UnreadMentions),0) AS Count, TeamId"). + Select("COALESCE(SUM(ThreadMemberships.UnreadMentions),0) AS Count, ThreadTeamId AS TeamId"). From("ThreadMemberships"). LeftJoin("Threads ON Threads.PostId = ThreadMemberships.PostId"). Where(fetchConditions). - GroupBy("Threads.TeamId") + GroupBy("Threads.ThreadTeamId") err := s.GetReplicaX().SelectBuilder(&unreadMentions, mentionsQuery) if err != nil { @@ -461,7 +461,7 @@ func (s *SqlThreadStore) GetThreadForUser(teamId string, threadMembership *model }) fetchConditions := sq.And{ - sq.Or{sq.Eq{"Threads.TeamId": teamId}, sq.Eq{"Threads.TeamId": ""}}, + sq.Or{sq.Eq{"Threads.ThreadTeamId": teamId}, sq.Eq{"Threads.ThreadTeamId": ""}}, sq.Eq{"Threads.PostId": threadMembership.PostId}, } @@ -670,7 +670,7 @@ func (s *SqlThreadStore) GetMembershipsForUser(userId, teamId string) ([]*model. Select("ThreadMemberships.*"). Join("Threads ON Threads.PostId = ThreadMemberships.PostId"). From("ThreadMemberships"). - Where(sq.Or{sq.Eq{"Threads.TeamId": teamId}, sq.Eq{"Threads.TeamId": ""}}). + Where(sq.Or{sq.Eq{"Threads.ThreadTeamId": teamId}, sq.Eq{"Threads.ThreadTeamId": ""}}). Where(sq.Eq{"ThreadMemberships.UserId": userId}) err := s.GetReplicaX().SelectBuilder(&memberships, query)