2020-10-16 19:20:43 +02:00
|
|
|
package sqlstore
|
2020-10-08 18:21:27 +02:00
|
|
|
|
|
|
|
import (
|
2021-04-22 22:53:01 +02:00
|
|
|
"context"
|
2020-10-08 18:21:27 +02:00
|
|
|
"database/sql"
|
2020-10-15 23:56:15 +02:00
|
|
|
"encoding/json"
|
2021-08-06 14:10:24 +02:00
|
|
|
"fmt"
|
2020-10-08 18:21:27 +02:00
|
|
|
"time"
|
|
|
|
|
2021-07-09 03:09:02 +02:00
|
|
|
"github.com/mattermost/focalboard/server/utils"
|
|
|
|
|
2020-10-20 20:00:46 +02:00
|
|
|
sq "github.com/Masterminds/squirrel"
|
2021-07-09 03:09:02 +02:00
|
|
|
_ "github.com/lib/pq" // postgres driver
|
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"
|
2021-07-09 03:09:02 +02:00
|
|
|
_ "github.com/mattn/go-sqlite3" // sqlite driver
|
2021-08-25 22:08:01 +02:00
|
|
|
|
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
2020-10-08 18:21:27 +02:00
|
|
|
)
|
|
|
|
|
2021-07-09 03:09:02 +02:00
|
|
|
type RootIDNilError struct{}
|
|
|
|
|
|
|
|
func (re RootIDNilError) Error() string {
|
|
|
|
return "rootId is nil"
|
|
|
|
}
|
|
|
|
|
2021-08-06 14:10:24 +02:00
|
|
|
type BlockNotFoundErr struct {
|
|
|
|
blockID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (be BlockNotFoundErr) Error() string {
|
|
|
|
return fmt.Sprintf("block not found (block id: %s", be.blockID)
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
func (s *SQLStore) GetBlocksWithParentAndType(c store.Container, parentID string, blockType string) ([]model.Block, error) {
|
2020-10-22 13:34:42 +02:00
|
|
|
query := s.getQueryBuilder().
|
2021-01-12 04:53:08 +02:00
|
|
|
Select(
|
|
|
|
"id",
|
|
|
|
"parent_id",
|
|
|
|
"root_id",
|
2021-07-08 16:36:43 +02:00
|
|
|
"created_by",
|
2021-01-12 04:53:08 +02:00
|
|
|
"modified_by",
|
2021-04-22 22:53:01 +02:00
|
|
|
s.escapeField("schema"),
|
2021-01-12 04:53:08 +02:00
|
|
|
"type",
|
|
|
|
"title",
|
2021-04-22 22:53:01 +02:00
|
|
|
"COALESCE(fields, '{}')",
|
2021-01-12 04:53:08 +02:00
|
|
|
"create_at",
|
|
|
|
"update_at",
|
|
|
|
"delete_at",
|
|
|
|
).
|
2021-04-22 22:53:01 +02:00
|
|
|
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-22 15:22:36 +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 {
|
2021-05-29 08:23:10 +02:00
|
|
|
s.logger.Error(`getBlocksWithParentAndType ERROR`, mlog.Err(err))
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-16 16:21:42 +02:00
|
|
|
return nil, err
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
2021-07-06 22:44:11 +02:00
|
|
|
defer s.CloseRows(rows)
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2021-05-29 08:23:10 +02:00
|
|
|
return s.blocksFromRows(rows)
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
func (s *SQLStore) GetBlocksWithParent(c store.Container, parentID string) ([]model.Block, error) {
|
2020-10-22 13:34:42 +02:00
|
|
|
query := s.getQueryBuilder().
|
2021-01-12 04:53:08 +02:00
|
|
|
Select(
|
|
|
|
"id",
|
|
|
|
"parent_id",
|
|
|
|
"root_id",
|
2021-07-08 16:36:43 +02:00
|
|
|
"created_by",
|
2021-01-12 04:53:08 +02:00
|
|
|
"modified_by",
|
2021-04-22 22:53:01 +02:00
|
|
|
s.escapeField("schema"),
|
2021-01-12 04:53:08 +02:00
|
|
|
"type",
|
|
|
|
"title",
|
2021-04-22 22:53:01 +02:00
|
|
|
"COALESCE(fields, '{}')",
|
2021-01-12 04:53:08 +02:00
|
|
|
"create_at",
|
|
|
|
"update_at",
|
|
|
|
"delete_at",
|
|
|
|
).
|
2021-04-22 22:53:01 +02:00
|
|
|
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 {
|
2021-05-29 08:23:10 +02:00
|
|
|
s.logger.Error(`getBlocksWithParent ERROR`, mlog.Err(err))
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-16 16:21:42 +02:00
|
|
|
return nil, err
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
2021-07-06 22:44:11 +02:00
|
|
|
defer s.CloseRows(rows)
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2021-05-29 08:23:10 +02:00
|
|
|
return s.blocksFromRows(rows)
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
|
|
|
|
2021-05-13 23:04:49 +02:00
|
|
|
func (s *SQLStore) GetBlocksWithRootID(c store.Container, rootID string) ([]model.Block, error) {
|
|
|
|
query := s.getQueryBuilder().
|
|
|
|
Select(
|
|
|
|
"id",
|
|
|
|
"parent_id",
|
|
|
|
"root_id",
|
2021-07-08 16:36:43 +02:00
|
|
|
"created_by",
|
2021-05-13 23:04:49 +02:00
|
|
|
"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 {
|
2021-05-29 08:23:10 +02:00
|
|
|
s.logger.Error(`GetBlocksWithRootID ERROR`, mlog.Err(err))
|
2021-05-13 23:04:49 +02:00
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-06 22:44:11 +02:00
|
|
|
defer s.CloseRows(rows)
|
2021-05-13 23:04:49 +02:00
|
|
|
|
2021-05-29 08:23:10 +02:00
|
|
|
return s.blocksFromRows(rows)
|
2021-05-13 23:04:49 +02:00
|
|
|
}
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
func (s *SQLStore) GetBlocksWithType(c store.Container, blockType string) ([]model.Block, error) {
|
2020-10-22 13:34:42 +02:00
|
|
|
query := s.getQueryBuilder().
|
2021-01-12 04:53:08 +02:00
|
|
|
Select(
|
|
|
|
"id",
|
|
|
|
"parent_id",
|
|
|
|
"root_id",
|
2021-07-08 16:36:43 +02:00
|
|
|
"created_by",
|
2021-01-12 04:53:08 +02:00
|
|
|
"modified_by",
|
2021-04-22 22:53:01 +02:00
|
|
|
s.escapeField("schema"),
|
2021-01-12 04:53:08 +02:00
|
|
|
"type",
|
|
|
|
"title",
|
2021-04-22 22:53:01 +02:00
|
|
|
"COALESCE(fields, '{}')",
|
2021-01-12 04:53:08 +02:00
|
|
|
"create_at",
|
|
|
|
"update_at",
|
|
|
|
"delete_at",
|
|
|
|
).
|
2021-04-22 22:53:01 +02:00
|
|
|
From(s.tablePrefix + "blocks").
|
|
|
|
Where(sq.Eq{"type": blockType}).
|
|
|
|
Where(sq.Eq{"coalesce(workspace_id, '0')": c.WorkspaceID})
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-20 20:00:46 +02:00
|
|
|
rows, err := query.Query()
|
2020-10-12 20:02:07 +02:00
|
|
|
if err != nil {
|
2021-05-29 08:23:10 +02:00
|
|
|
s.logger.Error(`getBlocksWithParentAndType ERROR`, mlog.Err(err))
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-16 16:21:42 +02:00
|
|
|
return nil, err
|
2020-10-12 20:02:07 +02:00
|
|
|
}
|
2021-07-06 22:44:11 +02:00
|
|
|
defer s.CloseRows(rows)
|
2020-10-12 20:02:07 +02:00
|
|
|
|
2021-05-29 08:23:10 +02:00
|
|
|
return s.blocksFromRows(rows)
|
2020-10-12 20:02:07 +02:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +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) {
|
2020-10-22 13:34:42 +02:00
|
|
|
query := s.getQueryBuilder().
|
2021-01-12 04:53:08 +02:00
|
|
|
Select(
|
|
|
|
"id",
|
|
|
|
"parent_id",
|
|
|
|
"root_id",
|
2021-07-08 16:36:43 +02:00
|
|
|
"created_by",
|
2021-01-12 04:53:08 +02:00
|
|
|
"modified_by",
|
2021-04-22 22:53:01 +02:00
|
|
|
s.escapeField("schema"),
|
2021-01-12 04:53:08 +02:00
|
|
|
"type",
|
|
|
|
"title",
|
2021-04-22 22:53:01 +02:00
|
|
|
"COALESCE(fields, '{}')",
|
2021-01-12 04:53:08 +02:00
|
|
|
"create_at",
|
|
|
|
"update_at",
|
|
|
|
"delete_at",
|
|
|
|
).
|
2021-04-22 22:53:01 +02:00
|
|
|
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 {
|
2021-05-29 08:23:10 +02:00
|
|
|
s.logger.Error(`getSubTree ERROR`, mlog.Err(err))
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-16 16:21:42 +02:00
|
|
|
return nil, err
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
2021-07-06 22:44:11 +02:00
|
|
|
defer s.CloseRows(rows)
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2021-05-29 08:23:10 +02:00
|
|
|
return s.blocksFromRows(rows)
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +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
|
2021-04-22 22:53:01 +02:00
|
|
|
query := s.getQueryBuilder().Select(
|
2021-01-12 04:53:08 +02:00
|
|
|
"l3.id",
|
|
|
|
"l3.parent_id",
|
|
|
|
"l3.root_id",
|
2021-07-08 16:36:43 +02:00
|
|
|
"l3.created_by",
|
2021-01-12 04:53:08 +02:00
|
|
|
"l3.modified_by",
|
2021-04-22 22:53:01 +02:00
|
|
|
"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",
|
|
|
|
).
|
2021-04-22 22:53:01 +02:00
|
|
|
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 {
|
2021-05-29 08:23:10 +02:00
|
|
|
s.logger.Error(`getSubTree3 ERROR`, mlog.Err(err))
|
2020-11-12 20:16:59 +02:00
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-06 22:44:11 +02:00
|
|
|
defer s.CloseRows(rows)
|
2020-11-12 20:16:59 +02:00
|
|
|
|
2021-05-29 08:23:10 +02:00
|
|
|
return s.blocksFromRows(rows)
|
2020-11-12 20:16:59 +02:00
|
|
|
}
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
func (s *SQLStore) GetAllBlocks(c store.Container) ([]model.Block, error) {
|
2020-10-22 13:34:42 +02:00
|
|
|
query := s.getQueryBuilder().
|
2021-01-12 04:53:08 +02:00
|
|
|
Select(
|
|
|
|
"id",
|
|
|
|
"parent_id",
|
|
|
|
"root_id",
|
2021-07-08 16:36:43 +02:00
|
|
|
"created_by",
|
2021-01-12 04:53:08 +02:00
|
|
|
"modified_by",
|
2021-04-22 22:53:01 +02:00
|
|
|
s.escapeField("schema"),
|
2021-01-12 04:53:08 +02:00
|
|
|
"type",
|
|
|
|
"title",
|
2021-04-22 22:53:01 +02:00
|
|
|
"COALESCE(fields, '{}')",
|
2021-01-12 04:53:08 +02:00
|
|
|
"create_at",
|
|
|
|
"update_at",
|
|
|
|
"delete_at",
|
|
|
|
).
|
2021-05-24 18:59:30 +02:00
|
|
|
From(s.tablePrefix + "blocks").
|
|
|
|
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 {
|
2021-05-29 08:23:10 +02:00
|
|
|
s.logger.Error(`getAllBlocks ERROR`, mlog.Err(err))
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-16 16:21:42 +02:00
|
|
|
return nil, err
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
2021-07-06 22:44:11 +02:00
|
|
|
defer s.CloseRows(rows)
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2021-05-29 08:23:10 +02:00
|
|
|
return s.blocksFromRows(rows)
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
|
|
|
|
2021-05-29 08:23:10 +02:00
|
|
|
func (s *SQLStore) blocksFromRows(rows *sql.Rows) ([]model.Block, error) {
|
2021-03-21 10:28:26 +02:00
|
|
|
results := []model.Block{}
|
2020-10-08 18:21:27 +02:00
|
|
|
|
|
|
|
for rows.Next() {
|
2020-10-16 19:12:53 +02:00
|
|
|
var block model.Block
|
2020-10-15 23:56:15 +02:00
|
|
|
var fieldsJSON string
|
2021-01-12 04:53:08 +02:00
|
|
|
var modifiedBy sql.NullString
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-15 23:56:15 +02:00
|
|
|
err := rows.Scan(
|
|
|
|
&block.ID,
|
|
|
|
&block.ParentID,
|
2020-12-04 00:09:48 +02:00
|
|
|
&block.RootID,
|
2021-07-08 16:36:43 +02:00
|
|
|
&block.CreatedBy,
|
2021-01-12 04:53:08 +02:00
|
|
|
&modifiedBy,
|
2020-10-15 23:56:15 +02:00
|
|
|
&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
|
2021-05-29 08:23:10 +02:00
|
|
|
s.logger.Error(`ERROR blocksFromRows`, mlog.Err(err))
|
2020-10-22 15:22:36 +02:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-15 23:56:15 +02:00
|
|
|
err = json.Unmarshal([]byte(fieldsJSON), &block.Fields)
|
|
|
|
if err != nil {
|
|
|
|
// handle this error
|
2021-05-29 08:23:10 +02:00
|
|
|
s.logger.Error(`ERROR blocksFromRows fields`, mlog.Err(err))
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-16 16:21:42 +02:00
|
|
|
return nil, err
|
2020-10-15 23:56:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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").
|
2021-04-22 22:53:01 +02:00
|
|
|
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").
|
2021-04-22 22:53:01 +02:00
|
|
|
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-22 15:22:36 +02:00
|
|
|
|
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-07-08 16:36:43 +02:00
|
|
|
func (s *SQLStore) InsertBlock(c store.Container, block *model.Block, userID string) error {
|
2020-12-04 00:09:48 +02:00
|
|
|
if block.RootID == "" {
|
2021-07-09 03:09:02 +02:00
|
|
|
return RootIDNilError{}
|
2020-12-04 00:09:48 +02:00
|
|
|
}
|
|
|
|
|
2020-10-15 23:56:15 +02:00
|
|
|
fieldsJSON, err := json.Marshal(block.Fields)
|
|
|
|
if err != nil {
|
2020-10-16 16:21:42 +02:00
|
|
|
return err
|
2020-10-15 23:56:15 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 16:36:43 +02:00
|
|
|
existingBlock, err := s.GetBlock(c, block.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-22 22:53:01 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-08 16:36:43 +02:00
|
|
|
insertQuery := 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",
|
2021-07-08 16:36:43 +02:00
|
|
|
"created_by",
|
2021-01-12 04:53:08 +02:00
|
|
|
"modified_by",
|
2021-04-22 22:53:01 +02:00
|
|
|
s.escapeField("schema"),
|
2021-01-12 04:53:08 +02:00
|
|
|
"type",
|
|
|
|
"title",
|
|
|
|
"fields",
|
|
|
|
"create_at",
|
|
|
|
"update_at",
|
|
|
|
"delete_at",
|
2021-07-08 16:36:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
insertQueryValues := map[string]interface{}{
|
|
|
|
"workspace_id": c.WorkspaceID,
|
|
|
|
"id": block.ID,
|
|
|
|
"parent_id": block.ParentID,
|
|
|
|
"root_id": block.RootID,
|
|
|
|
s.escapeField("schema"): block.Schema,
|
|
|
|
"type": block.Type,
|
|
|
|
"title": block.Title,
|
|
|
|
"fields": fieldsJSON,
|
|
|
|
"delete_at": block.DeleteAt,
|
|
|
|
"created_by": block.CreatedBy,
|
|
|
|
"modified_by": block.ModifiedBy,
|
|
|
|
"create_at": block.CreateAt,
|
|
|
|
"update_at": block.UpdateAt,
|
2021-04-22 22:53:01 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 16:36:43 +02:00
|
|
|
block.UpdateAt = utils.GetMillis()
|
|
|
|
block.ModifiedBy = userID
|
|
|
|
|
|
|
|
if existingBlock != nil {
|
|
|
|
// block with ID exists, so this is an update operation
|
|
|
|
query := s.getQueryBuilder().Update(s.tablePrefix+"blocks").
|
|
|
|
Where(sq.Eq{"id": block.ID}).
|
2021-07-24 17:08:25 +02:00
|
|
|
Where(sq.Eq{"COALESCE(workspace_id, '0')": c.WorkspaceID}).
|
2021-07-08 16:36:43 +02:00
|
|
|
Set("parent_id", block.ParentID).
|
|
|
|
Set("root_id", block.RootID).
|
|
|
|
Set("modified_by", block.ModifiedBy).
|
|
|
|
Set(s.escapeField("schema"), block.Schema).
|
|
|
|
Set("type", block.Type).
|
|
|
|
Set("title", block.Title).
|
|
|
|
Set("fields", fieldsJSON).
|
|
|
|
Set("update_at", block.UpdateAt).
|
|
|
|
Set("delete_at", block.DeleteAt)
|
|
|
|
|
2021-07-09 12:59:44 +02:00
|
|
|
q, args, err2 := query.ToSql()
|
|
|
|
if err2 != nil {
|
|
|
|
s.logger.Error("InsertBlock error converting update query object to SQL", mlog.Err(err2))
|
|
|
|
return err2
|
2021-07-08 16:36:43 +02:00
|
|
|
}
|
|
|
|
|
2021-07-09 12:59:44 +02:00
|
|
|
if _, err2 := tx.Exec(q, args...); err2 != nil {
|
|
|
|
s.logger.Error(`InsertBlock error occurred while updating existing block`, mlog.String("blockID", block.ID), mlog.Err(err2))
|
2021-07-29 19:49:57 +02:00
|
|
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
|
|
|
s.logger.Warn("Transaction rollback error", mlog.Err(rollbackErr))
|
|
|
|
}
|
2021-07-09 12:59:44 +02:00
|
|
|
return err2
|
2021-07-08 16:36:43 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
block.CreatedBy = userID
|
|
|
|
block.CreateAt = utils.GetMillis()
|
|
|
|
block.ModifiedBy = userID
|
|
|
|
block.UpdateAt = utils.GetMillis()
|
|
|
|
|
|
|
|
insertQueryValues["created_by"] = block.CreatedBy
|
|
|
|
insertQueryValues["create_at"] = block.CreateAt
|
|
|
|
insertQueryValues["update_at"] = block.UpdateAt
|
|
|
|
insertQueryValues["modified_by"] = block.ModifiedBy
|
|
|
|
|
|
|
|
query := insertQuery.SetMap(insertQueryValues)
|
|
|
|
_, err = sq.ExecContextWith(ctx, tx, query.Into(s.tablePrefix+"blocks"))
|
|
|
|
if err != nil {
|
2021-07-09 05:41:54 +02:00
|
|
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
|
|
|
s.logger.Warn("Transaction rollback error", mlog.Err(rollbackErr))
|
|
|
|
}
|
|
|
|
|
2021-07-08 16:36:43 +02:00
|
|
|
return err
|
|
|
|
}
|
2021-04-22 22:53:01 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 16:36:43 +02:00
|
|
|
// writing block history
|
|
|
|
query := insertQuery.SetMap(insertQueryValues)
|
|
|
|
|
2021-04-22 22:53:01 +02:00
|
|
|
_, err = sq.ExecContextWith(ctx, tx, query.Into(s.tablePrefix+"blocks_history"))
|
|
|
|
if err != nil {
|
2021-07-09 05:41:54 +02:00
|
|
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
|
|
|
s.logger.Warn("Transaction rollback error", mlog.Err(rollbackErr))
|
|
|
|
}
|
2021-04-22 22:53:01 +02:00
|
|
|
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-22 15:22:36 +02:00
|
|
|
|
2020-10-16 16:21:42 +02:00
|
|
|
return nil
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
|
|
|
|
2021-08-06 14:10:24 +02:00
|
|
|
func (s *SQLStore) PatchBlock(c store.Container, blockID string, blockPatch *model.BlockPatch, userID string) error {
|
|
|
|
existingBlock, err := s.GetBlock(c, blockID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if existingBlock == nil {
|
|
|
|
return BlockNotFoundErr{blockID}
|
|
|
|
}
|
|
|
|
|
|
|
|
block := blockPatch.Patch(existingBlock)
|
|
|
|
return s.InsertBlock(c, block, userID)
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
func (s *SQLStore) DeleteBlock(c store.Container, blockID string, modifiedBy string) error {
|
2021-04-22 22:53:01 +02:00
|
|
|
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()
|
2021-04-22 22:53:01 +02:00
|
|
|
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,
|
|
|
|
)
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2021-04-22 22:53:01 +02:00
|
|
|
_, err = sq.ExecContextWith(ctx, tx, insertQuery)
|
|
|
|
if err != nil {
|
2021-07-09 05:41:54 +02:00
|
|
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
|
|
|
s.logger.Warn("Transaction rollback error", mlog.Err(rollbackErr))
|
|
|
|
}
|
2021-04-22 22:53:01 +02:00
|
|
|
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})
|
2021-04-22 22:53:01 +02:00
|
|
|
|
|
|
|
_, err = sq.ExecContextWith(ctx, tx, deleteQuery)
|
|
|
|
if err != nil {
|
2021-07-09 05:41:54 +02:00
|
|
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
|
|
|
s.logger.Warn("Transaction rollback error", mlog.Err(rollbackErr))
|
|
|
|
}
|
2021-04-22 22:53:01 +02:00
|
|
|
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-22 15:22:36 +02:00
|
|
|
|
2020-10-16 16:21:42 +02:00
|
|
|
return nil
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
2021-06-04 16:38:49 +02:00
|
|
|
|
|
|
|
func (s *SQLStore) GetBlockCountsByType() (map[string]int64, error) {
|
|
|
|
query := s.getQueryBuilder().
|
|
|
|
Select(
|
|
|
|
"type",
|
|
|
|
"COUNT(*) AS count",
|
|
|
|
).
|
|
|
|
From(s.tablePrefix + "blocks").
|
|
|
|
GroupBy("type")
|
|
|
|
|
|
|
|
rows, err := query.Query()
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error(`GetBlockCountsByType ERROR`, mlog.Err(err))
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-06 22:44:11 +02:00
|
|
|
defer s.CloseRows(rows)
|
2021-06-04 16:38:49 +02:00
|
|
|
|
|
|
|
m := make(map[string]int64)
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var blockType string
|
|
|
|
var count int64
|
|
|
|
|
|
|
|
err := rows.Scan(&blockType, &count)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("Failed to fetch block count", mlog.Err(err))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m[blockType] = count
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
2021-07-08 16:36:43 +02:00
|
|
|
|
|
|
|
func (s *SQLStore) GetBlock(c store.Container, blockID string) (*model.Block, error) {
|
|
|
|
query := s.getQueryBuilder().
|
|
|
|
Select(
|
|
|
|
"id",
|
|
|
|
"parent_id",
|
|
|
|
"root_id",
|
|
|
|
"created_by",
|
|
|
|
"modified_by",
|
|
|
|
s.escapeField("schema"),
|
|
|
|
"type",
|
|
|
|
"title",
|
|
|
|
"COALESCE(fields, '{}')",
|
|
|
|
"create_at",
|
|
|
|
"update_at",
|
|
|
|
"delete_at",
|
|
|
|
).
|
|
|
|
From(s.tablePrefix + "blocks").
|
|
|
|
Where(sq.Eq{"id": blockID}).
|
|
|
|
Where(sq.Eq{"coalesce(workspace_id, '0')": c.WorkspaceID})
|
|
|
|
|
|
|
|
rows, err := query.Query()
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error(`GetBlock ERROR`, mlog.Err(err))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
blocks, err := s.blocksFromRows(rows)
|
2021-07-09 05:41:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-08 16:36:43 +02:00
|
|
|
|
|
|
|
if len(blocks) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &blocks[0], nil
|
|
|
|
}
|