From abc438ef9e1fc3be38399a040d34f873255e0969 Mon Sep 17 00:00:00 2001 From: Harshil Sharma <18575143+harshilsharma63@users.noreply.github.com> Date: Sat, 26 Mar 2022 04:51:56 +0530 Subject: [PATCH] Migrate DM and private group message boards to a team (#2557) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * wip * Added data migration for populating categories * wip * Added data migration for populating categories * Store WIP * migration WIP * category CRUD APIs complete * category block API WIP * block category update API done * Fetcehed data into store * Started displayting sidebar data * sidebar WIP * Dashboard - basic changes * Sidebar dashboard btn and board switcher UI only * Sidebar dashboard btn and board switcher UI only * create category dialog WIP * Create category webapp side done * Integrated move card to other category * board to block * Disabled dashboard route for now as we'll implement it in phase 2 * WIP * Added logic to open last board/view on per team level * Add workspace to teams and boards migrations (#1986) * Add workspace to teams and boards migrations * Update json annotations on board models * boards search dialog WIP * Seach dialog WIP * Implemented opening boiard from search results * Boards switcher styliung * Handled update category WS event * Template support * personal server support and styling fixes * test fix WIP * Fixed a bug causing boards to not be moved correctly beteen categories * Fixed webapp tests * fix * Store changes (#2011) * Permissions phase 1 - Websocket updates (#2014) * Store changes * Websockets changes * Permissions phase 1 - Permissions service (#2015) * Store changes * Websockets changes * Permissions service * Api and app updates (#2016) * Store changes * Websockets changes * Permissions service * New API and App changes * Delete and Patch boards and blocks endpoints * Used correct variable * Webapp changes WIP * Open correct team URL * Fixed get block API * Used React context for workspace users * WIP * On load navigation sorted out * WIP * Nav fix * categories WS broadcast * Used real search API * Fixed unfurl ppreview * set active team in sidebar * IMplemented navigation on changing team in sidebar * Misc fixes * close rows inside transaction (#2045) * update syntax for mysql (#2044) * Upadted mutator for new patchBlock API * Updated patchBlock API to use new URL * Listeining to correct event in plugin mode * Implemented WS messages for category operations: * Fix duplicated build tags on Makefile * Sidebar enhancements * Add missing prefix to SQLite migration and fix flaky tests * Sidebar boards menu enhancement * Fix board page interactions (#2144) * Fix patch board card properties error * Fix board interactions * Fix insert blocks interactions * Fix app tests (#2104) * Add json1 tag to vscode launch (#2157) * Fix add, delete and update boards and add board patch generation (#2146) * Fix update boards and add board patch generation * Make add board and add template work, as well as deleting a board * Update the state on board deletion * Delete unused variable * Fix bad parenthesis * Fix board creation inside plugin, options were coming null due websocket message serialization * update property type mutators to use boards API (#2168) * Add permissions modal (#2196) * Initial integration * Permissions modal, websocket updates and API tests implemented * Avoid updating/removing user if there is only one admin left * Fix duplicates on board search * Adds integration test * Addressing PR review comments Co-authored-by: Jesús Espino * Merge * I'm able to compile now * Some fixes around tests execution * Fixing migrations * Fixing migrations order * WIP * Fixing some other compilation problems on tests * Some typescript tests fixed * Fixing javascript tests * Fixing compilation * Fixing some problems to create boards * Load the templates on initial load * Improvements over initial team templates import * Adding new fields in the database * Working on adding duplicate board api * Removing RootID concept entirely * Improving a bit the subscriptions * Fixing store tests for notificationHints * Fixing more tests * fixing tests * Fixing tests * Fixing tests * Fixing some small bugs related to templates * Fixing registration link generation/regeneration * Fixing cypress tests * Adding store tests for duplicateBoard and duplicateBlock * Addressing some TODO comments * Making the export api simpler * Add redirect component for old workspace urls * Removing Dashboard code * Delete only the built-in templates on update * fixing tests * Adding users autocompletion * Updating snapshots * Fixing bad merge * fix panic when creating new card in notifysubscriptions (#2352) * fix lint errors (#2353) * fix lint errors * fix panic when creating new card in notifysubscriptions (#2352) * fix lint errors * fix unit test * Revert "fix unit test" This reverts commit 0ad78aed65745521c0bb45790c9ea91b6c316c44. Co-authored-by: Doug Lauder * fix sql syntax error for SearchUsersByTeam (#2357) * Fix mentions delivery (#2358) * fix sql syntax error for SearchUsersByTeam * fix mentions delivery Co-authored-by: Mattermod * update api for octoClient calls, pass correct variables to mutator (#2359) * Fixing tests after merge * Fix sidebar context menu UI issue (#2399) * Fix notification diff for text blocks (#2386) * fix notification diff for text blocks; fix various linter errors. * fix URLs to cards Co-authored-by: Mattermod * Permissions branch: Fix card links (#2391) * fix notification diff for text blocks; fix various linter errors. * fix URLs to cards Co-authored-by: Mattermod * Fixing sqlite tests * Fixing server tests * Update migrations to create global templates. (#2397) * fix duplicate templates * revert migrate.go * update UI for empty templates * implement updating built-in templates as global (teamId = 0) * handle error if board not found * update unit test * fix more tests * Update blocks_test.go Fix merge issue * fix migration sql error (#2414) * Fixing frontend tests * Set target team ID when using a global template (#2419) * Fix some server tests * Fixing onboarding creation * Permissions branch: Fix unit tests and CI errors (part 1) (#2425) * Fixing some small memory leaks (#2400) * Fixing some small memory leaks * fixing tests * passing the tags to all test targets * Increasing the timeout of the tests * Fix some type checkings * Permissions branch: Fixes all the linter errors (#2429) * fix linter errors * Reestructuring the router and splitting in more subcomponents (#2403) * Reestructuring the router and splitting in more subcomponents * Removing console.log calls * Removing unneeded selector * Addressing PR comment * Fix redirection to one team when you load directly the boards home path * Using properly the lastTeamID to redirect the user if needed * don't allow last admin change/deleted (#2416) * don't allow last admin change/deleted * update for i18-extract * fixed en.json Co-authored-by: Mattermod Co-authored-by: Harshil Sharma * Splitting BoardPage component into simpler/smaller components (#2435) * Splitting BoardPage component into simpler/smaller components * Removing unneeded import * Replace go migrate with morph permissions (#2424) * merge origin/replace-go-migrate-with-morph * run go mod tidy on mattermost-plugin and increase test timeout * fix merge issue temprorarily * remove some debug changes * fixing the linter * Allow always team 0 (global) templates fetch (#2472) * Fix problem with viewId 0 in the URL (#2473) * Migrate from binddata to goembed (#2471) * Adding join logic to the board switcher (#2434) * Adding join logic to the board switcher * Using already existing client function and removing the joinBoard one * Adding support for autojoin based on url * Fixing frontend tests * fix webapp compile error, missing enableSharedBoards (#2501) * Fixing duplication on postgres * Adding back views to the sidebar (#2494) * Fix #2507. Update Swagger comments (#2508) * Fix the flash of the template selector on board/team switch (#2490) * Fix the flash of the template selector on board/team switch * More fixes specially around error handling * Fixing the bot badge (#2487) * simplifying a bit the team store sync between channels and focalboard (#2481) * Fix menu tests (#2528) * fix failing menu tests * fix lint error * Added keyboard shortcut for boards switcher (#2407) * Added keyboard shortcut for boards switcher * Fixed a type error * Added some inline comments * Fixed lint * Fixed bug with scroll jumping when the card is opened: (#2477) - avoid remounting of `ScrollingComponent` for each render of `Kanban` component - property `autoFocus` set to false for `CalculationOptions` because it triggers `blur` even for the button in Jest tests and closes the menu - snapshots for tests with `CalculationOptions` updated * Adding the frontend support for permissions and applying it to a big part of the interface. (#2536) * Initial work on permissions gates * Applying permissions gates in more places * Adding more checks to the interface * Adding more permissions gates and keeping the store up to date * fixing some tests * Fixing some more tests * Fixing another test * Fixing all tests and adding some more * Adding no-permission snapshot tests * Addressing PR review comments * Fixing invert behavior * Permissions branch: No sqlstore calls after app shutdown (#2530) * fix webapp compile error, missing enableSharedBoards * refactor app init wip * - ensure all block change notifications are finished before shutting down app - fix unit tests for mysql (insert_at only has 1 second resolution!) * adjust logging Co-authored-by: Mattermod * Fixed migrations to allow upgrading from previous version (#2535) * Added mechanism to check if schema migration is needed * WIP * WIP * WIP * WIP * Fixed migration * Fixed for SQLite * minor cleaniup * Deleted old schema migration table after running migrations * Removed a debug log * Fixed a bug where the code always tried to delete a table which may or may not exist * Show properly the user avatar in the ShareBoard component (#2542) * Fixing the last CI problems from the permissions-branch (#2541) * Fix history ordering * Giving some times to avoid possible race conditions * Empty * Reverting accidental change in the config.json * Fixed psql migrations for old focalbaord archives * Cleanup * Added missing board type update command * Optimizing table view (#2540) * Optimizing table view * Reducing the amount of rendering for tables * Some other performance improvements * Improve the activeView updates * Some extra simplifications * Another small improvement * Fixing tests * Fixing linter errors * Reducing a bit the amount of dependency with big objects in the store * Small simplification * Removing Commenter role from the user role selector (#2561) * Shareboard cleanup (#2550) * Initial work on permissions gates * Applying permissions gates in more places * Adding more checks to the interface * Adding more permissions gates and keeping the store up to date * fixing some tests * Fixing some more tests * Fixing another test * Fixing all tests and adding some more * Adding no-permission snapshot tests * Addressing PR review comments * cleanup some shareboard settings * remove unused property, fix for user items being displayed for non admin * revert change, allow users to show Co-authored-by: Jesús Espino Co-authored-by: Mattermod * Fixing comments and cards with the new optimizations in the store (#2560) * Fixing property creation (#2563) * Fix user selection in table view (#2565) * Fixing focus new row in table view (#2567) * Permissions branch: Fix sqlite table lock (CI) (#2568) * fix sqlite table lock * remove test db on teardown * revert .gitignore * fix goimport on migration code * fix typo * more linter fixes * clean up tmp db for sqlstore tests Co-authored-by: Mattermod * Fixing snapshots * Migrating center panel to functional component (#2562) * Migrating center panel to functional component * Fixing some tests * Fixing another test * Fixing linter errors * Fixing types errors * Fixing linter error * Fixing cypress tests * Fixing the last cypress test * Simpliying a bit the code * Making property insertion more robust * Updating checkbox test * Fixed review comments * Fixed server lint errors Co-authored-by: Miguel de la Cruz Co-authored-by: Scott Bishel Co-authored-by: Chen-I Lim <46905241+chenilim@users.noreply.github.com> Co-authored-by: Jesús Espino Co-authored-by: Doug Lauder Co-authored-by: Mattermod Co-authored-by: Ibrahim Serdar Acikgoz Co-authored-by: kamre --- mattermost-plugin/.golangci.yml | 1 - mattermost-plugin/server/plugin.go | 1 + server/.golangci.yml | 1 - server/services/store/sqlstore/board.go | 23 +- server/services/store/sqlstore/migrate.go | 215 +++++++++++++++++- .../000017_add_teams_and_boards.up.sql | 36 ++- server/services/store/sqlstore/params.go | 3 + server/services/store/sqlstore/sqlstore.go | 4 + server/utils/utils.go | 41 ++++ 9 files changed, 309 insertions(+), 16 deletions(-) diff --git a/mattermost-plugin/.golangci.yml b/mattermost-plugin/.golangci.yml index e8163d76be7..90dbb359679 100644 --- a/mattermost-plugin/.golangci.yml +++ b/mattermost-plugin/.golangci.yml @@ -67,7 +67,6 @@ linters: - unconvert - unused - whitespace - - gocyclo issues: exclude-rules: diff --git a/mattermost-plugin/server/plugin.go b/mattermost-plugin/server/plugin.go index 39febf0125d..e91c87ba1d2 100644 --- a/mattermost-plugin/server/plugin.go +++ b/mattermost-plugin/server/plugin.go @@ -99,6 +99,7 @@ func (p *Plugin) OnActivate() error { NewMutexFn: func(name string) (*cluster.Mutex, error) { return cluster.NewMutex(p.API, name) }, + PluginAPI: &p.API, } var db store.Store diff --git a/server/.golangci.yml b/server/.golangci.yml index c1381759f2e..b336a462a0b 100644 --- a/server/.golangci.yml +++ b/server/.golangci.yml @@ -65,4 +65,3 @@ linters: - unconvert - unused - whitespace - - gocyclo diff --git a/server/services/store/sqlstore/board.go b/server/services/store/sqlstore/board.go index 421fab52ca9..17e375a4bf3 100644 --- a/server/services/store/sqlstore/board.go +++ b/server/services/store/sqlstore/board.go @@ -213,14 +213,34 @@ func (s *SQLStore) getBoardsForUserAndTeam(db sq.BaseRunner, userID, teamID stri func (s *SQLStore) insertBoard(db sq.BaseRunner, board *model.Board, userID string) (*model.Board, error) { propertiesBytes, err := json.Marshal(board.Properties) if err != nil { + s.logger.Error( + "failed to marshal board.Properties", + mlog.String("board_id", board.ID), + mlog.String("board.Properties", fmt.Sprintf("%v", board.Properties)), + mlog.Err(err), + ) return nil, err } + cardPropertiesBytes, err := json.Marshal(board.CardProperties) if err != nil { + s.logger.Error( + "failed to marshal board.CardProperties", + mlog.String("board_id", board.ID), + mlog.String("board.CardProperties", fmt.Sprintf("%v", board.CardProperties)), + mlog.Err(err), + ) return nil, err } + columnCalculationsBytes, err := json.Marshal(board.ColumnCalculations) if err != nil { + s.logger.Error( + "failed to marshal board.ColumnCalculations", + mlog.String("board_id", board.ID), + mlog.String("board.ColumnCalculations", fmt.Sprintf("%v", board.ColumnCalculations)), + mlog.Err(err), + ) return nil, err } @@ -290,6 +310,7 @@ func (s *SQLStore) insertBoard(db sq.BaseRunner, board *model.Board, userID stri // writing board history query := insertQuery.SetMap(insertQueryValues).Into(s.tablePrefix + "boards_history") if _, err := query.Exec(); err != nil { + s.logger.Error("failed to insert board history", mlog.String("board_id", board.ID), mlog.Err(err)) return nil, err } @@ -395,7 +416,7 @@ func (s *SQLStore) saveMember(db sq.BaseRunner, bm *model.BoardMember) (*model.B } else { query = query.Suffix( `ON CONFLICT (board_id, user_id) - DO UPDATE SET scheme_admin = EXCLUDED.scheme_admin, scheme_editor = EXCLUDED.scheme_editor, + DO UPDATE SET scheme_admin = EXCLUDED.scheme_admin, scheme_editor = EXCLUDED.scheme_editor, scheme_commenter = EXCLUDED.scheme_commenter, scheme_viewer = EXCLUDED.scheme_viewer`, ) } diff --git a/server/services/store/sqlstore/migrate.go b/server/services/store/sqlstore/migrate.go index 42bb7390d1e..63b681403da 100644 --- a/server/services/store/sqlstore/migrate.go +++ b/server/services/store/sqlstore/migrate.go @@ -5,8 +5,13 @@ import ( "context" "database/sql" "embed" + "errors" "fmt" + + "github.com/mattermost/focalboard/server/utils" + "path/filepath" + "strconv" "text/template" "github.com/mattermost/morph/models" @@ -33,11 +38,16 @@ import ( var assets embed.FS const ( - uniqueIDsMigrationRequiredVersion = 14 + uniqueIDsMigrationRequiredVersion = 14 + teamsAndBoardsMigrationRequiredVersion = 17 + + teamLessBoardsMigrationKey = "TeamLessBoardsMigrationComplete" tempSchemaMigrationTableName = "temp_schema_migration" ) +var errChannelCreatorNotInTeam = errors.New("channel creator not found in user teams") + func appendMultipleStatementsFlag(connectionString string) (string, error) { config, err := mysqldriver.ParseDSN(connectionString) if err != nil { @@ -219,6 +229,23 @@ func (s *SQLStore) Migrate() error { } if err := s.deleteOldSchemaMigrationTable(); err != nil { + if s.isPlugin { + mutex.Unlock() + } + return err + } + + if err := ensureMigrationsAppliedUpToVersion(engine, driver, teamsAndBoardsMigrationRequiredVersion); err != nil { + if s.isPlugin { + mutex.Unlock() + } + return err + } + + if err := s.migrateTeamLessBoards(); err != nil { + if s.isPlugin { + mutex.Unlock() + } return err } @@ -441,6 +468,192 @@ func (s *SQLStore) deleteOldSchemaMigrationTable() error { return nil } +// We no longer support boards existing in DMs and private +// group messages. This function migrates all boards +// belonging to a DM to the best possible team. +func (s *SQLStore) migrateTeamLessBoards() error { + if !s.isPlugin { + return nil + } + + setting, err := s.GetSystemSetting(teamLessBoardsMigrationKey) + if err != nil { + return fmt.Errorf("cannot get teamless boards migration state: %w", err) + } + + // If the migration is already completed, do not run it again. + if hasAlreadyRun, _ := strconv.ParseBool(setting); hasAlreadyRun { + return nil + } + + boards, err := s.getDMBoards(s.db) + if err != nil { + return err + } + + s.logger.Info(fmt.Sprintf("Migrating %d teamless boards to a team", len(boards))) + + // cache for best suitable team for a DM. Since a DM can + // contain multiple boards, caching this avoids + // duplicate queries for the same DM. + channelToTeamCache := map[string]string{} + + tx, err := s.db.BeginTx(context.Background(), nil) + if err != nil { + s.logger.Error("error starting transaction in migrateTeamLessBoards", mlog.Err(err)) + return err + } + + for i := range boards { + // check the cache first + teamID, ok := channelToTeamCache[boards[i].ChannelID] + + // query DB if entry not found in cache + if !ok { + teamID, err = s.getBestTeamForBoard(s.db, boards[i]) + if err != nil { + // don't let one board's error spoil + // the mood for others + continue + } + } + + channelToTeamCache[boards[i].ChannelID] = teamID + boards[i].TeamID = teamID + + query := s.getQueryBuilder(tx). + Update(s.tablePrefix+"boards"). + Set("team_id", teamID). + Set("type", model.BoardTypePrivate). + Where(sq.Eq{"id": boards[i].ID}) + + if _, err := query.Exec(); err != nil { + s.logger.Error("failed to set team id for board", mlog.String("board_id", boards[i].ID), mlog.String("team_id", teamID), mlog.Err(err)) + return err + } + } + + if err := s.setSystemSetting(tx, teamLessBoardsMigrationKey, strconv.FormatBool(true)); err != nil { + if rollbackErr := tx.Rollback(); rollbackErr != nil { + s.logger.Error("transaction rollback error", mlog.Err(rollbackErr), mlog.String("methodName", "migrateTeamLessBoards")) + } + return fmt.Errorf("cannot mark migration as completed: %w", err) + } + + if err := tx.Commit(); err != nil { + s.logger.Error("failed to commit migrateTeamLessBoards transaction", mlog.Err(err)) + return err + } + + return nil +} + +func (s *SQLStore) getDMBoards(tx sq.BaseRunner) ([]*model.Board, error) { + conditions := sq.And{ + sq.Eq{"team_id": ""}, + sq.Or{ + sq.Eq{"type": "D"}, + sq.Eq{"type": "G"}, + }, + } + + return s.getBoardsByCondition(tx, conditions) +} + +// The destination is selected as the first team where all members +// of the DM are a part of. If no such team exists, +// we use the first team to which DM creator belongs to. +func (s *SQLStore) getBestTeamForBoard(tx sq.BaseRunner, board *model.Board) (string, error) { + userTeams, err := s.getBoardUserTeams(tx, board) + if err != nil { + return "", err + } + + teams := [][]interface{}{} + for _, userTeam := range userTeams { + userTeamInterfaces := make([]interface{}, len(userTeam)) + for i := range userTeam { + userTeamInterfaces[i] = userTeam[i] + } + teams = append(teams, userTeamInterfaces) + } + + commonTeams := utils.Intersection(teams...) + var teamID string + if len(commonTeams) > 0 { + teamID = commonTeams[0].(string) + } else { + // no common teams found. Let's try finding the best suitable team + if board.Type == "D" { + // get DM's creator and pick one of their team + channel, appErr := (*s.pluginAPI).GetChannel(board.ChannelID) + if appErr != nil { + s.logger.Error("failed to fetch DM channel for board", mlog.String("board_id", board.ID), mlog.String("channel_id", board.ChannelID), mlog.Err(appErr)) + return "", appErr + } + + if _, ok := userTeams[channel.CreatorId]; !ok { + err := fmt.Errorf("%w board_id: %s, channel_id: %s, creator_id: %s", errChannelCreatorNotInTeam, board.ID, board.ChannelID, channel.CreatorId) + s.logger.Error(err.Error()) + return "", err + } + + teamID = userTeams[channel.CreatorId][0] + } else if board.Type == "G" { + // pick the team that has the most users as members + teamFrequency := map[string]int{} + highestFrequencyTeam := "" + highestFrequencyTeamFrequency := -1 + + for _, teams := range userTeams { + for _, teamID := range teams { + teamFrequency[teamID]++ + + if teamFrequency[teamID] > highestFrequencyTeamFrequency { + highestFrequencyTeamFrequency = teamFrequency[teamID] + highestFrequencyTeam = teamID + } + } + } + + teamID = highestFrequencyTeam + } + } + + return teamID, nil +} + +func (s *SQLStore) getBoardUserTeams(tx sq.BaseRunner, board *model.Board) (map[string][]string, error) { + query := s.getQueryBuilder(tx). + Select("teammembers.userid", "teammembers.teamid"). + From("channelmembers"). + Join("teammembers ON channelmembers.userid = teammembers.userid"). + Where(sq.Eq{"channelid": board.ChannelID}) + + rows, err := query.Query() + if err != nil { + s.logger.Error("failed to fetch user teams for board", mlog.String("boardID", board.ID), mlog.String("channelID", board.ChannelID), mlog.Err(err)) + return nil, err + } + + defer rows.Close() + + userTeams := map[string][]string{} + + for rows.Next() { + var userID, teamID string + err := rows.Scan(&userID, &teamID) + if err != nil { + s.logger.Error("getBoardUserTeams failed to scan SQL query result", mlog.String("boardID", board.ID), mlog.String("channelID", board.ChannelID), mlog.Err(err)) + return nil, err + } + + userTeams[userID] = append(userTeams[userID], teamID) + } + + return userTeams, nil +} + func ensureMigrationsAppliedUpToVersion(engine *morph.Morph, driver drivers.Driver, version int) error { applied, err := driver.AppliedMigrations() if err != nil { diff --git a/server/services/store/sqlstore/migrations/000017_add_teams_and_boards.up.sql b/server/services/store/sqlstore/migrations/000017_add_teams_and_boards.up.sql index 5b31f86c291..ffd155a58bd 100644 --- a/server/services/store/sqlstore/migrations/000017_add_teams_and_boards.up.sql +++ b/server/services/store/sqlstore/migrations/000017_add_teams_and_boards.up.sql @@ -108,8 +108,10 @@ CREATE TABLE {{.prefix}}boards_history ( {{if .postgres}} INSERT INTO {{.prefix}}boards ( SELECT B.id, B.insert_at, C.TeamId, B.channel_id, B.created_by, B.modified_by, C.type, B.title, (B.fields->>'description')::text, - B.fields->>'icon', (B.fields->'showDescription')::text::boolean, (B.fields->'isTemplate')::text::boolean, - COALESCE((B.fields->'templateVer')::text, '0')::int, + B.fields->>'icon', + COALESCE((fields->'showDescription')::text::boolean, false), + COALESCE((fields->'isTemplate')::text::boolean, false), + COALESCE((B.fields->'templateVer')::text::int, 0), '{}', B.fields->'cardProperties', B.fields->'columnCalculations', B.create_at, B.update_at, B.delete_at FROM {{.prefix}}blocks AS B @@ -118,8 +120,10 @@ CREATE TABLE {{.prefix}}boards_history ( ); INSERT INTO {{.prefix}}boards_history ( SELECT B.id, B.insert_at, C.TeamId, B.channel_id, B.created_by, B.modified_by, C.type, B.title, (B.fields->>'description')::text, - B.fields->>'icon', (B.fields->'showDescription')::text::boolean, (B.fields->'isTemplate')::text::boolean, - COALESCE((B.fields->'templateVer')::text, '0')::int, + B.fields->>'icon', + COALESCE((fields->'showDescription')::text::boolean, false), + COALESCE((fields->'isTemplate')::text::boolean, false), + COALESCE((B.fields->'templateVer')::text::int, 0), '{}', B.fields->'cardProperties', B.fields->'columnCalculations', B.create_at, B.update_at, B.delete_at FROM {{.prefix}}blocks_history AS B @@ -157,8 +161,10 @@ CREATE TABLE {{.prefix}}boards_history ( {{if .postgres}} INSERT INTO {{.prefix}}boards ( SELECT id, insert_at, '0', channel_id, created_by, modified_by, 'O', title, (fields->>'description')::text, - B.fields->>'icon', (fields->'showDescription')::text::boolean, (fields->'isTemplate')::text::boolean, - (B.fields->'templateVer')::text::int, + B.fields->>'icon', + COALESCE((fields->'showDescription')::text::boolean, false), + COALESCE((fields->'isTemplate')::text::boolean, false), + COALESCE((B.fields->'templateVer')::text::int, 0), '{}', fields->'cardProperties', fields->'columnCalculations', create_at, update_at, delete_at FROM {{.prefix}}blocks AS B @@ -166,8 +172,10 @@ CREATE TABLE {{.prefix}}boards_history ( ); INSERT INTO {{.prefix}}boards_history ( SELECT id, insert_at, '0', channel_id, created_by, modified_by, 'O', title, (fields->>'description')::text, - B.fields->>'icon', (fields->'showDescription')::text::boolean, (fields->'isTemplate')::text::boolean, - (B.fields->'templateVer')::text::int, + B.fields->>'icon', + COALESCE((fields->'showDescription')::text::boolean, false), + COALESCE((fields->'isTemplate')::text::boolean, false), + COALESCE((B.fields->'templateVer')::text::int, 0), '{}', fields->'cardProperties', fields->'columnCalculations', create_at, update_at, delete_at FROM {{.prefix}}blocks_history AS B @@ -177,8 +185,10 @@ CREATE TABLE {{.prefix}}boards_history ( {{if .mysql}} INSERT INTO {{.prefix}}boards ( SELECT id, insert_at, '0', channel_id, created_by, modified_by, 'O', title, JSON_UNQUOTE(JSON_EXTRACT(fields,'$.description')), - JSON_UNQUOTE(JSON_EXTRACT(fields,'$.icon')), fields->'$.showDescription', fields->'$.isTemplate', - B.fields->'$.templateVer', + JSON_UNQUOTE(JSON_EXTRACT(fields,'$.icon')), + COALESCE(B.fields->'$.showDescription', 'false') = 'true', + COALESCE(JSON_EXTRACT(B.fields, '$.isTemplate'), 'false') = 'true', + COALESCE(B.fields->'$.templateVer', 0), '{}', fields->'$.cardProperties', fields->'$.columnCalculations', create_at, update_at, delete_at FROM {{.prefix}}blocks AS B @@ -186,8 +196,10 @@ CREATE TABLE {{.prefix}}boards_history ( ); INSERT INTO {{.prefix}}boards_history ( SELECT id, insert_at, '0', channel_id, created_by, modified_by, 'O', title, JSON_UNQUOTE(JSON_EXTRACT(fields,'$.description')), - JSON_UNQUOTE(JSON_EXTRACT(fields,'$.icon')), fields->'$.showDescription', fields->'$.isTemplate', - B.fields->'$.templateVer', + JSON_UNQUOTE(JSON_EXTRACT(fields,'$.icon')), + COALESCE(B.fields->'$.showDescription', 'false') = 'true', + COALESCE(JSON_EXTRACT(B.fields, '$.isTemplate'), 'false') = 'true', + COALESCE(B.fields->'$.templateVer', 0), '{}', fields->'$.cardProperties', fields->'$.columnCalculations', create_at, update_at, delete_at FROM {{.prefix}}blocks_history AS B diff --git a/server/services/store/sqlstore/params.go b/server/services/store/sqlstore/params.go index a6992f6422a..1a2590a08c6 100644 --- a/server/services/store/sqlstore/params.go +++ b/server/services/store/sqlstore/params.go @@ -4,6 +4,8 @@ import ( "database/sql" "fmt" + "github.com/mattermost/mattermost-server/v6/plugin" + "github.com/mattermost/mattermost-server/v6/shared/mlog" ) @@ -15,6 +17,7 @@ type Params struct { DB *sql.DB IsPlugin bool NewMutexFn MutexFactory + PluginAPI *plugin.API } func (p Params) CheckValid() error { diff --git a/server/services/store/sqlstore/sqlstore.go b/server/services/store/sqlstore/sqlstore.go index 29751468357..6de84bff5e9 100644 --- a/server/services/store/sqlstore/sqlstore.go +++ b/server/services/store/sqlstore/sqlstore.go @@ -3,6 +3,8 @@ package sqlstore import ( "database/sql" + "github.com/mattermost/mattermost-server/v6/plugin" + sq "github.com/Masterminds/squirrel" "github.com/mattermost/focalboard/server/model" @@ -20,6 +22,7 @@ type SQLStore struct { isPlugin bool logger *mlog.Logger NewMutexFn MutexFactory + pluginAPI *plugin.API } // MutexFactory is used by the store in plugin mode to generate @@ -42,6 +45,7 @@ func New(params Params) (*SQLStore, error) { logger: params.Logger, isPlugin: params.IsPlugin, NewMutexFn: params.NewMutexFn, + pluginAPI: params.PluginAPI, } err := store.Migrate() diff --git a/server/utils/utils.go b/server/utils/utils.go index e0fe3d0af55..24329126b58 100644 --- a/server/utils/utils.go +++ b/server/utils/utils.go @@ -2,6 +2,7 @@ package utils import ( "encoding/json" + "reflect" "time" mm_model "github.com/mattermost/mattermost-server/v6/model" @@ -54,3 +55,43 @@ func StructToMap(v interface{}) (m map[string]interface{}) { _ = json.Unmarshal(b, &m) return } + +func intersection(a []interface{}, b []interface{}) []interface{} { + set := make([]interface{}, 0) + hash := make(map[interface{}]bool) + av := reflect.ValueOf(a) + bv := reflect.ValueOf(b) + + for i := 0; i < av.Len(); i++ { + el := av.Index(i).Interface() + hash[el] = true + } + + for i := 0; i < bv.Len(); i++ { + el := bv.Index(i).Interface() + if _, found := hash[el]; found { + set = append(set, el) + } + } + + return set +} + +func Intersection(x ...[]interface{}) []interface{} { + if len(x) == 0 { + return nil + } + + if len(x) == 1 { + return x[0] + } + + result := x[0] + i := 1 + for i < len(x) { + result = intersection(result, x[i]) + i++ + } + + return result +}