@@ -16,6 +16,7 @@ import (
1616 "sync"
1717 "syscall"
1818 "time"
19+ "crypto/sha256"
1920
2021 "github.com/aws/aws-sdk-go/aws"
2122 "github.com/aws/aws-sdk-go/aws/credentials"
@@ -364,6 +365,7 @@ func storeProtobufLocally(basePath string, payload *protos.Payload) error {
364365func storeProtobufInPostgres (db * sql.DB , payload * protos.Payload ) error {
365366 vin := payload .Vin
366367 createdAt := payload .CreatedAt .AsTime ().UTC ()
368+
367369 marshaller := protojson.MarshalOptions {
368370 UseProtoNames : true ,
369371 EmitUnpopulated : true ,
@@ -373,14 +375,23 @@ func storeProtobufInPostgres(db *sql.DB, payload *protos.Payload) error {
373375 return fmt .Errorf ("failed to marshal protobuf to JSON: %w" , err )
374376 }
375377
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 )
379390 if err != nil {
380391 return fmt .Errorf ("failed to insert into PostgreSQL: %w" , err )
381392 }
382393
383- // ** Log successful insertion into PostgreSQL**
394+ // Log successful insertion into PostgreSQL
384395 log .Printf ("Inserted data into PostgreSQL for VIN:%s CreatedAt:%s" , vin , createdAt .String ())
385396
386397 return nil
0 commit comments