Don't insert nulls into blocks_history table. (#1890)

Ensure all fields are non-NULL when inserting into blocks_history (on block delete).
This commit is contained in:
Doug Lauder 2021-12-03 12:32:57 -05:00 committed by GitHub
parent 206bb98800
commit 8be71b1498
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 811 additions and 209 deletions

View file

@ -391,21 +391,51 @@ func (s *SQLStore) patchBlock(db sq.BaseRunner, c store.Container, blockID strin
}
func (s *SQLStore) deleteBlock(db sq.BaseRunner, c store.Container, blockID string, modifiedBy string) error {
block, err := s.getBlock(db, c, blockID)
if err != nil {
return err
}
if block == nil {
return nil // deleting non-exiting block is not considered an error (for now)
}
fieldsJSON, err := json.Marshal(block.Fields)
if err != nil {
return err
}
now := utils.GetMillis()
insertQuery := s.getQueryBuilder(db).Insert(s.tablePrefix+"blocks_history").
Columns(
"workspace_id",
"id",
"parent_id",
"schema",
"type",
"title",
"fields",
"root_id",
"modified_by",
"create_at",
"update_at",
"delete_at",
"created_by",
).
Values(
c.WorkspaceID,
blockID,
block.ID,
block.ParentID,
block.Schema,
block.Type,
block.Title,
fieldsJSON,
block.RootID,
modifiedBy,
block.CreateAt,
now,
now,
block.CreatedBy,
)
if _, err := insertQuery.Exec(); err != nil {
@ -423,7 +453,6 @@ func (s *SQLStore) deleteBlock(db sq.BaseRunner, c store.Container, blockID stri
return nil
}
func (s *SQLStore) getBlockCountsByType(db sq.BaseRunner) (map[string]int64, error) {
query := s.getQueryBuilder(db).
Select(

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,6 @@
-- no reverse migration for this version
-- We need a query here otherwise the migration will result
-- in an empty query when the if condition is false.
-- Empty query causes a "Query was empty" error.
SELECT 1;

View file

@ -0,0 +1,90 @@
/* parent_id */
UPDATE {{.prefix}}blocks_history AS bh1
SET parent_id = COALESCE(
(SELECT bh2.parent_id
FROM {{.prefix}}blocks_history AS bh2
WHERE bh1.id = bh2.id AND bh2.parent_id IS NOT NULL
ORDER BY bh2.insert_at ASC limit 1)
, '')
WHERE parent_id IS NULL;
/* schema */
UPDATE {{.prefix}}blocks_history AS bh1
SET schema = COALESCE(
(SELECT bh2.schema
FROM {{.prefix}}blocks_history AS bh2
WHERE bh1.id = bh2.id AND bh2.schema IS NOT NULL
ORDER BY bh2.insert_at ASC limit 1)
, 1)
WHERE schema IS NULL;
/* type */
UPDATE {{.prefix}}blocks_history AS bh1
SET type = COALESCE(
(SELECT bh2.type
FROM {{.prefix}}blocks_history AS bh2
WHERE bh1.id = bh2.id AND bh2.type IS NOT NULL
ORDER BY bh2.insert_at ASC limit 1)
, '')
WHERE type IS NULL;
/* title */
UPDATE {{.prefix}}blocks_history AS bh1
SET title = COALESCE(
(SELECT bh2.title
FROM {{.prefix}}blocks_history AS bh2
WHERE bh1.id = bh2.id AND bh2.title IS NOT NULL
ORDER BY bh2.insert_at ASC limit 1)
, '')
WHERE title IS NULL;
/* fields */
{{if .postgres}}
UPDATE {{.prefix}}blocks_history AS bh1
SET fields = COALESCE(
(SELECT bh2.fields
FROM {{.prefix}}blocks_history AS bh2
WHERE bh1.id = bh2.id AND bh2.fields IS NOT NULL
ORDER BY bh2.insert_at ASC limit 1)
, '{}'::json)
WHERE fields IS NULL;
{{else}}
UPDATE {{.prefix}}blocks_history AS bh1
SET fields = COALESCE(
(SELECT bh2.fields
FROM {{.prefix}}blocks_history AS bh2
WHERE bh1.id = bh2.id AND bh2.fields IS NOT NULL
ORDER BY bh2.insert_at ASC limit 1)
, '')
WHERE fields IS NULL;
{{end}}
/* create_at */
UPDATE {{.prefix}}blocks_history AS bh1
SET create_at = COALESCE(
(SELECT bh2.create_at
FROM {{.prefix}}blocks_history AS bh2
WHERE bh1.id = bh2.id AND bh2.create_at IS NOT NULL
ORDER BY bh2.insert_at ASC limit 1)
, bh1.update_at)
WHERE create_at IS NULL;
/* root_id */
UPDATE {{.prefix}}blocks_history AS bh1
SET root_id = COALESCE(
(SELECT bh2.root_id
FROM {{.prefix}}blocks_history AS bh2
WHERE bh1.id = bh2.id AND bh2.root_id IS NOT NULL
ORDER BY bh2.insert_at ASC limit 1)
, '')
WHERE root_id IS NULL;
/* created_by */
UPDATE {{.prefix}}blocks_history AS bh1
SET created_by = COALESCE(
(SELECT bh2.created_by
FROM {{.prefix}}blocks_history AS bh2
WHERE bh1.id = bh2.id AND bh2.created_by IS NOT NULL
ORDER BY bh2.insert_at ASC limit 1)
, 'system')
WHERE created_by IS NULL;

View file

@ -2,7 +2,9 @@
//go:generate go run ./generators/main.go
package store
import "github.com/mattermost/focalboard/server/model"
import (
"github.com/mattermost/focalboard/server/model"
)
// Conainer represents a container in a store
// Using a struct to make extending this easier in the future.