From e5ba0496a6d8261b5152dbe38735757fd91d2cee Mon Sep 17 00:00:00 2001 From: Rob Mulholand Date: Fri, 19 Oct 2018 11:48:37 -0500 Subject: [PATCH] Add log index to Dent --- db/migrations/1534799167_create_dent_table.up.sql | 5 +++-- db/schema.sql | 5 +++-- pkg/transformers/dent/converter.go | 2 ++ pkg/transformers/dent/model.go | 1 + pkg/transformers/dent/repository.go | 6 +++--- pkg/transformers/dent/repository_test.go | 3 ++- pkg/transformers/test_data/dent.go | 1 + 7 files changed, 15 insertions(+), 8 deletions(-) diff --git a/db/migrations/1534799167_create_dent_table.up.sql b/db/migrations/1534799167_create_dent_table.up.sql index 77a9af17..133b366a 100644 --- a/db/migrations/1534799167_create_dent_table.up.sql +++ b/db/migrations/1534799167_create_dent_table.up.sql @@ -6,7 +6,8 @@ CREATE TABLE maker.dent ( bid NUMERIC, guy BYTEA, tic NUMERIC, - tx_idx INTEGER NOT NUll, + log_idx INTEGER NOT NUll, + tx_idx INTEGER NOT NUll, raw_log JSONB, - UNIQUE (header_id, tx_idx) + UNIQUE (header_id, tx_idx, log_idx) ); diff --git a/db/schema.sql b/db/schema.sql index 6faa1e0f..ae6f7182 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -248,6 +248,7 @@ CREATE TABLE maker.dent ( bid numeric, guy bytea, tic numeric, + log_idx integer NOT NULL, tx_idx integer NOT NULL, raw_log jsonb ); @@ -1789,11 +1790,11 @@ ALTER TABLE ONLY maker.deal -- --- Name: dent dent_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - +-- Name: dent dent_header_id_tx_idx_log_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - -- ALTER TABLE ONLY maker.dent - ADD CONSTRAINT dent_header_id_tx_idx_key UNIQUE (header_id, tx_idx); + ADD CONSTRAINT dent_header_id_tx_idx_log_idx_key UNIQUE (header_id, tx_idx, log_idx); -- diff --git a/pkg/transformers/dent/converter.go b/pkg/transformers/dent/converter.go index 801db498..d91b504a 100644 --- a/pkg/transformers/dent/converter.go +++ b/pkg/transformers/dent/converter.go @@ -48,6 +48,7 @@ func (c DentConverter) ToModels(ethLogs []types.Log) (result []DentModel, err er //TODO: it is likely that the tic value will need to be added to an emitted event, //so this will need to be updated at that point + logIndex := log.Index transactionIndex := log.TxIndex raw, err := json.Marshal(log) @@ -61,6 +62,7 @@ func (c DentConverter) ToModels(ethLogs []types.Log) (result []DentModel, err er Bid: bidValue, Guy: guy, Tic: tic, + LogIndex: logIndex, TransactionIndex: transactionIndex, Raw: raw, } diff --git a/pkg/transformers/dent/model.go b/pkg/transformers/dent/model.go index 8ee86de3..c6b4b37a 100644 --- a/pkg/transformers/dent/model.go +++ b/pkg/transformers/dent/model.go @@ -20,6 +20,7 @@ type DentModel struct { Bid string Guy string Tic string + LogIndex uint `db:"log_idx"` TransactionIndex uint `db:"tx_idx"` Raw []byte `db:"raw_log"` } diff --git a/pkg/transformers/dent/repository.go b/pkg/transformers/dent/repository.go index c4d8b2cd..7f765aa9 100644 --- a/pkg/transformers/dent/repository.go +++ b/pkg/transformers/dent/repository.go @@ -40,9 +40,9 @@ func (r DentRepository) Create(headerId int64, models []DentModel) error { } for _, model := range models { _, err = tx.Exec( - `INSERT into maker.dent (header_id, bid_id, lot, bid, guy, tic, tx_idx, raw_log) - VALUES($1, $2, $3, $4, $5, $6, $7, $8)`, - headerId, model.BidId, model.Lot, model.Bid, model.Guy, model.Tic, model.TransactionIndex, model.Raw, + `INSERT into maker.dent (header_id, bid_id, lot, bid, guy, tic, log_idx, tx_idx, raw_log) + VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9)`, + headerId, model.BidId, model.Lot, model.Bid, model.Guy, model.Tic, model.LogIndex, model.TransactionIndex, model.Raw, ) if err != nil { tx.Rollback() diff --git a/pkg/transformers/dent/repository_test.go b/pkg/transformers/dent/repository_test.go index f00eca5e..f46c44ce 100644 --- a/pkg/transformers/dent/repository_test.go +++ b/pkg/transformers/dent/repository_test.go @@ -59,13 +59,14 @@ var _ = Describe("Dent Repository", func() { Expect(count).To(Equal(1)) var dbResult dent.DentModel - err = db.Get(&dbResult, `SELECT bid_id, lot, bid, guy, tic, tx_idx, raw_log FROM maker.dent WHERE header_id = $1`, headerId) + err = db.Get(&dbResult, `SELECT bid_id, lot, bid, guy, tic, log_idx, tx_idx, raw_log FROM maker.dent WHERE header_id = $1`, headerId) Expect(err).NotTo(HaveOccurred()) Expect(dbResult.BidId).To(Equal(test_data.DentModel.BidId)) Expect(dbResult.Lot).To(Equal(test_data.DentModel.Lot)) Expect(dbResult.Bid).To(Equal(test_data.DentModel.Bid)) Expect(dbResult.Guy).To(Equal(test_data.DentModel.Guy)) Expect(dbResult.Tic).To(Equal(test_data.DentModel.Tic)) + Expect(dbResult.LogIndex).To(Equal(test_data.DentModel.LogIndex)) Expect(dbResult.TransactionIndex).To(Equal(test_data.DentModel.TransactionIndex)) Expect(dbResult.Raw).To(MatchJSON(test_data.DentModel.Raw)) }) diff --git a/pkg/transformers/test_data/dent.go b/pkg/transformers/test_data/dent.go index 8e36c063..8c59f0ca 100644 --- a/pkg/transformers/test_data/dent.go +++ b/pkg/transformers/test_data/dent.go @@ -59,6 +59,7 @@ var DentModel = dent.DentModel{ Bid: dentBid, Guy: dentGuy, Tic: DentTic, + LogIndex: DentLog.Index, TransactionIndex: DentLog.TxIndex, Raw: dentRawJson, }