Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore(store): use jsonb for json payload (#697)

Danny Xub4c46f909be9
diff --git a/store/activity.go b/store/activity.go
index c4d698a41c..44f9287a0e 100644
--- a/store/activity.go
+++ b/store/activity.go
@@ -127,6 +127,9 @@ func (s *ActivityService) DeleteActivity(ctx context.Context, delete *api.Activi
// createActivity creates a new activity.
func createActivity(ctx context.Context, tx *sql.Tx, create *api.ActivityCreate) (*api.Activity, error) {
// Insert row into activity.
+ if create.Payload == "" {
+ create.Payload = "{}"
+ }
row, err := tx.QueryContext(ctx, `
INSERT INTO activity (
creator_id,
diff --git a/store/anomaly.go b/store/anomaly.go
index 58f44fe352..68e368d1fb 100644
--- a/store/anomaly.go
+++ b/store/anomaly.go
@@ -115,6 +115,9 @@ func (s *AnomalyService) ArchiveAnomaly(ctx context.Context, archive *api.Anomal
// createAnomaly creates a new anomaly.
func createAnomaly(ctx context.Context, tx *sql.Tx, upsert *api.AnomalyUpsert) (*api.Anomaly, error) {
// Inserts row into database.
+ if upsert.Payload == "" {
+ upsert.Payload = "{}"
+ }
row, err := tx.QueryContext(ctx, `
INSERT INTO anomaly (
creator_id,
@@ -253,6 +256,9 @@ type anomalyPatch struct {
// patchAnomaly patches an anomaly
func patchAnomaly(ctx context.Context, tx *sql.Tx, patch *anomalyPatch) (*api.Anomaly, error) {
// Build UPDATE clause.
+ if patch.Payload == "" {
+ patch.Payload = "{}"
+ }
set, args := []string{"updater_id = $1"}, []interface{}{patch.UpdaterID}
set, args = append(set, "payload = $2"), append(args, patch.Payload)
args = append(args, patch.ID)
diff --git a/store/deploy.go b/store/deploy.go
index e8d188420c..bec5b31ae3 100644
--- a/store/deploy.go
+++ b/store/deploy.go
@@ -121,6 +121,9 @@ func (s *DeploymentConfigService) UpsertDeploymentConfig(ctx context.Context, up
}
func (s *DeploymentConfigService) upsertDeploymentConfig(ctx context.Context, tx *sql.Tx, upsert *api.DeploymentConfigUpsert) (*api.DeploymentConfig, error) {
+ if upsert.Payload == "" {
+ upsert.Payload = "{}"
+ }
row, err := tx.QueryContext(ctx, `
INSERT INTO deployment_config (
creator_id,
diff --git a/store/issue.go b/store/issue.go
index 157d46849c..8bcc28223c 100644
--- a/store/issue.go
+++ b/store/issue.go
@@ -129,6 +129,9 @@ func (s *IssueService) PatchIssue(ctx context.Context, patch *api.IssuePatch) (*
// createIssue creates a new issue.
func (s *IssueService) createIssue(ctx context.Context, tx *sql.Tx, create *api.IssueCreate) (*api.Issue, error) {
+ if create.Payload == "" {
+ create.Payload = "{}"
+ }
row, err := tx.QueryContext(ctx, `
INSERT INTO issue (
creator_id,
diff --git a/store/migration/10001__init_schema.sql b/store/migration/10001__init_schema.sql
index 6c36503748..22852aa7a3 100644
--- a/store/migration/10001__init_schema.sql
+++ b/store/migration/10001__init_schema.sql
@@ -143,7 +143,7 @@ CREATE TABLE policy (
environment_id INTEGER NOT NULL REFERENCES environment (id),
-- allowed types are in the format of 'bb.policy.*'.
type TEXT NOT NULL,
- payload TEXT NOT NULL
+ payload JSONB NOT NULL DEFAULT '{}'
);
CREATE INDEX idx_policy_environment_id ON policy(environment_id);
@@ -233,7 +233,7 @@ CREATE TABLE project_member (
-- allowed role_provider are 'BYTEBASE', 'GITLAB_SELF_HOST'.
role_provider TEXT NOT NULL DEFAULT 'BYTEBASE',
-- payload is determined by the type of role_provider
- payload TEXT NOT NULL DEFAULT ''
+ payload JSONB NOT NULL DEFAULT '{}'
);
CREATE UNIQUE INDEX idx_project_member_unique_project_id_principal_id ON project_member(project_id, principal_id);
@@ -660,7 +660,7 @@ CREATE TABLE task (
status TEXT NOT NULL,
-- allowed types are in the format of 'bb.task.*'.
type TEXT NOT NULL,
- payload TEXT NOT NULL DEFAULT '',
+ payload JSONB NOT NULL DEFAULT '{}',
earliest_allowed_ts BIGINT NOT NULL DEFAULT 0
);
@@ -694,8 +694,8 @@ CREATE TABLE task_run (
code INTEGER NOT NULL DEFAULT 0,
comment TEXT NOT NULL DEFAULT '',
-- result saves the task run result in json format
- result TEXT NOT NULL DEFAULT '',
- payload TEXT NOT NULL DEFAULT ''
+ result JSONB NOT NULL DEFAULT '{}',
+ payload JSONB NOT NULL DEFAULT '{}'
);
CREATE INDEX idx_task_run_task_id ON task_run(task_id);
@@ -723,8 +723,8 @@ CREATE TABLE task_check_run (
code INTEGER NOT NULL DEFAULT 0,
comment TEXT NOT NULL DEFAULT '',
-- result saves the task check run result in json format
- result TEXT NOT NULL DEFAULT '',
- payload TEXT NOT NULL DEFAULT ''
+ result JSONB NOT NULL DEFAULT '{}',
+ payload JSONB NOT NULL DEFAULT '{}'
);
CREATE INDEX idx_task_check_run_task_id ON task_check_run(task_id);
@@ -759,7 +759,7 @@ CREATE TABLE issue (
description TEXT NOT NULL DEFAULT '',
-- we require an assignee, if user wants to unassign herself, she can re-assign to the system account.
assignee_id INTEGER NOT NULL REFERENCES principal (id),
- payload TEXT NOT NULL DEFAULT ''
+ payload JSONB NOT NULL DEFAULT '{}'
);
CREATE INDEX idx_issue_project_id ON issue(project_id);
@@ -807,7 +807,7 @@ CREATE TABLE activity (
-- allowed levels are 'INFO', 'WARN', 'ERROR'.
level TEXT NOT NULL,
comment TEXT NOT NULL DEFAULT '',
- payload TEXT NOT NULL DEFAULT ''
+ payload JSONB NOT NULL DEFAULT '{}'
);
CREATE INDEX idx_activity_container_id ON activity(container_id);
@@ -970,7 +970,7 @@ CREATE TABLE anomaly (
database_id INTEGER NULL REFERENCES db (id),
-- allowed types are in the format of 'bb.anomaly.*'.
type TEXT NOT NULL,
- payload TEXT NOT NULL DEFAULT ''
+ payload JSONB NOT NULL DEFAULT '{}'
);
CREATE INDEX idx_anomaly_instance_id_row_status_type ON anomaly(instance_id, row_status, type);
@@ -1070,7 +1070,7 @@ CREATE TABLE deployment_config (
updated_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
project_id INTEGER NOT NULL REFERENCES project (id),
name TEXT NOT NULL,
- config TEXT NOT NULL
+ config JSONB NOT NULL DEFAULT '{}'
);
CREATE UNIQUE INDEX idx_deployment_config_unique_project_id ON deployment_config(project_id);
diff --git a/store/policy.go b/store/policy.go
index c83bfbd392..0f7036d8bf 100644
--- a/store/policy.go
+++ b/store/policy.go
@@ -160,6 +160,9 @@ func (s *PolicyService) UpsertPolicy(ctx context.Context, upsert *api.PolicyUpse
// upsertPolicy updates an existing policy.
func (s *PolicyService) upsertPolicy(ctx context.Context, tx *sql.Tx, upsert *api.PolicyUpsert) (*api.Policy, error) {
// Upsert row into policy.
+ if upsert.Payload == "" {
+ upsert.Payload = "{}"
+ }
row, err := tx.QueryContext(ctx, `
INSERT INTO policy (
creator_id,
diff --git a/store/project_member.go b/store/project_member.go
index 067236f307..2dfc353b8a 100644
--- a/store/project_member.go
+++ b/store/project_member.go
@@ -199,6 +199,9 @@ func (s *ProjectMemberService) SetProjectMember(ctx context.Context, set *api.Pr
// createProjectMember creates a new projectMember.
func createProjectMember(ctx context.Context, tx *sql.Tx, create *api.ProjectMemberCreate) (*api.ProjectMember, error) {
// Insert row into database.
+ if create.Payload == "" {
+ create.Payload = "{}"
+ }
row, err := tx.QueryContext(ctx, `
INSERT INTO project_member (
creator_id,
@@ -316,7 +319,11 @@ func patchProjectMember(ctx context.Context, tx *sql.Tx, patch *api.ProjectMembe
set, args = append(set, fmt.Sprintf("role_provider = $%d", len(args)+1)), append(args, api.Role(*v))
}
if v := patch.Payload; v != nil {
- set, args = append(set, fmt.Sprintf("payload = $%d", len(args)+1)), append(args, api.Role(*v))
+ payload := "{}"
+ if *v == "" {
+ payload = *v
+ }
+ set, args = append(set, fmt.Sprintf("payload = $%d", len(args)+1)), append(args, api.Role(payload))
}
args = append(args, patch.ID)
diff --git a/store/seed/release/10001__init_seed.sql b/store/seed/release/10001__init_seed.sql
index e8007eeff4..8dc09caaf0 100644
--- a/store/seed/release/10001__init_seed.sql
+++ b/store/seed/release/10001__init_seed.sql
@@ -439,7 +439,7 @@ VALUES
'bb.task.general',
'',
'{"detail":"Something is not right..."}',
- ''
+ '{}'
);
INSERT INTO
@@ -462,7 +462,7 @@ VALUES
'RUNNING',
'bb.task.general',
'Let''s give another try',
- ''
+ '{}'
);
-- Create issue 101 "Hello world"
diff --git a/store/seed/test/10121__task_check_run.sql b/store/seed/test/10121__task_check_run.sql
index 7ff9ae5a24..5799101847 100644
--- a/store/seed/test/10121__task_check_run.sql
+++ b/store/seed/test/10121__task_check_run.sql
@@ -22,8 +22,8 @@ VALUES
'bb.task-check.database.connect',
101,
'failed to connect database "shop": failed to connect database On-premises Staging MySQL/shop at "mysql.staging.example.com":"3306" with user "admin": dial tcp: lookup mysql.staging.example.com: no such host',
- '',
- ''
+ '{}',
+ '{}'
);
INSERT INTO
diff --git a/store/seed/test/10140__activity.sql b/store/seed/test/10140__activity.sql
index bbbb0f13e4..ee02b795af 100644
--- a/store/seed/test/10140__activity.sql
+++ b/store/seed/test/10140__activity.sql
@@ -1052,7 +1052,7 @@ VALUES
'bb.project.member.role.update',
'INFO',
'Changed Demo Owner ([email protected]) from OWNER to DEVELOPER.',
- ''
+ '{}'
);
-- Project activity for 3002
@@ -1080,7 +1080,7 @@ VALUES
'bb.project.member.create',
'INFO',
'Granted Jerry DBA to [email protected] (DEVELOPER).',
- ''
+ '{}'
);
INSERT INTO
@@ -1135,7 +1135,7 @@ VALUES
'bb.project.member.create',
'INFO',
'Granted Tom Dev to [email protected] (DEVELOPER).',
- ''
+ '{}'
);
INSERT INTO
diff --git a/store/task.go b/store/task.go
index 5a0da0d616..5330b10ff2 100644
--- a/store/task.go
+++ b/store/task.go
@@ -125,6 +125,9 @@ func (s *TaskService) createTask(ctx context.Context, tx *sql.Tx, create *api.Ta
var row *sql.Rows
var err error
+ if create.Payload == "" {
+ create.Payload = "{}"
+ }
if create.DatabaseID == nil {
row, err = tx.QueryContext(ctx, `
INSERT INTO task (
@@ -338,7 +341,11 @@ func (s *TaskService) patchTask(ctx context.Context, tx *sql.Tx, patch *api.Task
set, args = append(set, fmt.Sprintf("database_id = $%d", len(args)+1)), append(args, *v)
}
if v := patch.Payload; v != nil {
- set, args = append(set, fmt.Sprintf("payload = $%d", len(args)+1)), append(args, *v)
+ payload := "{}"
+ if *v != "" {
+ payload = *v
+ }
+ set, args = append(set, fmt.Sprintf("payload = $%d", len(args)+1)), append(args, payload)
}
if v := patch.EarliestAllowedTs; v != nil {
set, args = append(set, fmt.Sprintf("earliest_allowed_ts = $%d", len(args)+1)), append(args, *v)
diff --git a/store/task_check_run.go b/store/task_check_run.go
index 8ba4018cf3..8a399015eb 100644
--- a/store/task_check_run.go
+++ b/store/task_check_run.go
@@ -91,6 +91,9 @@ func (s *TaskCheckRunService) CreateTaskCheckRunIfNeeded(ctx context.Context, cr
// createTaskCheckRunTx creates a new taskCheckRun.
func (s *TaskCheckRunService) createTaskCheckRunTx(ctx context.Context, tx *sql.Tx, create *api.TaskCheckRunCreate) (*api.TaskCheckRun, error) {
+ if create.Payload == "" {
+ create.Payload = "{}"
+ }
rows, err := tx.QueryContext(ctx, `
INSERT INTO task_check_run (
creator_id,
@@ -192,6 +195,9 @@ func (s *TaskCheckRunService) PatchTaskCheckRunStatus(ctx context.Context, patch
// patchTaskCheckRunStatusTx updates a taskCheckRun status. Returns the new state of the taskCheckRun after update.
func (s *TaskCheckRunService) patchTaskCheckRunStatusTx(ctx context.Context, tx *sql.Tx, patch *api.TaskCheckRunStatusPatch) (*api.TaskCheckRun, error) {
// Build UPDATE clause.
+ if patch.Result == "" {
+ patch.Result = "{}"
+ }
set, args := []string{"updater_id = $1"}, []interface{}{patch.UpdaterID}
set, args = append(set, "status = $2"), append(args, patch.Status)
set, args = append(set, "code = $3"), append(args, patch.Code)
diff --git a/store/task_run.go b/store/task_run.go
index 56520959bc..4eb2f90891 100644
--- a/store/task_run.go
+++ b/store/task_run.go
@@ -28,6 +28,9 @@ func NewTaskRunService(logger *zap.Logger, db *DB) *TaskRunService {
// CreateTaskRunTx creates a new taskRun.
func (s *TaskRunService) CreateTaskRunTx(ctx context.Context, tx *sql.Tx, create *api.TaskRunCreate) (*api.TaskRun, error) {
+ if create.Payload == "" {
+ create.Payload = "{}"
+ }
row, err := tx.QueryContext(ctx, `
INSERT INTO task_run (
creator_id,
@@ -114,7 +117,11 @@ func (s *TaskRunService) PatchTaskRunStatusTx(ctx context.Context, tx *sql.Tx, p
set, args = append(set, fmt.Sprintf("comment = $%d", len(args)+1)), append(args, *v)
}
if v := patch.Result; v != nil {
- set, args = append(set, fmt.Sprintf("result = $%d", len(args)+1)), append(args, *v)
+ result := "{}"
+ if *v != "" {
+ result = *v
+ }
+ set, args = append(set, fmt.Sprintf("result = $%d", len(args)+1)), append(args, result)
}
// Build WHERE clause.