Skip to content

Commit

Permalink
feat: add index on postings (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag authored and flemzord committed May 12, 2023
1 parent 0a90c9a commit 3afcd9a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ CREATE TABLE IF NOT EXISTS "VAR_LEDGER_NAME_v2_0_0".postings (
amount bigint not null,
asset varchar not null,
source jsonb not null,
destination jsonb not null
destination jsonb not null,
index int8,

primary key (txid, index)
);

--statement
Expand Down
11 changes: 4 additions & 7 deletions pkg/storage/sqlstorage/ledger/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type Posting struct {
Asset string `bun:"asset,type:string"`
Source account `bun:"source,type:jsonb"`
Destination account `bun:"destination,type:jsonb"`
Index uint8 `bun:"index,type:int8"`
}

func (p Posting) toCore() core.Posting {
Expand Down Expand Up @@ -258,13 +259,14 @@ func (s *Store) insertTransactions(ctx context.Context, txs ...core.ExpandedTran
ts[i].Reference = cp
}

for _, p := range tx.Postings {
for i, p := range tx.Postings {
ps = append(ps, Posting{
TransactionID: tx.ID,
Amount: (*bunbig.Int)(p.Amount),
Asset: p.Asset,
Source: account(p.Source),
Destination: account(p.Destination),
Index: uint8(i),
})
}
}
Expand All @@ -279,12 +281,7 @@ func (s *Store) insertTransactions(ctx context.Context, txs ...core.ExpandedTran

_, err = s.schema.NewInsert(PostingsTableName).
Model(&ps).
// TODO(polo/gfyrag): Current postings table does not have
// unique indexes in txid and posting_index. It means that if we insert
// a posting with same txid and same posting index, it will be
// duplicated. We should fix this in the future.
// Why this index was removed ?
// On("CONFLICT (txid, posting_index) DO NOTHING").
On("CONFLICT (txid, index) DO NOTHING").
Exec(ctx)

return storageerrors.PostgresError(err)
Expand Down

0 comments on commit 3afcd9a

Please sign in to comment.