@@ -16,6 +16,7 @@ import (
16
16
"sync"
17
17
"syscall"
18
18
"time"
19
+ "crypto/sha256"
19
20
20
21
"github.com/aws/aws-sdk-go/aws"
21
22
"github.com/aws/aws-sdk-go/aws/credentials"
@@ -364,6 +365,7 @@ func storeProtobufLocally(basePath string, payload *protos.Payload) error {
364
365
func storeProtobufInPostgres (db * sql.DB , payload * protos.Payload ) error {
365
366
vin := payload .Vin
366
367
createdAt := payload .CreatedAt .AsTime ().UTC ()
368
+
367
369
marshaller := protojson.MarshalOptions {
368
370
UseProtoNames : true ,
369
371
EmitUnpopulated : true ,
@@ -373,14 +375,23 @@ func storeProtobufInPostgres(db *sql.DB, payload *protos.Payload) error {
373
375
return fmt .Errorf ("failed to marshal protobuf to JSON: %w" , err )
374
376
}
375
377
376
- // Insert into PostgreSQL
377
- query := `INSERT INTO telemetry_data (vin, created_at, data) VALUES ($1, $2, $3) ON CONFLICT (vin, created_at) DO UPDATE SET data = EXCLUDED.data`
378
- _ , err = db .Exec (query , vin , createdAt , jsonData )
378
+ // Compute data_hash using SHA256
379
+ hash := sha256 .Sum256 (jsonData )
380
+ dataHash := fmt .Sprintf ("%x" , hash )
381
+
382
+ // Insert into PostgreSQL including data_hash
383
+ query := `
384
+ INSERT INTO telemetry_data (vin, created_at, data, data_hash)
385
+ VALUES ($1, $2, $3, $4)
386
+ ON CONFLICT (vin, created_at, data_hash)
387
+ DO NOTHING;
388
+ `
389
+ _ , err = db .Exec (query , vin , createdAt , jsonData , dataHash )
379
390
if err != nil {
380
391
return fmt .Errorf ("failed to insert into PostgreSQL: %w" , err )
381
392
}
382
393
383
- // ** Log successful insertion into PostgreSQL**
394
+ // Log successful insertion into PostgreSQL
384
395
log .Printf ("Inserted data into PostgreSQL for VIN:%s CreatedAt:%s" , vin , createdAt .String ())
385
396
386
397
return nil
0 commit comments