diff --git a/db/migrations/1536615440_create_deal_table.up.sql b/db/migrations/1536615440_create_deal_table.up.sql index 2950bb53..99d80ae9 100644 --- a/db/migrations/1536615440_create_deal_table.up.sql +++ b/db/migrations/1536615440_create_deal_table.up.sql @@ -1,8 +1,10 @@ CREATE TABLE maker.deal ( - id SERIAL PRIMARY KEY, - header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE, - bid_id NUMERIC NOT NULL, - tx_idx INTEGER NOT NUll, - raw_log JSONB, - UNIQUE (header_id, tx_idx) + id SERIAL PRIMARY KEY, + header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE, + bid_id NUMERIC NOT NULL, + contract_address VARCHAR, + log_idx INTEGER NOT NUll, + tx_idx INTEGER NOT NUll, + raw_log JSONB, + UNIQUE (header_id, tx_idx, log_idx) ); \ No newline at end of file diff --git a/db/migrations/1537814159_add_contract_address_to_deal.down.sql b/db/migrations/1537814159_add_contract_address_to_deal.down.sql deleted file mode 100644 index f7ee591b..00000000 --- a/db/migrations/1537814159_add_contract_address_to_deal.down.sql +++ /dev/null @@ -1,2 +0,0 @@ -ALTER TABLE maker.deal - DROP COLUMN contract_address; diff --git a/db/migrations/1537814159_add_contract_address_to_deal.up.sql b/db/migrations/1537814159_add_contract_address_to_deal.up.sql deleted file mode 100644 index 979be471..00000000 --- a/db/migrations/1537814159_add_contract_address_to_deal.up.sql +++ /dev/null @@ -1,2 +0,0 @@ -ALTER TABLE maker.deal - ADD COLUMN contract_address VARCHAR; diff --git a/db/schema.sql b/db/schema.sql index b079c817..6faa1e0f 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -209,9 +209,10 @@ CREATE TABLE maker.deal ( id integer NOT NULL, header_id integer NOT NULL, bid_id numeric NOT NULL, + contract_address character varying, + log_idx integer NOT NULL, tx_idx integer NOT NULL, - raw_log jsonb, - contract_address character varying + raw_log jsonb ); @@ -1772,11 +1773,11 @@ ALTER TABLE ONLY maker.cat_file_pit_vow -- --- Name: deal deal_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - +-- Name: deal deal_header_id_tx_idx_log_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - -- ALTER TABLE ONLY maker.deal - ADD CONSTRAINT deal_header_id_tx_idx_key UNIQUE (header_id, tx_idx); + ADD CONSTRAINT deal_header_id_tx_idx_log_idx_key UNIQUE (header_id, tx_idx, log_idx); -- diff --git a/pkg/transformers/deal/converter.go b/pkg/transformers/deal/converter.go index b277ab79..5e4505d9 100644 --- a/pkg/transformers/deal/converter.go +++ b/pkg/transformers/deal/converter.go @@ -47,6 +47,7 @@ func (DealConverter) ToModels(ethLogs []types.Log) (result []DealModel, err erro model := DealModel{ BidId: bidId.String(), ContractAddress: log.Address.Hex(), + LogIndex: log.Index, TransactionIndex: log.TxIndex, Raw: raw, } diff --git a/pkg/transformers/deal/model.go b/pkg/transformers/deal/model.go index acb4d996..cbfb194d 100644 --- a/pkg/transformers/deal/model.go +++ b/pkg/transformers/deal/model.go @@ -17,6 +17,7 @@ package deal type DealModel struct { BidId string `db:"bid_id"` ContractAddress string `db:"contract_address"` + LogIndex uint `db:"log_idx"` TransactionIndex uint `db:"tx_idx"` Raw []byte `db:"raw_log"` } diff --git a/pkg/transformers/deal/repository.go b/pkg/transformers/deal/repository.go index 993a805f..8845af89 100644 --- a/pkg/transformers/deal/repository.go +++ b/pkg/transformers/deal/repository.go @@ -38,9 +38,9 @@ func (r DealRepository) Create(headerId int64, models []DealModel) error { } for _, model := range models { _, err = tx.Exec( - `INSERT into maker.deal (header_id, bid_id, contract_address, tx_idx, raw_log) - VALUES($1, $2, $3, $4, $5)`, - headerId, model.BidId, model.ContractAddress, model.TransactionIndex, model.Raw, + `INSERT into maker.deal (header_id, bid_id, contract_address, log_idx, tx_idx, raw_log) + VALUES($1, $2, $3, $4, $5, $6)`, + headerId, model.BidId, model.ContractAddress, model.LogIndex, model.TransactionIndex, model.Raw, ) if err != nil { tx.Rollback() diff --git a/pkg/transformers/deal/repository_test.go b/pkg/transformers/deal/repository_test.go index 1338ab4b..9848253b 100644 --- a/pkg/transformers/deal/repository_test.go +++ b/pkg/transformers/deal/repository_test.go @@ -57,10 +57,11 @@ var _ = Describe("Deal Repository", func() { db.QueryRow(`SELECT count(*) FROM maker.deal`).Scan(&count) Expect(count).To(Equal(1)) var dbResult deal.DealModel - err = db.Get(&dbResult, `SELECT bid_id, contract_address, tx_idx, raw_log FROM maker.deal WHERE header_id = $1`, headerId) + err = db.Get(&dbResult, `SELECT bid_id, contract_address, log_idx, tx_idx, raw_log FROM maker.deal WHERE header_id = $1`, headerId) Expect(err).NotTo(HaveOccurred()) Expect(dbResult.BidId).To(Equal(test_data.DealModel.BidId)) Expect(dbResult.ContractAddress).To(Equal(test_data.DealModel.ContractAddress)) + Expect(dbResult.LogIndex).To(Equal(test_data.DealModel.LogIndex)) Expect(dbResult.TransactionIndex).To(Equal(test_data.DealModel.TransactionIndex)) Expect(dbResult.Raw).To(MatchJSON(test_data.DealModel.Raw)) }) diff --git a/pkg/transformers/test_data/deal.go b/pkg/transformers/test_data/deal.go index c0a8862d..e3d1bff2 100644 --- a/pkg/transformers/test_data/deal.go +++ b/pkg/transformers/test_data/deal.go @@ -36,7 +36,7 @@ var DealLogNote = types.Log{ TxHash: common.HexToHash("0xc6ff19de9299e5b290ba2d52fdb4662360ca86376613d78ee546244866a0be2d"), TxIndex: 74, BlockHash: common.HexToHash("0x6454844075164a1d264c86d2a2c31ac1b64eb2f4ebdbbaeb4d44388fdf74470b"), - Index: 0, + Index: 75, Removed: false, } var dealRawJson, _ = json.Marshal(DealLogNote) @@ -44,6 +44,7 @@ var dealRawJson, _ = json.Marshal(DealLogNote) var DealModel = deal.DealModel{ BidId: "123", ContractAddress: shared.FlipperContractAddress, - TransactionIndex: 74, + LogIndex: DealLogNote.Index, + TransactionIndex: DealLogNote.TxIndex, Raw: dealRawJson, }