Skip to content

Commit 00fdc5b

Browse files
Merge pull request #839 from CrowleyRajapakse/main
Fixing postgres db related flows for api channel related flows
2 parents cb67517 + c34b91a commit 00fdc5b

2 files changed

Lines changed: 37 additions & 13 deletions

File tree

platform-api/src/internal/database/schema.postgres.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ CREATE TABLE IF NOT EXISTS apis (
6262
UNIQUE(name, version, organization_uuid)
6363
);
6464

65+
-- XHub Signature Security Configuration table
66+
CREATE TABLE IF NOT EXISTS xhub_signature_security (
67+
id SERIAL PRIMARY KEY,
68+
api_uuid VARCHAR(40) NOT NULL,
69+
enabled BOOLEAN,
70+
header VARCHAR(255),
71+
algorithm VARCHAR(50),
72+
secret VARCHAR(255),
73+
FOREIGN KEY (api_uuid) REFERENCES apis(uuid) ON DELETE CASCADE
74+
);
75+
6576
-- API MTLS Configuration table
6677
CREATE TABLE IF NOT EXISTS api_mtls_config (
6778
id SERIAL PRIMARY KEY,

platform-api/src/internal/repository/api.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ func (r *APIRepo) insertSecurityConfig(tx *sql.Tx, apiId string, security *model
638638
INSERT INTO xhub_signature_security (api_uuid, enabled, secret, algorithm, header)
639639
VALUES (?, ?, ?, ?, ?)
640640
`
641-
_, err := tx.Exec(xHubQuery, apiId, security.XHubSignature.Enabled,
641+
_, err := tx.Exec(r.db.Rebind(xHubQuery), apiId, security.XHubSignature.Enabled,
642642
security.XHubSignature.Secret, security.XHubSignature.Algorithm, security.XHubSignature.Header)
643643
if err != nil {
644644
return err
@@ -670,7 +670,7 @@ func (r *APIRepo) loadSecurityConfig(apiId string) (*model.SecurityConfig, error
670670
SELECT enabled, secret, algorithm, header
671671
FROM xhub_signature_security WHERE api_uuid = ?
672672
`
673-
err = r.db.QueryRow(xHubQuery, apiId).Scan(&xHub.Enabled,
673+
err = r.db.QueryRow(r.db.Rebind(xHubQuery), apiId).Scan(&xHub.Enabled,
674674
&xHub.Secret, &xHub.Algorithm, &xHub.Header)
675675
if err == nil {
676676
security.XHubSignature = xHub
@@ -882,19 +882,32 @@ func (r *APIRepo) insertChannel(tx *sql.Tx, apiId string, channel *model.Channel
882882
}
883883
}
884884
// Insert channel
885-
channelQuery := `
885+
var channelID int64
886+
if r.db.Driver() == "postgres" || r.db.Driver() == "postgresql" {
887+
// PostgreSQL: use RETURNING to get the generated ID
888+
channelQuery := `
889+
INSERT INTO api_operations (api_uuid, name, description, method, path, authentication_required, scopes)
890+
VALUES (?, ?, ?, ?, ?, ?, ?)
891+
RETURNING id`
892+
if err := tx.QueryRow(r.db.Rebind(channelQuery), apiId, channel.Name, channel.Description,
893+
channel.Request.Method, channel.Request.Name, authRequired, scopesJSON).Scan(&channelID); err != nil {
894+
return err
895+
}
896+
} else {
897+
// SQLite (and other drivers that support LastInsertId)
898+
channelQuery := `
886899
INSERT INTO api_operations (api_uuid, name, description, method, path, authentication_required, scopes)
887900
VALUES (?, ?, ?, ?, ?, ?, ?)`
888-
result, err := tx.Exec(channelQuery, apiId, channel.Name, channel.Description,
889-
channel.Request.Method, channel.Request.Name, authRequired, scopesJSON)
890-
891-
if err != nil {
892-
return err
893-
}
901+
result, err := tx.Exec(r.db.Rebind(channelQuery), apiId, channel.Name, channel.Description,
902+
channel.Request.Method, channel.Request.Name, authRequired, scopesJSON)
903+
if err != nil {
904+
return err
905+
}
894906

895-
channelID, err := result.LastInsertId()
896-
if err != nil {
897-
return err
907+
channelID, err = result.LastInsertId()
908+
if err != nil {
909+
return err
910+
}
898911
}
899912

900913
// Insert policies
@@ -927,7 +940,7 @@ func (r *APIRepo) loadChannels(apiId string) ([]model.Channel, error) {
927940
SELECT id, name, description, method, path, authentication_required, scopes
928941
FROM api_operations WHERE api_uuid = ?
929942
`
930-
rows, err := r.db.Query(query, apiId)
943+
rows, err := r.db.Query(r.db.Rebind(query), apiId)
931944
if err != nil {
932945
return nil, err
933946
}

0 commit comments

Comments
 (0)