Skip to content

Commit c796e43

Browse files
authored
chore: migration support for notification db changes (#4906)
* migration support for notification db changes * updated query with internal flag
1 parent 75a2963 commit c796e43

File tree

6 files changed

+120
-21
lines changed

6 files changed

+120
-21
lines changed

internal/sql/repository/NotificationSettingsRepository.go

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package repository
2020
import (
2121
"github.com/devtron-labs/devtron/pkg/sql"
2222
"github.com/go-pg/pg"
23+
"github.com/go-pg/pg/orm"
2324
"strconv"
2425
)
2526

@@ -55,9 +56,7 @@ type NotificationSettingsView struct {
5556
tableName struct{} `sql:"notification_settings_view" pg:",discard_unknown_columns"`
5657
Id int `sql:"id,pk"`
5758
Config string `sql:"config"`
58-
//ConfigName string `sql:"config_name"`
59-
//AppId *int `sql:"app_id"`
60-
//EnvironmentId *int `sql:"env_id"`
59+
Internal bool `sql:"internal"`
6160
sql.AuditLog
6261
}
6362

@@ -73,16 +72,18 @@ type NotificationSettingsViewWithAppEnv struct {
7372
}
7473

7574
type NotificationSettings struct {
76-
tableName struct{} `sql:"notification_settings" pg:",discard_unknown_columns"`
77-
Id int `sql:"id,pk"`
78-
TeamId *int `sql:"team_id"`
79-
AppId *int `sql:"app_id"`
80-
EnvId *int `sql:"env_id"`
81-
PipelineId *int `sql:"pipeline_id"`
82-
PipelineType string `sql:"pipeline_type"`
83-
EventTypeId int `sql:"event_type_id"`
84-
Config string `sql:"config"`
85-
ViewId int `sql:"view_id"`
75+
tableName struct{} `sql:"notification_settings" pg:",discard_unknown_columns"`
76+
Id int `sql:"id,pk"`
77+
TeamId *int `sql:"team_id"`
78+
AppId *int `sql:"app_id"`
79+
EnvId *int `sql:"env_id"`
80+
PipelineId *int `sql:"pipeline_id"`
81+
PipelineType string `sql:"pipeline_type"`
82+
EventTypeId int `sql:"event_type_id"`
83+
Config string `sql:"config"`
84+
ViewId int `sql:"view_id"`
85+
NotificationRuleId int `sql:"notification_rule_id"`
86+
AdditionalConfigJson string `sql:"additional_config_json"` // user defined config json;
8687
}
8788

8889
type SettingOptionDTO struct {
@@ -97,7 +98,14 @@ type SettingOptionDTO struct {
9798
}
9899

99100
func (impl *NotificationSettingsRepositoryImpl) FindNSViewCount() (int, error) {
100-
count, err := impl.dbConnection.Model(&NotificationSettingsView{}).Count()
101+
count, err := impl.dbConnection.Model(&NotificationSettingsView{}).
102+
WhereGroup(func(query *orm.Query) (*orm.Query, error) {
103+
query = query.
104+
WhereOr("internal IS NULL").
105+
WhereOr("internal = ?", false)
106+
return query, nil
107+
}).
108+
Count()
101109
if err != nil {
102110
return 0, err
103111
}
@@ -106,7 +114,18 @@ func (impl *NotificationSettingsRepositoryImpl) FindNSViewCount() (int, error) {
106114

107115
func (impl *NotificationSettingsRepositoryImpl) FindAll(offset int, size int) ([]*NotificationSettingsView, error) {
108116
var ns []*NotificationSettingsView
109-
err := impl.dbConnection.Model(&ns).Order("created_on desc").Offset(offset).Limit(size).Select()
117+
err := impl.dbConnection.
118+
Model(&ns).
119+
WhereGroup(func(query *orm.Query) (*orm.Query, error) {
120+
query = query.
121+
WhereOr("internal IS NULL").
122+
WhereOr("internal = ?", false)
123+
return query, nil
124+
}).
125+
Order("created_on desc").
126+
Offset(offset).
127+
Limit(size).
128+
Select()
110129
if err != nil {
111130
return nil, err
112131
}

internal/sql/repository/WebhookNotificationRepository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type WebhookConfig struct {
2929
WebHookUrl string `sql:"web_hook_url"`
3030
ConfigName string `sql:"config_name"`
3131
Header map[string]interface{} `sql:"header"`
32-
Payload map[string]interface{} `sql:"payload"`
32+
Payload string `sql:"payload"`
3333
Description string `sql:"description"`
3434
OwnerId int32 `sql:"owner_id"`
3535
Active bool `sql:"active"`

pkg/notifier/WebhookNotificationService.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type WebhookConfigDto struct {
5858
WebhookUrl string `json:"webhookUrl" validate:"required"`
5959
ConfigName string `json:"configName" validate:"required"`
6060
Header map[string]interface{} `json:"header"`
61-
Payload map[string]interface{} `json:"payload"`
61+
Payload string `json:"payload"`
6262
Description string `json:"description"`
6363
Id int `json:"id" validate:"number"`
6464
}

pkg/notifier/WebhookNotificationService_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func Test_buildWebhookNewConfigs(t *testing.T) {
3131
{
3232
WebhookUrl: "dfcd nmc dc",
3333
ConfigName: "aditya",
34-
Payload: map[string]interface{}{"text": "final"},
34+
Payload: "{\"text\": \"final\"}",
3535
Header: map[string]interface{}{"Content-type": "application/json"},
3636
},
3737
},
@@ -41,7 +41,7 @@ func Test_buildWebhookNewConfigs(t *testing.T) {
4141
{
4242
WebHookUrl: "dfcd nmc dc",
4343
ConfigName: "aditya",
44-
Payload: map[string]interface{}{"text": "final"},
44+
Payload: "{\"text\": \"final\"}",
4545
Header: map[string]interface{}{"Content-type": "application/json"},
4646
},
4747
},
@@ -91,7 +91,7 @@ func TestWebhookNotificationServiceImpl_SaveOrEditNotificationConfig(t *testing.
9191
{
9292
WebhookUrl: "djfndgfbd,gds",
9393
ConfigName: "aditya",
94-
Payload: map[string]interface{}{"text": "final"},
94+
Payload: "{\"text\": \"final\"}",
9595
Header: map[string]interface{}{"Content-type": "application/json"},
9696
},
9797
},
@@ -107,7 +107,7 @@ func TestWebhookNotificationServiceImpl_SaveOrEditNotificationConfig(t *testing.
107107
{
108108
WebhookUrl: "d,fm sdfd",
109109
ConfigName: "aditya",
110-
Payload: map[string]interface{}{"text": "final"},
110+
Payload: "{\"text\": \"final\"}",
111111
Header: map[string]interface{}{"Content-type": "application/json"},
112112
},
113113
},
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
-- Create a new function for JSON validation
2+
CREATE OR REPLACE FUNCTION jsonb_valid(_txt text)
3+
RETURNS bool
4+
LANGUAGE plpgsql IMMUTABLE STRICT AS
5+
$func$
6+
BEGIN
7+
RETURN _txt::json IS NOT NULL;
8+
EXCEPTION
9+
WHEN SQLSTATE '22P02' THEN -- invalid_text_representation
10+
RETURN false;
11+
END
12+
$func$;
13+
14+
-- Add comment to the func
15+
COMMENT ON FUNCTION jsonb_valid(text) IS 'Validate if input text is valid JSON.
16+
Returns true, false, or NULL on NULL input.';
17+
18+
-- Down Migration
19+
DELETE FROM webhook_config WHERE NOT jsonb_valid(payload);
20+
ALTER TABLE webhook_config ALTER payload TYPE JSONB USING payload::JSONB;
21+
22+
23+
-- Migration for creating new column in notification_settings
24+
ALTER TABLE "public"."notification_settings"
25+
DROP COLUMN IF EXISTS notification_rule_id,
26+
DROP COLUMN IF EXISTS additional_config_json;
27+
28+
-- Drop notification_rule table
29+
DROP TABLE IF EXISTS "public"."notification_rule";
30+
31+
-- Drop sequence notification_rule
32+
DROP SEQUENCE IF EXISTS id_seq_notification_rule_sequence;
33+
34+
-- Drop internal column from table notification_settings_view
35+
ALTER TABLE "public"."notification_settings_view"
36+
DROP COLUMN IF EXISTS internal;
37+
38+
-- Delete notification_settings
39+
DELETE FROM "public"."notification_settings" WHERE event_type_id = 8;
40+
41+
-- Delete notification_settings_view
42+
DELETE FROM "public"."notification_settings_view" WHERE config::jsonb @> '{"eventTypeIds": [8]}';
43+
44+
-- Delete event
45+
DELETE FROM "public"."event" WHERE id = 8;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
-- Migration of data type in webhook_config
2+
ALTER TABLE webhook_config ALTER payload TYPE VARCHAR;
3+
4+
-- Migration for creating new event
5+
INSERT INTO "public"."event" (id, event_type, description) VALUES (8, 'IMAGE SCANNING', '');
6+
7+
-- Create notification_rule sequence
8+
CREATE SEQUENCE IF NOT EXISTS id_seq_notification_rule_sequence;
9+
10+
-- notification_rule table definition
11+
CREATE TABLE IF NOT EXISTS "public"."notification_rule" (
12+
"id" integer NOT NULL DEFAULT nextval('id_seq_notification_rule_sequence'::regclass),
13+
"expression" VARCHAR(1000),
14+
"condition_type" integer NOT NULL DEFAULT 0,
15+
"created_on" timestamptz NOT NULL,
16+
"created_by" int4 NOT NULL,
17+
"updated_on" timestamptz,
18+
"updated_by" int4,
19+
PRIMARY KEY ("id")
20+
);
21+
22+
-- Migration for creating new column in notification_settings_view
23+
ALTER TABLE "public"."notification_settings_view"
24+
ADD COLUMN IF NOT EXISTS internal bool;
25+
26+
-- Migration for creating new column in notification_settings
27+
ALTER TABLE "public"."notification_settings"
28+
ADD COLUMN IF NOT EXISTS notification_rule_id integer,
29+
ADD COLUMN IF NOT EXISTS additional_config_json VARCHAR;
30+
31+
-- Constraints for the new column in notification_settings
32+
ALTER TABLE "public"."notification_settings"
33+
ADD CONSTRAINT notification_setting_notification_rule_id_fk
34+
FOREIGN KEY ("notification_rule_id")
35+
REFERENCES "public"."notification_rule"("id");

0 commit comments

Comments
 (0)