1
0
mirror of https://github.com/mattermost/focalboard.git synced 2024-12-27 13:48:52 +02:00
focalboard/server/services/store/sqlstore/blocks.go

437 lines
8.8 KiB
Go
Raw Normal View History

2020-10-16 19:20:43 +02:00
package sqlstore
2020-10-08 18:21:27 +02:00
import (
"context"
2020-10-08 18:21:27 +02:00
"database/sql"
"encoding/json"
"errors"
2020-10-08 18:21:27 +02:00
"log"
"time"
2020-10-20 20:00:46 +02:00
sq "github.com/Masterminds/squirrel"
2020-10-08 18:21:27 +02:00
_ "github.com/lib/pq"
2021-01-27 00:13:46 +02:00
"github.com/mattermost/focalboard/server/model"
2021-03-26 20:01:54 +02:00
"github.com/mattermost/focalboard/server/services/store"
2020-10-08 18:21:27 +02:00
_ "github.com/mattn/go-sqlite3"
)
2021-03-26 20:01:54 +02:00
func (s *SQLStore) GetBlocksWithParentAndType(c store.Container, parentID string, blockType string) ([]model.Block, error) {
query := s.getQueryBuilder().
2021-01-12 04:53:08 +02:00
Select(
"id",
"parent_id",
"root_id",
"modified_by",
s.escapeField("schema"),
2021-01-12 04:53:08 +02:00
"type",
"title",
"COALESCE(fields, '{}')",
2021-01-12 04:53:08 +02:00
"create_at",
"update_at",
"delete_at",
).
From(s.tablePrefix + "blocks").
Where(sq.Eq{"COALESCE(workspace_id, '0')": c.WorkspaceID}).
2020-10-20 20:00:46 +02:00
Where(sq.Eq{"parent_id": parentID}).
Where(sq.Eq{"type": blockType})
2020-10-20 20:00:46 +02:00
rows, err := query.Query()
2020-10-08 18:21:27 +02:00
if err != nil {
log.Printf(`getBlocksWithParentAndType ERROR: %v`, err)
2020-10-16 16:21:42 +02:00
return nil, err
2020-10-08 18:21:27 +02:00
}
return blocksFromRows(rows)
}
2021-03-26 20:01:54 +02:00
func (s *SQLStore) GetBlocksWithParent(c store.Container, parentID string) ([]model.Block, error) {
query := s.getQueryBuilder().
2021-01-12 04:53:08 +02:00
Select(
"id",
"parent_id",
"root_id",
"modified_by",
s.escapeField("schema"),
2021-01-12 04:53:08 +02:00
"type",
"title",
"COALESCE(fields, '{}')",
2021-01-12 04:53:08 +02:00
"create_at",
"update_at",
"delete_at",
).
From(s.tablePrefix + "blocks").
Where(sq.Eq{"parent_id": parentID}).
Where(sq.Eq{"coalesce(workspace_id, '0')": c.WorkspaceID})
2020-10-08 18:21:27 +02:00
2020-10-20 20:00:46 +02:00
rows, err := query.Query()
2020-10-08 18:21:27 +02:00
if err != nil {
log.Printf(`getBlocksWithParent ERROR: %v`, err)
2020-10-16 16:21:42 +02:00
return nil, err
2020-10-08 18:21:27 +02:00
}
return blocksFromRows(rows)
}
func (s *SQLStore) GetBlocksWithRootID(c store.Container, rootID string) ([]model.Block, error) {
query := s.getQueryBuilder().
Select(
"id",
"parent_id",
"root_id",
"modified_by",
s.escapeField("schema"),
"type",
"title",
"COALESCE(fields, '{}')",
"create_at",
"update_at",
"delete_at",
).
From(s.tablePrefix + "blocks").
Where(sq.Eq{"root_id": rootID}).
Where(sq.Eq{"coalesce(workspace_id, '0')": c.WorkspaceID})
rows, err := query.Query()
if err != nil {
log.Printf(`GetBlocksWithRootID ERROR: %v`, err)
return nil, err
}
return blocksFromRows(rows)
}
2021-03-26 20:01:54 +02:00
func (s *SQLStore) GetBlocksWithType(c store.Container, blockType string) ([]model.Block, error) {
query := s.getQueryBuilder().
2021-01-12 04:53:08 +02:00
Select(
"id",
"parent_id",
"root_id",
"modified_by",
s.escapeField("schema"),
2021-01-12 04:53:08 +02:00
"type",
"title",
"COALESCE(fields, '{}')",
2021-01-12 04:53:08 +02:00
"create_at",
"update_at",
"delete_at",
).
From(s.tablePrefix + "blocks").
Where(sq.Eq{"type": blockType}).
Where(sq.Eq{"coalesce(workspace_id, '0')": c.WorkspaceID})
2020-10-20 20:00:46 +02:00
rows, err := query.Query()
2020-10-12 20:02:07 +02:00
if err != nil {
log.Printf(`getBlocksWithParentAndType ERROR: %v`, err)
2020-10-16 16:21:42 +02:00
return nil, err
2020-10-12 20:02:07 +02:00
}
return blocksFromRows(rows)
}
2020-11-12 20:16:59 +02:00
// GetSubTree2 returns blocks within 2 levels of the given blockID
2021-03-26 20:01:54 +02:00
func (s *SQLStore) GetSubTree2(c store.Container, blockID string) ([]model.Block, error) {
query := s.getQueryBuilder().
2021-01-12 04:53:08 +02:00
Select(
"id",
"parent_id",
"root_id",
"modified_by",
s.escapeField("schema"),
2021-01-12 04:53:08 +02:00
"type",
"title",
"COALESCE(fields, '{}')",
2021-01-12 04:53:08 +02:00
"create_at",
"update_at",
"delete_at",
).
From(s.tablePrefix + "blocks").
Where(sq.Or{sq.Eq{"id": blockID}, sq.Eq{"parent_id": blockID}}).
Where(sq.Eq{"coalesce(workspace_id, '0')": c.WorkspaceID})
2020-10-08 18:21:27 +02:00
2020-10-20 20:00:46 +02:00
rows, err := query.Query()
2020-10-08 18:21:27 +02:00
if err != nil {
log.Printf(`getSubTree ERROR: %v`, err)
2020-10-16 16:21:42 +02:00
return nil, err
2020-10-08 18:21:27 +02:00
}
return blocksFromRows(rows)
}
2020-11-12 20:16:59 +02:00
// GetSubTree3 returns blocks within 3 levels of the given blockID
2021-03-26 20:01:54 +02:00
func (s *SQLStore) GetSubTree3(c store.Container, blockID string) ([]model.Block, error) {
2020-11-12 20:16:59 +02:00
// This first subquery returns repeated blocks
query := s.getQueryBuilder().Select(
2021-01-12 04:53:08 +02:00
"l3.id",
"l3.parent_id",
"l3.root_id",
"l3.modified_by",
"l3."+s.escapeField("schema"),
2021-01-12 04:53:08 +02:00
"l3.type",
"l3.title",
"l3.fields",
"l3.create_at",
"l3.update_at",
"l3.delete_at",
).
From(s.tablePrefix + "blocks as l1").
Join(s.tablePrefix + "blocks as l2 on l2.parent_id = l1.id or l2.id = l1.id").
Join(s.tablePrefix + "blocks as l3 on l3.parent_id = l2.id or l3.id = l2.id").
Where(sq.Eq{"l1.id": blockID}).
Where(sq.Eq{"COALESCE(l3.workspace_id, '0')": c.WorkspaceID})
if s.dbType == postgresDBType {
query = query.Options("DISTINCT ON (l3.id)")
} else {
query = query.Distinct()
}
2020-11-12 20:16:59 +02:00
rows, err := query.Query()
if err != nil {
log.Printf(`getSubTree3 ERROR: %v`, err)
return nil, err
}
return blocksFromRows(rows)
}
2021-03-26 20:01:54 +02:00
func (s *SQLStore) GetAllBlocks(c store.Container) ([]model.Block, error) {
query := s.getQueryBuilder().
2021-01-12 04:53:08 +02:00
Select(
"id",
"parent_id",
"root_id",
"modified_by",
s.escapeField("schema"),
2021-01-12 04:53:08 +02:00
"type",
"title",
"COALESCE(fields, '{}')",
2021-01-12 04:53:08 +02:00
"create_at",
"update_at",
"delete_at",
).
From(s.tablePrefix + "blocks")
2020-10-08 18:21:27 +02:00
2020-10-20 20:00:46 +02:00
rows, err := query.Query()
2020-10-08 18:21:27 +02:00
if err != nil {
log.Printf(`getAllBlocks ERROR: %v`, err)
2020-10-16 16:21:42 +02:00
return nil, err
2020-10-08 18:21:27 +02:00
}
return blocksFromRows(rows)
}
func blocksFromRows(rows *sql.Rows) ([]model.Block, error) {
2020-10-08 18:21:27 +02:00
defer rows.Close()
2021-03-21 10:28:26 +02:00
results := []model.Block{}
2020-10-08 18:21:27 +02:00
for rows.Next() {
var block model.Block
var fieldsJSON string
2021-01-12 04:53:08 +02:00
var modifiedBy sql.NullString
err := rows.Scan(
&block.ID,
&block.ParentID,
&block.RootID,
2021-01-12 04:53:08 +02:00
&modifiedBy,
&block.Schema,
&block.Type,
&block.Title,
&fieldsJSON,
&block.CreateAt,
&block.UpdateAt,
&block.DeleteAt)
2020-10-08 18:21:27 +02:00
if err != nil {
// handle this error
log.Printf(`ERROR blocksFromRows: %v`, err)
2020-10-16 16:21:42 +02:00
return nil, err
2020-10-08 18:21:27 +02:00
}
2021-01-12 04:53:08 +02:00
if modifiedBy.Valid {
block.ModifiedBy = modifiedBy.String
}
err = json.Unmarshal([]byte(fieldsJSON), &block.Fields)
if err != nil {
// handle this error
log.Printf(`ERROR blocksFromRows fields: %v`, err)
2020-10-16 16:21:42 +02:00
return nil, err
}
results = append(results, block)
2020-10-08 18:21:27 +02:00
}
2020-10-16 16:21:42 +02:00
return results, nil
2020-10-08 18:21:27 +02:00
}
2021-03-26 20:01:54 +02:00
func (s *SQLStore) GetRootID(c store.Container, blockID string) (string, error) {
2021-01-13 04:49:08 +02:00
query := s.getQueryBuilder().Select("root_id").
From(s.tablePrefix + "blocks").
Where(sq.Eq{"id": blockID}).
Where(sq.Eq{"coalesce(workspace_id, '0')": c.WorkspaceID})
2021-01-13 04:49:08 +02:00
row := query.QueryRow()
var rootID string
err := row.Scan(&rootID)
if err != nil {
return "", err
}
return rootID, nil
}
2021-03-26 20:01:54 +02:00
func (s *SQLStore) GetParentID(c store.Container, blockID string) (string, error) {
2020-10-20 20:00:46 +02:00
query := s.getQueryBuilder().Select("parent_id").
From(s.tablePrefix + "blocks").
Where(sq.Eq{"id": blockID}).
Where(sq.Eq{"coalesce(workspace_id, '0')": c.WorkspaceID})
2020-10-08 18:21:27 +02:00
2020-10-20 20:00:46 +02:00
row := query.QueryRow()
2020-10-08 18:21:27 +02:00
var parentID string
2020-10-08 18:21:27 +02:00
err := row.Scan(&parentID)
if err != nil {
2020-10-16 16:21:42 +02:00
return "", err
2020-10-08 18:21:27 +02:00
}
2020-10-16 16:21:42 +02:00
return parentID, nil
2020-10-08 18:21:27 +02:00
}
2021-03-26 20:01:54 +02:00
func (s *SQLStore) InsertBlock(c store.Container, block model.Block) error {
if block.RootID == "" {
return errors.New("rootId is nil")
}
fieldsJSON, err := json.Marshal(block.Fields)
if err != nil {
2020-10-16 16:21:42 +02:00
return err
}
ctx := context.Background()
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
query := s.getQueryBuilder().Insert("").
2021-01-12 04:53:08 +02:00
Columns(
2021-03-26 20:01:54 +02:00
"workspace_id",
2021-01-12 04:53:08 +02:00
"id",
"parent_id",
"root_id",
"modified_by",
s.escapeField("schema"),
2021-01-12 04:53:08 +02:00
"type",
"title",
"fields",
"create_at",
"update_at",
"delete_at",
).Values(
2021-03-26 20:01:54 +02:00
c.WorkspaceID,
2021-01-12 04:53:08 +02:00
block.ID,
block.ParentID,
block.RootID,
block.ModifiedBy,
block.Schema,
block.Type,
block.Title,
fieldsJSON,
block.CreateAt,
block.UpdateAt,
block.DeleteAt,
)
// TODO: migrate this delete/insert to an upsert
2021-04-26 13:43:02 +02:00
deleteQuery := s.getQueryBuilder().
Delete(s.tablePrefix + "blocks").
Where(sq.Eq{"id": block.ID}).
Where(sq.Eq{"COALESCE(workspace_id, '0')": c.WorkspaceID})
_, err = sq.ExecContextWith(ctx, tx, deleteQuery)
if err != nil {
tx.Rollback()
return err
}
_, err = sq.ExecContextWith(ctx, tx, query.Into(s.tablePrefix+"blocks"))
if err != nil {
tx.Rollback()
return err
}
_, err = sq.ExecContextWith(ctx, tx, query.Into(s.tablePrefix+"blocks_history"))
if err != nil {
tx.Rollback()
return err
}
err = tx.Commit()
2020-10-08 18:21:27 +02:00
if err != nil {
2020-10-16 16:21:42 +02:00
return err
2020-10-08 18:21:27 +02:00
}
2020-10-16 16:21:42 +02:00
return nil
2020-10-08 18:21:27 +02:00
}
2021-03-26 20:01:54 +02:00
func (s *SQLStore) DeleteBlock(c store.Container, blockID string, modifiedBy string) error {
ctx := context.Background()
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
2020-10-08 18:21:27 +02:00
now := time.Now().Unix()
insertQuery := s.getQueryBuilder().Insert(s.tablePrefix+"blocks_history").
2021-01-12 21:16:25 +02:00
Columns(
2021-03-26 20:01:54 +02:00
"workspace_id",
2021-01-12 21:16:25 +02:00
"id",
"modified_by",
"update_at",
"delete_at",
).
Values(
2021-03-26 20:01:54 +02:00
c.WorkspaceID,
2021-01-12 21:16:25 +02:00
blockID,
modifiedBy,
now,
now,
)
_, err = sq.ExecContextWith(ctx, tx, insertQuery)
if err != nil {
tx.Rollback()
return err
}
2021-04-26 13:43:02 +02:00
deleteQuery := s.getQueryBuilder().
Delete(s.tablePrefix + "blocks").
Where(sq.Eq{"id": blockID}).
Where(sq.Eq{"COALESCE(workspace_id, '0')": c.WorkspaceID})
_, err = sq.ExecContextWith(ctx, tx, deleteQuery)
if err != nil {
tx.Rollback()
return err
}
err = tx.Commit()
2020-10-08 18:21:27 +02:00
if err != nil {
2020-10-16 16:21:42 +02:00
return err
2020-10-08 18:21:27 +02:00
}
2020-10-16 16:21:42 +02:00
return nil
2020-10-08 18:21:27 +02:00
}