Skip to content

Commit 4ff1172

Browse files
feat: Add core-data event related db methods for postgres (edgexfoundry#4855)
* feat: Add core-data event related db methods for postgres Relates to edgexfoundry#4847. Add core-data event related db methods for postgres db client. Signed-off-by: Lindsey Cheng <[email protected]> * fix: Define json query string as query argument Define json query string as query argument instead of in sql statement. Signed-off-by: Lindsey Cheng <[email protected]> * fix: Update core-data event table design Update event design based on the event struct fields to have multi columns. Signed-off-by: Lindsey Cheng <[email protected]> --------- Signed-off-by: Lindsey Cheng <[email protected]>
1 parent 5b52836 commit 4ff1172

File tree

11 files changed

+447
-20
lines changed

11 files changed

+447
-20
lines changed

cmd/core-data/res/db/sql/00-utils.sql

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--
2+
-- Copyright (C) 2024 IOTech Ltd
3+
--
4+
-- SPDX-License-Identifier: Apache-2.0
5+
6+
-- schema for core-data related tables
7+
CREATE SCHEMA IF NOT EXISTS core_data;
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--
2+
-- Copyright (C) 2024 IOTech Ltd
3+
--
4+
-- SPDX-License-Identifier: Apache-2.0
5+
6+
-- core_data.event is used to store the event information
7+
CREATE TABLE IF NOT EXISTS core_data.event (
8+
id UUID PRIMARY KEY,
9+
devicename TEXT,
10+
profilename TEXT,
11+
sourcename TEXT,
12+
origin BIGINT,
13+
tags JSONB
14+
);

cmd/security-secretstore-setup/res/configuration.yaml

+7-7
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,25 @@ Databases:
4949
Username: admin
5050
command:
5151
Service: core-command
52-
Username: core-command
52+
Username: core_command
5353
metadata:
5454
Service: core-metadata
55-
Username: core-metadata
55+
Username: core_metadata
5656
coredata:
5757
Service: core-data
58-
Username: core-data
58+
Username: core_data
5959
corekeeper:
6060
Service: core-keeper
61-
Username: core-keeper
61+
Username: core_keeper
6262
rulesengine:
6363
Service: app-rules-engine
64-
Username: app-rules-engine
64+
Username: app_rules_engine
6565
notifications:
6666
Service: support-notifications
67-
Username: support-notifications
67+
Username: support_notifications
6868
scheduler:
6969
Service: support-scheduler
70-
Username: support-scheduler
70+
Username: support_scheduler
7171
SecureMessageBus:
7272
Type: none
7373
KuiperConfigPath: /tmp/kuiper/edgex.yaml

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/go-co-op/gocron/v2 v2.11.0
1414
github.com/gomodule/redigo v1.9.2
1515
github.com/google/uuid v1.6.0
16+
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438
1617
github.com/jackc/pgx/v5 v5.6.0
1718
github.com/labstack/echo/v4 v4.11.4
1819
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
327327
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
328328
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
329329
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
330+
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438 h1:Dj0L5fhJ9F82ZJyVOmBx6msDp/kfd1t9GRfny/mfJA0=
331+
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438/go.mod h1:a/s9Lp5W7n/DD0VrVoyJ00FbP2ytTPDVOivvn2bMlds=
330332
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
331333
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
332334
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=

internal/pkg/bootstrap/handlers/database.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (C) 2020 IOTech Ltd
2+
// Copyright (C) 2020-2024 IOTech Ltd
33
//
44
// SPDX-License-Identifier: Apache-2.0
55

@@ -24,6 +24,12 @@ import (
2424
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger"
2525
)
2626

27+
const (
28+
baseScriptPath = "/res/db/sql"
29+
redisDBType = "redisdb"
30+
postgresDBType = "postgres"
31+
)
32+
2733
// httpServer defines the contract used to determine whether or not the http httpServer is running.
2834
type httpServer interface {
2935
IsRunning() bool
@@ -60,12 +66,12 @@ func (d Database) newDBClient(
6066
}
6167

6268
switch databaseInfo.Type {
63-
case "redisdb":
69+
case redisDBType:
6470
return redis.NewClient(databaseConfig, lc)
65-
case "postgres":
71+
case postgresDBType:
6672
databaseConfig.Username = credentials.Username
6773
// TODO: The baseScriptPath and extScriptPath should be passed in from the configuration file
68-
return postgres.NewClient(ctx, databaseConfig, "/res/db/sql", "", lc)
74+
return postgres.NewClient(ctx, databaseConfig, baseScriptPath, "", lc)
6975
default:
7076
return nil, db.ErrUnsupportedDatabase
7177
}
@@ -139,7 +145,7 @@ func (d Database) BootstrapHandler(
139145
},
140146
})
141147

142-
lc.Info("Database connected")
148+
lc.Infof("%s database connected", dbInfo.Type)
143149
wg.Add(1)
144150
go func() {
145151
defer wg.Done()
@@ -153,7 +159,7 @@ func (d Database) BootstrapHandler(
153159
}
154160
time.Sleep(time.Second)
155161
}
156-
lc.Info("Database disconnected")
162+
lc.Infof("%s database disconnected", dbInfo.Type)
157163
}()
158164

159165
return true

internal/pkg/db/postgres/client.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ func NewClient(ctx context.Context, config db.Configuration, baseScriptPath, ext
6363

6464
// execute base DB scripts
6565
if edgeXerr = executeDBScripts(ctx, dc.ConnPool, baseScriptPath); edgeXerr != nil {
66-
return nil, errors.NewCommonEdgeX(errors.Kind(edgeXerr), "failed to execute base DB scripts", edgeXerr)
66+
return nil, errors.NewCommonEdgeX(errors.Kind(edgeXerr), "failed to execute Postgres base DB scripts", edgeXerr)
6767
}
68-
lc.Info("successfully execute base DB scripts")
68+
lc.Info("successfully execute Postgres base DB scripts")
6969

7070
// execute extension DB scripts
7171
if edgeXerr = executeDBScripts(ctx, dc.ConnPool, extScriptPath); edgeXerr != nil {
72-
return nil, errors.NewCommonEdgeX(errors.Kind(edgeXerr), "failed to execute extension DB scripts", edgeXerr)
72+
return nil, errors.NewCommonEdgeX(errors.Kind(edgeXerr), "failed to execute Postgres extension DB scripts", edgeXerr)
7373
}
7474

7575
return dc, nil

internal/pkg/db/postgres/utils.go

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"regexp"
1515
"sort"
1616

17+
"github.com/jackc/pgerrcode"
1718
"github.com/jackc/pgx/v5/pgconn"
1819
"github.com/jackc/pgx/v5/pgxpool"
1920

@@ -128,6 +129,9 @@ func sortedSqlFileNames(sqlFilesDir string) ([]string, errors.EdgeX) {
128129
func WrapDBError(message string, err error) errors.EdgeX {
129130
var pgErr *pgconn.PgError
130131
if goErrors.As(err, &pgErr) {
132+
if pgerrcode.IsIntegrityConstraintViolation(pgErr.Code) {
133+
return errors.NewCommonEdgeX(errors.KindDuplicateName, pgErr.Detail, nil)
134+
}
131135
return errors.NewCommonEdgeX(errors.KindDatabaseError, fmt.Sprintf("%s: %s %s", message, pgErr.Error(), pgErr.Detail), nil)
132136
}
133137
return errors.NewCommonEdgeX(errors.KindDatabaseError, message, err)

0 commit comments

Comments
 (0)