Skip to content

Commit

Permalink
refactor: removed standalone sql part of code
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajat-Dabade committed Jul 2, 2024
1 parent 99d7b81 commit c18a26c
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 859 deletions.
3 changes: 1 addition & 2 deletions server/app/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ func SetupTestHelper(t *testing.T) (*TestHelper, func()) {
filesBackend := &mocks.FileBackend{}
auth := auth.New(&cfg, store, nil)
logger, _ := mlog.NewLogger()
sessionToken := "TESTTOKEN"
wsserver := ws.NewServer(auth, sessionToken, false, logger, store)
wsserver := ws.NewServer(auth, logger, store)
webhook := webhook.NewClient(&cfg, logger)
metricsService := metrics.NewMetrics(metrics.InstanceInfo{})

Expand Down
3 changes: 1 addition & 2 deletions server/server/initHandlers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package server

func (s *Server) initHandlers() {
cfg := s.config
s.api.MattermostAuth = cfg.AuthMode == MattermostAuthMod
s.api.MattermostAuth = true
}
19 changes: 1 addition & 18 deletions server/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ import (
const (
cleanupSessionTaskFrequency = 10 * time.Minute
updateMetricsTaskFrequency = 15 * time.Minute

minSessionExpiryTime = int64(60 * 60 * 24 * 31) // 31 days

MattermostAuthMod = "mattermost"
)

type Server struct {
Expand Down Expand Up @@ -78,7 +74,7 @@ func New(params Params) (*Server, error) {
// if no ws adapter is provided, we spin up a websocket server
wsAdapter := params.WSAdapter
if wsAdapter == nil {
wsAdapter = ws.NewServer(authenticator, params.SingleUserToken, params.Cfg.AuthMode == MattermostAuthMod, params.Logger, params.DBStore)
wsAdapter = ws.NewServer(authenticator, params.Logger, params.DBStore)
}

filesBackendSettings := filestore.FileBackendSettings{}
Expand Down Expand Up @@ -253,19 +249,6 @@ func (s *Server) Start() error {
}
}

if s.config.AuthMode != MattermostAuthMod {
s.cleanUpSessionsTask = scheduler.CreateRecurringTask("cleanUpSessions", func() {
secondsAgo := minSessionExpiryTime
if secondsAgo < s.config.SessionExpireTime {
secondsAgo = s.config.SessionExpireTime
}

if err := s.store.CleanUpSessions(secondsAgo); err != nil {
s.logger.Error("Unable to clean up the sessions", mlog.Err(err))
}
}, cleanupSessionTaskFrequency)
}

metricsUpdater := func() {
blockCounts, err := s.store.GetBlockCountsByType()
if err != nil {
Expand Down
136 changes: 0 additions & 136 deletions server/services/store/sqlstore/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,36 +234,6 @@ func (s *SQLStore) getBoard(db sq.BaseRunner, boardID string) (*model.Board, err
return s.getBoardByCondition(db, sq.Eq{"id": boardID})
}

func (s *SQLStore) getBoardsForUserAndTeam(db sq.BaseRunner, userID, teamID string, includePublicBoards bool) ([]*model.Board, error) {
query := s.getQueryBuilder(db).
Select(boardFields("b.")...).
Distinct().
From(s.tablePrefix + "boards as b").
LeftJoin(s.tablePrefix + "board_members as bm on b.id=bm.board_id").
Where(sq.Eq{"b.team_id": teamID}).
Where(sq.Eq{"b.is_template": false})

if includePublicBoards {
query = query.Where(sq.Or{
sq.Eq{"b.type": model.BoardTypeOpen},
sq.Eq{"bm.user_id": userID},
})
} else {
query = query.Where(sq.Or{
sq.Eq{"bm.user_id": userID},
})
}

rows, err := query.Query()
if err != nil {
s.logger.Error(`getBoardsForUserAndTeam ERROR`, mlog.Err(err))
return nil, err
}
defer s.CloseRows(rows)

return s.boardsFromRows(rows)
}

func (s *SQLStore) getBoardsInTeamByIds(db sq.BaseRunner, boardIDs []string, teamID string) ([]*model.Board, error) {
query := s.getQueryBuilder(db).
Select(boardFields("b.")...).
Expand Down Expand Up @@ -646,112 +616,6 @@ func (s *SQLStore) getMembersForBoard(db sq.BaseRunner, boardID string) ([]*mode
return s.boardMembersFromRows(rows)
}

// searchBoardsForUser returns all boards that match with the
// term that are either private and which the user is a member of, or
// they're open, regardless of the user membership.
// Search is case-insensitive.
func (s *SQLStore) searchBoardsForUser(db sq.BaseRunner, term string, searchField model.BoardSearchField, userID string, includePublicBoards bool) ([]*model.Board, error) {
query := s.getQueryBuilder(db).
Select(boardFields("b.")...).
Distinct().
From(s.tablePrefix + "boards as b").
LeftJoin(s.tablePrefix + "board_members as bm on b.id=bm.board_id").
Where(sq.Eq{"b.is_template": false})

if includePublicBoards {
query = query.Where(sq.Or{
sq.Eq{"b.type": model.BoardTypeOpen},
sq.Eq{"bm.user_id": userID},
})
} else {
query = query.Where(sq.Or{
sq.Eq{"bm.user_id": userID},
})
}

if term != "" {
if searchField == model.BoardSearchFieldPropertyName {
switch s.dbType {
case model.PostgresDBType:
where := "b.properties->? is not null"
query = query.Where(where, term)
case model.MysqlDBType, model.SqliteDBType:
where := "JSON_EXTRACT(b.properties, ?) IS NOT NULL"
query = query.Where(where, "$."+term)
default:
where := "b.properties LIKE ?"
query = query.Where(where, "%\""+term+"\"%")
}
} else { // model.BoardSearchFieldTitle
// break search query into space separated words
// and search for all words.
// This should later be upgraded to industrial-strength
// word tokenizer, that uses much more than space
// to break words.
conditions := sq.And{}
for _, word := range strings.Split(strings.TrimSpace(term), " ") {
conditions = append(conditions, sq.Like{"lower(b.title)": "%" + strings.ToLower(word) + "%"})
}
query = query.Where(conditions)
}
}

rows, err := query.Query()
if err != nil {
s.logger.Error(`searchBoardsForUser ERROR`, mlog.Err(err))
return nil, err
}
defer s.CloseRows(rows)

return s.boardsFromRows(rows)
}

// searchBoardsForUserInTeam returns all boards that match with the
// term that are either private and which the user is a member of, or
// they're open, regardless of the user membership.
// Search is case-insensitive.
func (s *SQLStore) searchBoardsForUserInTeam(db sq.BaseRunner, teamID, term, userID string) ([]*model.Board, error) {
query := s.getQueryBuilder(db).
Select(boardFields("b.")...).
Distinct().
From(s.tablePrefix + "boards as b").
LeftJoin(s.tablePrefix + "board_members as bm on b.id=bm.board_id").
Where(sq.Eq{"b.is_template": false}).
Where(sq.Eq{"b.team_id": teamID}).
Where(sq.Or{
sq.Eq{"b.type": model.BoardTypeOpen},
sq.And{
sq.Eq{"b.type": model.BoardTypePrivate},
sq.Eq{"bm.user_id": userID},
},
})

if term != "" {
// break search query into space separated words
// and search for all words.
// This should later be upgraded to industrial-strength
// word tokenizer, that uses much more than space
// to break words.

conditions := sq.And{}

for _, word := range strings.Split(strings.TrimSpace(term), " ") {
conditions = append(conditions, sq.Like{"lower(b.title)": "%" + strings.ToLower(word) + "%"})
}

query = query.Where(conditions)
}

rows, err := query.Query()
if err != nil {
s.logger.Error(`searchBoardsForUser ERROR`, mlog.Err(err))
return nil, err
}
defer s.CloseRows(rows)

return s.boardsFromRows(rows)
}

func (s *SQLStore) getBoardHistory(db sq.BaseRunner, boardID string, opts model.QueryBoardHistoryOptions) ([]*model.Board, error) {
var order string
if opts.Descending {
Expand Down
92 changes: 0 additions & 92 deletions server/services/store/sqlstore/file.go

This file was deleted.

Loading

0 comments on commit c18a26c

Please sign in to comment.