diff --git a/db/migrations/1534295712_create_tend_table.up.sql b/db/migrations/1534295712_create_tend_table.up.sql index a6dd18e8..3e833506 100644 --- a/db/migrations/1534295712_create_tend_table.up.sql +++ b/db/migrations/1534295712_create_tend_table.up.sql @@ -4,9 +4,10 @@ CREATE TABLE maker.tend ( bid_id NUMERIC NOT NULL UNIQUE, lot NUMERIC, bid NUMERIC, - guy VARCHAR, + guy TEXT, 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 dd14f1c0..0aa8ca4b 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -685,8 +685,9 @@ CREATE TABLE maker.tend ( bid_id numeric NOT NULL, lot numeric, bid numeric, - guy character varying, + guy text, tic numeric, + log_idx integer NOT NULL, tx_idx integer NOT NULL, raw_log jsonb ); @@ -1994,11 +1995,11 @@ ALTER TABLE ONLY maker.price_feeds -- --- Name: tend tend_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - +-- Name: tend tend_header_id_tx_idx_log_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - -- ALTER TABLE ONLY maker.tend - ADD CONSTRAINT tend_header_id_tx_idx_key UNIQUE (header_id, tx_idx); + ADD CONSTRAINT tend_header_id_tx_idx_log_idx_key UNIQUE (header_id, tx_idx, log_idx); -- diff --git a/pkg/transformers/tend/converter.go b/pkg/transformers/tend/converter.go index 52ace3c1..ba46ee9d 100644 --- a/pkg/transformers/tend/converter.go +++ b/pkg/transformers/tend/converter.go @@ -44,6 +44,7 @@ func (TendConverter) ToModels(ethLogs []types.Log) (results []interface{}, err e //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 transactionIndex := ethLog.TxIndex + logIndex := ethLog.Index rawJson, err := json.Marshal(ethLog) if err != nil { @@ -57,6 +58,7 @@ func (TendConverter) ToModels(ethLogs []types.Log) (results []interface{}, err e Bid: bidValue, Guy: guy, Tic: tic, + LogIndex: logIndex, TransactionIndex: transactionIndex, Raw: raw, } diff --git a/pkg/transformers/tend/model.go b/pkg/transformers/tend/model.go index 0361e08c..4a8c6b43 100644 --- a/pkg/transformers/tend/model.go +++ b/pkg/transformers/tend/model.go @@ -20,6 +20,7 @@ type TendModel struct { Bid string Guy string Tic string + LogIndex uint `db:"log_idx"` TransactionIndex uint `db:"tx_idx"` Raw string `db:"raw_log"` } diff --git a/pkg/transformers/tend/repository.go b/pkg/transformers/tend/repository.go index de1ef581..77228629 100644 --- a/pkg/transformers/tend/repository.go +++ b/pkg/transformers/tend/repository.go @@ -38,9 +38,9 @@ func (repository TendRepository) Create(headerId int64, models []interface{}) er } _, err = tx.Exec( - `INSERT into maker.tend (header_id, bid_id, lot, bid, guy, tic, tx_idx, raw_log) - VALUES($1, $2, $3, $4, $5, $6, $7, $8)`, - headerId, tend.BidId, tend.Lot, tend.Bid, tend.Guy, tend.Tic, tend.TransactionIndex, tend.Raw, + `INSERT into maker.tend (header_id, bid_id, lot, bid, guy, tic, log_idx, tx_idx, raw_log) + VALUES($1, $2, $3::NUMERIC, $4::NUMERIC, $5, $6::NUMERIC, $7, $8, $9)`, + headerId, tend.BidId, tend.Lot, tend.Bid, tend.Guy, tend.Tic, tend.LogIndex, tend.TransactionIndex, tend.Raw, ) if err != nil { diff --git a/pkg/transformers/tend/repository_test.go b/pkg/transformers/tend/repository_test.go index d9d2e7a3..4381cd04 100644 --- a/pkg/transformers/tend/repository_test.go +++ b/pkg/transformers/tend/repository_test.go @@ -50,19 +50,19 @@ var _ = Describe("TendRepository", func() { BeforeEach(func() { headerId, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) Expect(err).NotTo(HaveOccurred()) - - err = tendRepository.Create(headerId, []interface{}{test_data.TendModel}) - Expect(err).NotTo(HaveOccurred()) }) It("persists a tend record", func() { + err = tendRepository.Create(headerId, []interface{}{test_data.TendModel}) + + Expect(err).NotTo(HaveOccurred()) var count int err = db.QueryRow(`SELECT count(*) from maker.tend`).Scan(&count) Expect(err).NotTo(HaveOccurred()) Expect(count).To(Equal(1)) dbResult := tend.TendModel{} - err = db.Get(&dbResult, `SELECT bid_id, lot, bid, guy, tic, tx_idx, raw_log FROM maker.tend WHERE header_id = $1`, headerId) + err = db.Get(&dbResult, `SELECT bid_id, lot, bid, guy, tic, log_idx, tx_idx, raw_log FROM maker.tend WHERE header_id = $1`, headerId) Expect(err).NotTo(HaveOccurred()) Expect(dbResult.BidId).To(Equal(test_data.TendModel.BidId)) @@ -70,11 +70,28 @@ var _ = Describe("TendRepository", func() { Expect(dbResult.Bid).To(Equal(test_data.TendModel.Bid)) Expect(dbResult.Guy).To(Equal(test_data.TendModel.Guy)) Expect(dbResult.Tic).To(Equal(test_data.TendModel.Tic)) + Expect(dbResult.LogIndex).To(Equal(test_data.TendModel.LogIndex)) Expect(dbResult.TransactionIndex).To(Equal(test_data.TendModel.TransactionIndex)) Expect(dbResult.Raw).To(MatchJSON(test_data.RawLogNoteJson)) }) It("marks header as checked", func() { + err = tendRepository.Create(headerId, []interface{}{test_data.TendModel}) + + Expect(err).NotTo(HaveOccurred()) + var headerChecked bool + err = db.Get(&headerChecked, `SELECT tend_checked FROM public.checked_headers WHERE header_id = $1`, headerId) + Expect(err).NotTo(HaveOccurred()) + Expect(headerChecked).To(BeTrue()) + }) + + It("updates the header to checked if checked headers row already exists", func() { + _, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerId) + Expect(err).NotTo(HaveOccurred()) + + err = tendRepository.Create(headerId, []interface{}{test_data.TendModel}) + + Expect(err).NotTo(HaveOccurred()) var headerChecked bool err = db.Get(&headerChecked, `SELECT tend_checked FROM public.checked_headers WHERE header_id = $1`, headerId) Expect(err).NotTo(HaveOccurred()) @@ -83,11 +100,17 @@ var _ = Describe("TendRepository", func() { It("returns an error if inserting a tend record fails", func() { err = tendRepository.Create(headerId, []interface{}{test_data.TendModel}) + Expect(err).NotTo(HaveOccurred()) + + err = tendRepository.Create(headerId, []interface{}{test_data.TendModel}) + Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) }) It("deletes the tend record if its corresponding header record is deleted", func() { + err = tendRepository.Create(headerId, []interface{}{test_data.TendModel}) + Expect(err).NotTo(HaveOccurred()) var count int err = db.QueryRow(`SELECT count(*) from maker.tend`).Scan(&count) Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/transformers/test_data/tend.go b/pkg/transformers/test_data/tend.go index 9f82d113..cbc8f2a8 100644 --- a/pkg/transformers/test_data/tend.go +++ b/pkg/transformers/test_data/tend.go @@ -61,6 +61,7 @@ var TendModel = tend.TendModel{ Bid: tendBid, Guy: tendGuy, Tic: TendTic, + LogIndex: TendLogNote.Index, TransactionIndex: TendLogNote.TxIndex, Raw: string(RawLogNoteJson), }