Skip to content

Commit

Permalink
Fixed a bug where migrations didn't run on a clean DB (#3104)
Browse files Browse the repository at this point in the history
  • Loading branch information
harshilsharma63 committed May 24, 2022
1 parent 3f49be6 commit d6e494c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
6 changes: 5 additions & 1 deletion server/services/store/sqlstore/board.go
Expand Up @@ -216,8 +216,12 @@ func (s *SQLStore) getBoardByCondition(db sq.BaseRunner, conditions ...interface
}

func (s *SQLStore) getBoardsByCondition(db sq.BaseRunner, conditions ...interface{}) ([]*model.Board, error) {
return s.getBoardsFieldsByCondition(db, boardFields(""), conditions...)
}

func (s *SQLStore) getBoardsFieldsByCondition(db sq.BaseRunner, fields []string, conditions ...interface{}) ([]*model.Board, error) {
query := s.getQueryBuilder(db).
Select(boardFields("")...).
Select(fields...).
From(s.tablePrefix + "boards")
for _, c := range conditions {
query = query.Where(c)
Expand Down
2 changes: 1 addition & 1 deletion server/services/store/sqlstore/data_migrations.go
Expand Up @@ -386,7 +386,7 @@ func (s *SQLStore) getDMBoards(tx sq.BaseRunner) ([]*model.Board, error) {
},
}

boards, err := s.getBoardsByCondition(tx, conditions)
boards, err := s.getLegacyBoardsByCondition(tx, conditions)
if err != nil && model.IsErrNotFound(err) {
return []*model.Board{}, nil
}
Expand Down
45 changes: 45 additions & 0 deletions server/services/store/sqlstore/legacy_blocks.go
Expand Up @@ -3,6 +3,7 @@ package sqlstore
import (
"database/sql"
"encoding/json"
"strings"

"github.com/mattermost/focalboard/server/utils"

Expand All @@ -12,6 +13,46 @@ import (
"github.com/mattermost/mattermost-server/v6/shared/mlog"
)

func legacyBoardFields(prefix string) []string {
// substitute new columns with `"\"\""` (empty string) so as to allow
// row scan to continue to work with new models.

fields := []string{
"id",
"team_id",
"COALESCE(channel_id, '')",
"COALESCE(created_by, '')",
"modified_by",
"type",
"\"\"", // substitute for minimum_role column.
"title",
"description",
"icon",
"show_description",
"is_template",
"template_version",
"COALESCE(properties, '{}')",
"COALESCE(card_properties, '[]')",
"create_at",
"update_at",
"delete_at",
}

if prefix == "" {
return fields
}

prefixedFields := make([]string, len(fields))
for i, field := range fields {
if strings.HasPrefix(field, "COALESCE(") {
prefixedFields[i] = strings.Replace(field, "COALESCE(", "COALESCE("+prefix, 1)
} else {
prefixedFields[i] = prefix + field
}
}
return prefixedFields
}

// legacyBlocksFromRows is the old getBlock version that still uses
// the old block model. This method is kept to enable the unique IDs
// data migration.
Expand Down Expand Up @@ -206,3 +247,7 @@ func (s *SQLStore) insertLegacyBlock(db sq.BaseRunner, workspaceID string, block

return nil
}

func (s *SQLStore) getLegacyBoardsByCondition(db sq.BaseRunner, conditions ...interface{}) ([]*model.Board, error) {
return s.getBoardsFieldsByCondition(db, legacyBoardFields(""), conditions...)
}

0 comments on commit d6e494c

Please sign in to comment.