Skip to content

Commit 17e59d2

Browse files
authored
NOISSUE - Add Metadata to Rule struct (#39)
* add metadata Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com> * update sql formatting Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com> * add created by Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com> * format sql Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com> * format sql query Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com> --------- Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com>
1 parent cd528e7 commit 17e59d2

File tree

4 files changed

+92
-47
lines changed

4 files changed

+92
-47
lines changed

re/postgres/init.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,24 @@ func Migration() *migrate.MemoryMigrationSource {
1717
// STATUS 0 to imply enabled and 1 to imply disabled
1818
Up: []string{
1919
`CREATE TABLE IF NOT EXISTS rules (
20-
id VARCHAR(36) PRIMARY KEY,
21-
name VARCHAR(1024),
22-
domain_id VARCHAR(36) NOT NULL,
23-
metadata JSONB,
24-
created_at TIMESTAMP,
25-
updated_at TIMESTAMP,
26-
updated_by VARCHAR(254),
27-
input_channel VARCHAR(36),
28-
input_topic TEXT,
29-
output_channel VARCHAR(36),
30-
output_topic TEXT,
31-
status SMALLINT NOT NULL DEFAULT 0 CHECK (status >= 0),
32-
logic_type SMALLINT NOT NULL DEFAULT 0 CHECK (status >= 0),
33-
logic_value BYTEA,
34-
recurring_time TIMESTAMP[],
35-
recurring_type SMALLINT,
36-
recurring_period SMALLINT
20+
id VARCHAR(36) PRIMARY KEY,
21+
name VARCHAR(1024),
22+
domain_id VARCHAR(36) NOT NULL,
23+
metadata JSONB,
24+
created_by VARCHAR(254),
25+
created_at TIMESTAMP,
26+
updated_at TIMESTAMP,
27+
updated_by VARCHAR(254),
28+
input_channel VARCHAR(36),
29+
input_topic TEXT,
30+
output_channel VARCHAR(36),
31+
output_topic TEXT,
32+
status SMALLINT NOT NULL DEFAULT 0 CHECK (status >= 0),
33+
logic_type SMALLINT NOT NULL DEFAULT 0 CHECK (status >= 0),
34+
logic_value BYTEA,
35+
recurring_time TIMESTAMP[],
36+
recurring_type SMALLINT,
37+
recurring_period SMALLINT
3738
)`,
3839
},
3940
Down: []string{

re/postgres/repository.go

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,30 @@ import (
1717
// SQL Queries as Strings.
1818
const (
1919
addRuleQuery = `
20-
INSERT INTO rules (id, name, domain_id, input_channel, input_topic, logic_type, logic_value,
21-
output_channel, output_topic, recurring_time, recurring_type, recurring_period, created_at, updated_at, updated_by, status)
22-
VALUES (:id, :name, :domain_id, :input_channel, :input_topic, :logic_type, :logic_value,
20+
INSERT INTO rules (id, name, domain_id, metadata, input_channel, input_topic, logic_type, logic_value,
21+
output_channel, output_topic, recurring_time, recurring_type, recurring_period, created_at, created_by, updated_at, updated_by, status)
22+
VALUES (:id, :name, :domain_id, :metadata, :input_channel, :input_topic, :logic_type, :logic_value,
2323
:output_channel, :output_topic, :recurring_time, :recurring_type, :recurring_period, :created_at, :updated_at, :updated_by, :status)
24-
RETURNING id, name, domain_id, input_channel, input_topic, logic_type, logic_value,
25-
output_channel, output_topic, recurring_time, recurring_type, recurring_period, created_at, updated_at, updated_by, status;
24+
RETURNING id, name, domain_id, metadata, input_channel, input_topic, logic_type, logic_value,
25+
output_channel, output_topic, recurring_time, recurring_type, recurring_period, created_at, created_by, updated_at, updated_by, status;
2626
`
2727

2828
viewRuleQuery = `
29-
SELECT id, name, domain_id, input_channel, input_topic, logic_type, logic_value, output_channel,
30-
output_topic, recurring_time, recurring_type, recurring_period, created_at, updated_at, updated_by, status
29+
SELECT id, name, domain_id, metadata, input_channel, input_topic, logic_type, logic_value, output_channel,
30+
output_topic, recurring_time, recurring_type, recurring_period, created_at, created_by, updated_at, updated_by, status
3131
FROM rules
3232
WHERE id = $1;
3333
`
3434

3535
updateRuleQuery = `
3636
UPDATE rules
37-
SET name = :name, input_channel = :input_channel, input_topic = :input_topic, logic_type = :logic_type,
37+
SET name = :name, metadata = :metadata, input_channel = :input_channel, input_topic = :input_topic, logic_type = :logic_type,
3838
logic_value = :logic_value, output_channel = :output_channel, output_topic = :output_topic,
3939
recurring_time = :recurring_time, recurring_type = :recurring_type,
4040
recurring_period = :recurring_period, updated_at = :updated_at, updated_by = :updated_by, status = :status
4141
WHERE id = :id
42-
RETURNING id, name, domain_id, input_channel, input_topic, logic_type, logic_value,
43-
output_channel, output_topic, recurring_time, recurring_type, recurring_period, created_at, updated_at, updated_by, status;
42+
RETURNING id, name, domain_id, metadata, input_channel, input_topic, logic_type, logic_value,
43+
output_channel, output_topic, recurring_time, recurring_type, recurring_period, created_at, created_by, updated_at, updated_by, status;
4444
`
4545

4646
removeRuleQuery = `
@@ -52,13 +52,13 @@ const (
5252
UPDATE rules
5353
SET status = $2
5454
WHERE id = $1
55-
RETURNING id, name, domain_id, input_channel, input_topic, logic_type, logic_value,
56-
output_channel, output_topic, recurring_time, recurring_type, recurring_period, created_at, updated_at, updated_by, status;
55+
RETURNING id, name, domain_id, metadata, input_channel, input_topic, logic_type, logic_value,
56+
output_channel, output_topic, recurring_time, recurring_type, recurring_period, created_at, created_by, updated_at, updated_by, status;
5757
`
5858

5959
listRulesQuery = `
6060
SELECT id, name, domain_id, input_channel, input_topic, logic_type, logic_value, output_channel,
61-
output_topic, recurring_time, recurring_type, recurring_period, created_at, updated_at, updated_by, status
61+
output_topic, recurring_time, recurring_type, recurring_period, created_at, created_by, updated_at, updated_by, status
6262
FROM rules r %s %s;
6363
`
6464

@@ -74,7 +74,10 @@ func NewRepository(db postgres.Database) re.Repository {
7474
}
7575

7676
func (repo *PostgresRepository) AddRule(ctx context.Context, r re.Rule) (re.Rule, error) {
77-
dbr := ruleToDb(r)
77+
dbr, err := ruleToDb(r)
78+
if err != nil {
79+
return re.Rule{}, err
80+
}
7881
row, err := repo.DB.NamedQueryContext(ctx, addRuleQuery, dbr)
7982
if err != nil {
8083
return re.Rule{}, err
@@ -88,7 +91,10 @@ func (repo *PostgresRepository) AddRule(ctx context.Context, r re.Rule) (re.Rule
8891
}
8992
}
9093

91-
rule := dbToRule(dbRule)
94+
rule, err := dbToRule(dbRule)
95+
if err != nil {
96+
return re.Rule{}, err
97+
}
9298

9399
return rule, nil
94100
}
@@ -102,7 +108,10 @@ func (repo *PostgresRepository) ViewRule(ctx context.Context, id string) (re.Rul
102108
if err := row.StructScan(&dbr); err != nil {
103109
return re.Rule{}, err
104110
}
105-
ret := dbToRule(dbr)
111+
ret, err := dbToRule(dbr)
112+
if err != nil {
113+
return re.Rule{}, err
114+
}
106115

107116
return ret, nil
108117
}
@@ -118,13 +127,19 @@ func (repo *PostgresRepository) UpdateRuleStatus(ctx context.Context, id string,
118127
return re.Rule{}, err
119128
}
120129

121-
rule := dbToRule(dbr)
130+
rule, err := dbToRule(dbr)
131+
if err != nil {
132+
return re.Rule{}, err
133+
}
122134

123135
return rule, nil
124136
}
125137

126138
func (repo *PostgresRepository) UpdateRule(ctx context.Context, r re.Rule) (re.Rule, error) {
127-
dbr := ruleToDb(r)
139+
dbr, err := ruleToDb(r)
140+
if err != nil {
141+
return re.Rule{}, err
142+
}
128143
row, err := repo.DB.NamedQueryContext(ctx, updateRuleQuery, dbr)
129144
if err != nil {
130145
return re.Rule{}, err
@@ -137,7 +152,10 @@ func (repo *PostgresRepository) UpdateRule(ctx context.Context, r re.Rule) (re.R
137152
return re.Rule{}, err
138153
}
139154
}
140-
rule := dbToRule(dbRule)
155+
rule, err := dbToRule(dbRule)
156+
if err != nil {
157+
return re.Rule{}, err
158+
}
141159

142160
return rule, nil
143161
}
@@ -177,7 +195,11 @@ func (repo *PostgresRepository) ListRules(ctx context.Context, pm re.PageMeta) (
177195
if err := rows.StructScan(&r); err != nil {
178196
return re.Page{}, errors.Wrap(repoerr.ErrViewEntity, err)
179197
}
180-
rules = append(rules, dbToRule(r))
198+
ret, err := dbToRule(r)
199+
if err != nil {
200+
return re.Page{}, err
201+
}
202+
rules = append(rules, ret)
181203
}
182204

183205
cq := fmt.Sprintf(totalQuery, pq)

re/postgres/rule.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ package postgres
55

66
import (
77
"database/sql"
8+
"encoding/json"
89
"time"
910

1011
"github.com/absmach/magistrala/re"
12+
"github.com/absmach/supermq/pkg/errors"
1113
"github.com/jackc/pgx/v5/pgtype"
1214
)
1315

@@ -16,6 +18,7 @@ type dbRule struct {
1618
ID string `db:"id"`
1719
Name string `db:"name"`
1820
DomainID string `db:"domain_id"`
21+
Metadata []byte `db:"metadata,omitempty"`
1922
InputChannel string `db:"input_channel"`
2023
InputTopic sql.NullString `db:"input_topic"`
2124
LogicType re.ScriptType `db:"logic_type"`
@@ -32,11 +35,20 @@ type dbRule struct {
3235
UpdatedBy string `db:"updated_by"`
3336
}
3437

35-
func ruleToDb(r re.Rule) dbRule {
38+
func ruleToDb(r re.Rule) (dbRule, error) {
39+
metadata := []byte("{}")
40+
if len(r.Metadata) > 0 {
41+
b, err := json.Marshal(r.Metadata)
42+
if err != nil {
43+
return dbRule{}, errors.Wrap(errors.ErrMalformedEntity, err)
44+
}
45+
metadata = b
46+
}
3647
return dbRule{
3748
ID: r.ID,
3849
Name: r.Name,
3950
DomainID: r.DomainID,
51+
Metadata: metadata,
4052
InputChannel: r.InputChannel,
4153
InputTopic: toNullString(r.InputTopic),
4254
LogicType: r.Logic.Type,
@@ -51,14 +63,21 @@ func ruleToDb(r re.Rule) dbRule {
5163
CreatedBy: r.CreatedBy,
5264
UpdatedAt: r.UpdatedAt,
5365
UpdatedBy: r.UpdatedBy,
54-
}
66+
}, nil
5567
}
5668

57-
func dbToRule(dto dbRule) re.Rule {
69+
func dbToRule(dto dbRule) (re.Rule, error) {
70+
var metadata re.Metadata
71+
if dto.Metadata != nil {
72+
if err := json.Unmarshal(dto.Metadata, &metadata); err != nil {
73+
return re.Rule{}, errors.Wrap(errors.ErrMalformedEntity, err)
74+
}
75+
}
5876
return re.Rule{
5977
ID: dto.ID,
6078
Name: dto.Name,
6179
DomainID: dto.DomainID,
80+
Metadata: metadata,
6281
InputChannel: dto.InputChannel,
6382
InputTopic: fromNullString(dto.InputTopic),
6483
Logic: re.Script{
@@ -77,7 +96,7 @@ func dbToRule(dto dbRule) re.Rule {
7796
CreatedBy: dto.CreatedBy,
7897
UpdatedAt: dto.UpdatedAt,
7998
UpdatedBy: dto.UpdatedBy,
80-
}
99+
}, nil
81100
}
82101

83102
func toNullString(value string) sql.NullString {

re/service.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ import (
1515
lua "github.com/yuin/gopher-lua"
1616
)
1717

18-
type ScriptType uint
19-
20-
type Script struct {
21-
Type ScriptType `json:"type"`
22-
Value string `json:"value"`
23-
}
18+
type (
19+
ScriptType uint
20+
Metadata map[string]interface{}
21+
Script struct {
22+
Type ScriptType `json:"type"`
23+
Value string `json:"value"`
24+
}
25+
)
2426

2527
// Type can be daily, weekly or monthly.
2628
type ReccuringType uint
@@ -42,6 +44,7 @@ type Rule struct {
4244
ID string `json:"id"`
4345
Name string `json:"name"`
4446
DomainID string `json:"domain"`
47+
Metadata Metadata `json:"metadata,omitempty"`
4548
InputChannel string `json:"input_channel"`
4649
InputTopic string `json:"input_topic"`
4750
Logic Script `json:"logic"`

0 commit comments

Comments
 (0)