2020-12-10 21:41:06 +01:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
2021-04-26 13:43:02 +02:00
|
|
|
sq "github.com/Masterminds/squirrel"
|
2021-01-26 23:13:46 +01:00
|
|
|
"github.com/mattermost/focalboard/server/model"
|
2021-03-26 19:01:54 +01:00
|
|
|
"github.com/mattermost/focalboard/server/services/store"
|
2021-01-26 23:13:46 +01:00
|
|
|
"github.com/mattermost/focalboard/server/services/store/sqlstore/initializations"
|
2021-08-25 22:08:01 +02:00
|
|
|
|
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
2020-12-10 21:41:06 +01:00
|
|
|
)
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// InitializeTemplates imports default templates if the blocks table is empty.
|
2020-12-10 21:41:06 +01:00
|
|
|
func (s *SQLStore) InitializeTemplates() error {
|
|
|
|
isNeeded, err := s.isInitializationNeeded()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if isNeeded {
|
|
|
|
return s.importInitialTemplates()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQLStore) importInitialTemplates() error {
|
2021-05-29 08:23:10 +02:00
|
|
|
s.logger.Debug("importInitialTemplates")
|
2020-12-10 21:41:06 +01:00
|
|
|
blocksJSON := initializations.MustAsset("templates.json")
|
|
|
|
|
|
|
|
var archive model.Archive
|
|
|
|
err := json.Unmarshal(blocksJSON, &archive)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-26 19:01:54 +01:00
|
|
|
globalContainer := store.Container{
|
2021-03-30 23:04:00 +02:00
|
|
|
WorkspaceID: "0",
|
2021-03-26 19:01:54 +01:00
|
|
|
}
|
|
|
|
|
2021-05-29 08:23:10 +02:00
|
|
|
s.logger.Debug("Inserting blocks", mlog.Int("block_count", len(archive.Blocks)))
|
2021-07-08 16:36:43 +02:00
|
|
|
for i := range archive.Blocks {
|
2021-05-29 08:23:10 +02:00
|
|
|
s.logger.Trace("insert block",
|
2021-07-08 16:36:43 +02:00
|
|
|
mlog.String("blockID", archive.Blocks[i].ID),
|
|
|
|
mlog.String("block_type", archive.Blocks[i].Type),
|
|
|
|
mlog.String("block_title", archive.Blocks[i].Title),
|
2021-05-29 08:23:10 +02:00
|
|
|
)
|
2021-07-08 16:36:43 +02:00
|
|
|
err := s.InsertBlock(globalContainer, &archive.Blocks[i], "system")
|
2020-12-10 21:41:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// isInitializationNeeded returns true if the blocks table is empty.
|
2020-12-10 21:41:06 +01:00
|
|
|
func (s *SQLStore) isInitializationNeeded() (bool, error) {
|
2021-10-22 12:48:53 +02:00
|
|
|
query := s.getQueryBuilder(s.db).
|
2020-12-10 21:41:06 +01:00
|
|
|
Select("count(*)").
|
2021-04-26 13:43:02 +02:00
|
|
|
From(s.tablePrefix + "blocks").
|
|
|
|
Where(sq.Eq{"COALESCE(workspace_id, '0')": "0"})
|
2020-12-10 21:41:06 +01:00
|
|
|
|
|
|
|
row := query.QueryRow()
|
|
|
|
|
|
|
|
var count int
|
|
|
|
err := row.Scan(&count)
|
|
|
|
if err != nil {
|
2021-07-09 03:09:02 +02:00
|
|
|
s.logger.Error("isInitializationNeeded", mlog.Err(err))
|
2020-12-10 21:41:06 +01:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return (count == 0), nil
|
|
|
|
}
|