mirror of
https://github.com/mattermost/focalboard.git
synced 2025-02-01 19:14:35 +02:00
Load default templates on server initialization
This commit is contained in:
parent
6f40b80d6e
commit
0900546b68
@ -19,6 +19,13 @@ type Block struct {
|
||||
DeleteAt int64 `json:"deleteAt"`
|
||||
}
|
||||
|
||||
// Archive is an import / export archive
|
||||
type Archive struct {
|
||||
Version int64 `json:"version"`
|
||||
Date int64 `json:"date"`
|
||||
Blocks []Block `json:"blocks"`
|
||||
}
|
||||
|
||||
func BlocksFromJSON(data io.Reader) []Block {
|
||||
var blocks []Block
|
||||
json.NewDecoder(data).Decode(&blocks)
|
||||
|
233
server/services/store/sqlstore/initializations/bindata.go
Normal file
233
server/services/store/sqlstore/initializations/bindata.go
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,2 @@
|
||||
//go:generate go-bindata -prefix templates/ -pkg initializations -o bindata.go ./templates
|
||||
package initializations
|
File diff suppressed because one or more lines are too long
63
server/services/store/sqlstore/initialize.go
Normal file
63
server/services/store/sqlstore/initialize.go
Normal file
@ -0,0 +1,63 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"github.com/mattermost/mattermost-octo-tasks/server/model"
|
||||
"github.com/mattermost/mattermost-octo-tasks/server/services/store/sqlstore/initializations"
|
||||
)
|
||||
|
||||
// InitializeTemplates imports default templates if the blocks table is empty
|
||||
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 {
|
||||
log.Printf("importInitialTemplates")
|
||||
blocksJSON := initializations.MustAsset("templates.json")
|
||||
|
||||
var archive model.Archive
|
||||
err := json.Unmarshal(blocksJSON, &archive)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("Inserting %d blocks", len(archive.Blocks))
|
||||
for _, block := range archive.Blocks {
|
||||
// log.Printf("\t%v %v %v", block.ID, block.Type, block.Title)
|
||||
err := s.InsertBlock(block)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// isInitializationNeeded returns true if the blocks table is empty
|
||||
func (s *SQLStore) isInitializationNeeded() (bool, error) {
|
||||
query := s.getQueryBuilder().
|
||||
Select("count(*)").
|
||||
From("blocks")
|
||||
|
||||
row := query.QueryRow()
|
||||
|
||||
var count int
|
||||
err := row.Scan(&count)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return false, err
|
||||
}
|
||||
|
||||
return (count == 0), nil
|
||||
}
|
@ -44,6 +44,13 @@ func New(dbType, connectionString string) (*SQLStore, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = store.InitializeTemplates()
|
||||
if err != nil {
|
||||
log.Printf(`InitializeTemplates failed: %v`, err)
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return store, nil
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user