Skip to content

Commit

Permalink
feat: Add core-data event related db methods for postgres (#4855)
Browse files Browse the repository at this point in the history
* feat: Add core-data event related db methods for postgres

Relates to #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]>
  • Loading branch information
lindseysimple authored Aug 12, 2024
1 parent 5b52836 commit 4ff1172
Show file tree
Hide file tree
Showing 11 changed files with 447 additions and 20 deletions.
7 changes: 7 additions & 0 deletions cmd/core-data/res/db/sql/00-utils.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--
-- Copyright (C) 2024 IOTech Ltd
--
-- SPDX-License-Identifier: Apache-2.0

-- schema for core-data related tables
CREATE SCHEMA IF NOT EXISTS core_data;
14 changes: 14 additions & 0 deletions cmd/core-data/res/db/sql/01-tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--
-- Copyright (C) 2024 IOTech Ltd
--
-- SPDX-License-Identifier: Apache-2.0

-- core_data.event is used to store the event information
CREATE TABLE IF NOT EXISTS core_data.event (
id UUID PRIMARY KEY,
devicename TEXT,
profilename TEXT,
sourcename TEXT,
origin BIGINT,
tags JSONB
);
14 changes: 7 additions & 7 deletions cmd/security-secretstore-setup/res/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,25 @@ Databases:
Username: admin
command:
Service: core-command
Username: core-command
Username: core_command
metadata:
Service: core-metadata
Username: core-metadata
Username: core_metadata
coredata:
Service: core-data
Username: core-data
Username: core_data
corekeeper:
Service: core-keeper
Username: core-keeper
Username: core_keeper
rulesengine:
Service: app-rules-engine
Username: app-rules-engine
Username: app_rules_engine
notifications:
Service: support-notifications
Username: support-notifications
Username: support_notifications
scheduler:
Service: support-scheduler
Username: support-scheduler
Username: support_scheduler
SecureMessageBus:
Type: none
KuiperConfigPath: /tmp/kuiper/edgex.yaml
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/go-co-op/gocron/v2 v2.11.0
github.com/gomodule/redigo v1.9.2
github.com/google/uuid v1.6.0
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438
github.com/jackc/pgx/v5 v5.6.0
github.com/labstack/echo/v4 v4.11.4
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438 h1:Dj0L5fhJ9F82ZJyVOmBx6msDp/kfd1t9GRfny/mfJA0=
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438/go.mod h1:a/s9Lp5W7n/DD0VrVoyJ00FbP2ytTPDVOivvn2bMlds=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
Expand Down
18 changes: 12 additions & 6 deletions internal/pkg/bootstrap/handlers/database.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (C) 2020 IOTech Ltd
// Copyright (C) 2020-2024 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

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

const (
baseScriptPath = "/res/db/sql"
redisDBType = "redisdb"
postgresDBType = "postgres"
)

// httpServer defines the contract used to determine whether or not the http httpServer is running.
type httpServer interface {
IsRunning() bool
Expand Down Expand Up @@ -60,12 +66,12 @@ func (d Database) newDBClient(
}

switch databaseInfo.Type {
case "redisdb":
case redisDBType:
return redis.NewClient(databaseConfig, lc)
case "postgres":
case postgresDBType:
databaseConfig.Username = credentials.Username
// TODO: The baseScriptPath and extScriptPath should be passed in from the configuration file
return postgres.NewClient(ctx, databaseConfig, "/res/db/sql", "", lc)
return postgres.NewClient(ctx, databaseConfig, baseScriptPath, "", lc)
default:
return nil, db.ErrUnsupportedDatabase
}
Expand Down Expand Up @@ -139,7 +145,7 @@ func (d Database) BootstrapHandler(
},
})

lc.Info("Database connected")
lc.Infof("%s database connected", dbInfo.Type)
wg.Add(1)
go func() {
defer wg.Done()
Expand All @@ -153,7 +159,7 @@ func (d Database) BootstrapHandler(
}
time.Sleep(time.Second)
}
lc.Info("Database disconnected")
lc.Infof("%s database disconnected", dbInfo.Type)
}()

return true
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/db/postgres/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ func NewClient(ctx context.Context, config db.Configuration, baseScriptPath, ext

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

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

return dc, nil
Expand Down
4 changes: 4 additions & 0 deletions internal/pkg/db/postgres/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"regexp"
"sort"

"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"

Expand Down Expand Up @@ -128,6 +129,9 @@ func sortedSqlFileNames(sqlFilesDir string) ([]string, errors.EdgeX) {
func WrapDBError(message string, err error) errors.EdgeX {
var pgErr *pgconn.PgError
if goErrors.As(err, &pgErr) {
if pgerrcode.IsIntegrityConstraintViolation(pgErr.Code) {
return errors.NewCommonEdgeX(errors.KindDuplicateName, pgErr.Detail, nil)
}
return errors.NewCommonEdgeX(errors.KindDatabaseError, fmt.Sprintf("%s: %s %s", message, pgErr.Error(), pgErr.Detail), nil)
}
return errors.NewCommonEdgeX(errors.KindDatabaseError, message, err)
Expand Down
Loading

0 comments on commit 4ff1172

Please sign in to comment.