MCPcopy Create free account
hub / github.com/devaccuracy/ledgerforge / recordTransactionInTx

Function recordTransactionInTx

database/transaction.go:84–109  ·  view source on GitHub ↗

recordTransactionInTx inserts a transaction record within an existing database transaction. This is a helper function used by RecordTransactionWithBalances for atomic operations.

(ctx context.Context, tx *sql.Tx, txn *model.Transaction)

Source from the content-addressed store, hash-verified

82// recordTransactionInTx inserts a transaction record within an existing database transaction.
83// This is a helper function used by RecordTransactionWithBalances for atomic operations.
84func recordTransactionInTx(ctx context.Context, tx *sql.Tx, txn *model.Transaction) error {
85 ctx, span := otel.Tracer("transaction.database").Start(ctx, "recordTransactionInTx")
86 defer span.End()
87
88 metaDataJSON, err := json.Marshal(txn.MetaData)
89 if err != nil {
90 span.RecordError(err)
91 return apierror.NewAPIError(apierror.ErrInternalServer, "Failed to marshal metadata", err)
92 }
93
94 _, err = tx.ExecContext(ctx,
95 `INSERT INTO ledgerforge.transactions(transaction_id, parent_transaction, source, reference, amount, precise_amount, precision, rate, currency, destination, description, status, created_at, meta_data, scheduled_for, hash, effective_date)
96 VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)`,
97 txn.TransactionID, txn.ParentTransaction, txn.Source, txn.Reference, txn.AmountString, txn.PreciseAmount.String(), txn.Precision, txn.Rate, txn.Currency, txn.Destination, txn.Description, txn.Status, txn.CreatedAt, metaDataJSON, txn.ScheduledFor, txn.Hash, txn.EffectiveDate,
98 )
99 if err != nil {
100 span.RecordError(err)
101 return apierror.NewAPIError(apierror.ErrInternalServer, "Failed to record transaction", err)
102 }
103
104 span.AddEvent("Transaction recorded in transaction", trace.WithAttributes(
105 attribute.String("transaction.id", txn.TransactionID),
106 ))
107
108 return nil
109}
110
111// recordTransactionsInTx inserts multiple transaction records within an existing database
112// transaction using PostgreSQL's COPY protocol to reduce SQL parsing and roundtrips.

Calls 2

NewAPIErrorFunction · 0.92
StartMethod · 0.45

Tested by

no test coverage detected