Skip to content

Commit

Permalink
672: Manually migrate the db schema to support the sent message ID
Browse files Browse the repository at this point in the history
  • Loading branch information
halprin committed Jan 8, 2024
1 parent a4eb334 commit 0ed7ea5
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/terraform-deploy_reusable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jobs:
- name: Run Db migration
run: |
export PGPASSWORD=$(az account get-access-token --resource-type oss-rdbms --query "[accessToken]" -o tsv)
psql "host=$(terraform output -raw database_hostname) port=5432 dbname=postgres user=cdcti-github sslmode=require" -c "CREATE TABLE IF NOT EXISTS metadata (message_id varchar(30) PRIMARY KEY, sender varchar(30), receiver varchar(30), hash_of_order varchar(1000), time_received timestamptz); GRANT ALL ON metadata TO azure_pg_admin; ALTER TABLE metadata OWNER TO azure_pg_admin;"
psql "host=$(terraform output -raw database_hostname) port=5432 dbname=postgres user=cdcti-github sslmode=require" -c "CREATE TABLE IF NOT EXISTS metadata (received_message_id varchar(40) PRIMARY KEY, sent_message_id varchar(40), sender varchar(30), receiver varchar(30), hash_of_order varchar(1000), time_received timestamptz); GRANT ALL ON metadata TO azure_pg_admin; ALTER TABLE metadata OWNER TO azure_pg_admin;"
- id: export-terraform-output
name: Export Terraform Output
Expand Down
24 changes: 22 additions & 2 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
},
{
"path": "detect_secrets.filters.common.is_baseline_file",
"filename": ".secrets.baseline"
"filename": "./.secrets.baseline"
},
{
"path": "detect_secrets.filters.common.is_ignored_due_to_verification_policies",
Expand Down Expand Up @@ -142,6 +142,16 @@
"is_secret": false
}
],
"docker-compose.postgres.yml": [
{
"type": "Secret Keyword",
"filename": "docker-compose.postgres.yml",
"hashed_secret": "0f7866a6cab6f2793ea9f68e92935e4d726d58b5",
"is_verified": false,
"line_number": 11,
"is_secret": false
}
],
"mock_credentials/organization-report-stream-private-key-local.pem": [
{
"type": "Private Key",
Expand Down Expand Up @@ -212,6 +222,16 @@
"is_secret": false
}
],
"operations/template/db.tf": [
{
"type": "Secret Keyword",
"filename": "operations/template/db.tf",
"hashed_secret": "7cb6efb98ba5972a9b5090dc2e517fe14d12cb04",
"is_verified": false,
"line_number": 15,
"is_secret": false
}
],
"shared/src/main/java/gov/hhs/cdc/trustedintermediary/external/jjwt/JjwtEngine.java": [
{
"type": "Private Key",
Expand All @@ -233,5 +253,5 @@
}
]
},
"generated_at": "2023-11-30T22:27:41Z"
"generated_at": "2024-01-08T18:22:05Z"
}
2 changes: 2 additions & 0 deletions docker-compose.postgres.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Find the SQL schema in `/.github/workflows/terraform-deploy_reusable.yml`.

version: "3.7"

services:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void saveMetadata(final PartnerMetadata metadata) throws PartnerMetadataE
try {
dao.upsertMetadata(
metadata.receivedSubmissionId(),
metadata.sentSubmissionId(),
metadata.sender(),
metadata.receiver(),
metadata.hash(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public static PostgresDao getInstance() {
@Override
public synchronized void upsertMetadata(
String receivedSubmissionId,
String sentSubmissionId,
String sender,
String receiver,
String hash,
Expand All @@ -92,15 +93,16 @@ public synchronized void upsertMetadata(
PreparedStatement statement =
conn.prepareStatement(
"""
INSERT INTO metadata VALUES (?, ?, ?, ?, ?)
ON CONFLICT (message_id) DO UPDATE SET receiver = EXCLUDED.receiver
INSERT INTO metadata VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT (received_message_id) DO UPDATE SET receiver = EXCLUDED.receiver, sent_message_id = EXCLUDED.sent_message_id
""")) {

statement.setString(1, receivedSubmissionId);
statement.setString(2, sender);
statement.setString(3, receiver);
statement.setString(4, hash);
statement.setTimestamp(5, Timestamp.from(timeReceived));
statement.setString(2, sentSubmissionId);
statement.setString(3, sender);
statement.setString(4, receiver);
statement.setString(5, hash);
statement.setTimestamp(6, Timestamp.from(timeReceived));

statement.executeUpdate();
}
Expand All @@ -111,7 +113,8 @@ public synchronized PartnerMetadata fetchMetadata(String receivedSubmissionId)
throws SQLException {
try (Connection conn = connect();
PreparedStatement statement =
conn.prepareStatement("SELECT * FROM metadata where message_id = ?")) {
conn.prepareStatement(
"SELECT * FROM metadata where received_message_id = ?")) {

statement.setString(1, receivedSubmissionId);

Expand All @@ -123,7 +126,8 @@ public synchronized PartnerMetadata fetchMetadata(String receivedSubmissionId)
}

return new PartnerMetadata(
result.getString("message_id"),
result.getString("received_message_id"),
result.getString("sent_message_id"),
result.getString("sender"),
result.getString("receiver"),
result.getTimestamp("time_received").toInstant(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,5 +297,4 @@ class HapiOrderConverterTest extends Specification {

return patient
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
/** Interface for accessing the database for metadata */
public interface DbDao {
void upsertMetadata(
String id, String sender, String receiver, String hash, Instant timeReceived)
String receivedId,
String sentId,
String sender,
String receiver,
String hash,
Instant timeReceived)
throws SQLException;

Object fetchMetadata(String uniqueId) throws SQLException;
Expand Down

0 comments on commit 0ed7ea5

Please sign in to comment.