diff --git a/db/migrations/1538510582_create_vat_fold_table.up.sql b/db/migrations/1538510582_create_vat_fold_table.up.sql index ad5ca113..4076ec0d 100644 --- a/db/migrations/1538510582_create_vat_fold_table.up.sql +++ b/db/migrations/1538510582_create_vat_fold_table.up.sql @@ -1,12 +1,13 @@ CREATE TABLE maker.vat_fold ( - id SERIAL PRIMARY KEY, - header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE, - ilk text, - urn text, - rate numeric, - 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, + ilk TEXT, + urn TEXT, + rate NUMERIC, + log_idx INTEGER NOT NULL, + tx_idx INTEGER NOT NULL, + raw_log JSONB, + UNIQUE (header_id, tx_idx, log_idx) ); ALTER TABLE public.checked_headers diff --git a/db/schema.sql b/db/schema.sql index 0aa8ca4b..95109c5b 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -759,6 +759,7 @@ CREATE TABLE maker.vat_fold ( ilk text, urn text, rate numeric, + log_idx integer NOT NULL, tx_idx integer NOT NULL, raw_log jsonb ); @@ -2027,11 +2028,11 @@ ALTER TABLE ONLY maker.vat_flux -- --- Name: vat_fold vat_fold_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - +-- Name: vat_fold vat_fold_header_id_tx_idx_log_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - -- ALTER TABLE ONLY maker.vat_fold - ADD CONSTRAINT vat_fold_header_id_tx_idx_key UNIQUE (header_id, tx_idx); + ADD CONSTRAINT vat_fold_header_id_tx_idx_log_idx_key UNIQUE (header_id, tx_idx, log_idx); -- diff --git a/pkg/transformers/test_data/vat_fold.go b/pkg/transformers/test_data/vat_fold.go index e033da17..e4ffb595 100644 --- a/pkg/transformers/test_data/vat_fold.go +++ b/pkg/transformers/test_data/vat_fold.go @@ -47,6 +47,7 @@ var VatFoldModel = vat_fold.VatFoldModel{ Ilk: "REP", Urn: "0x3728e9777B2a0a611ee0F89e00E01044ce4736d1", Rate: "2", + LogIndex: EthVatFoldLog.Index, TransactionIndex: EthVatFoldLog.TxIndex, Raw: rawVatFoldLog, } diff --git a/pkg/transformers/vat_fold/converter.go b/pkg/transformers/vat_fold/converter.go index 30936043..95ade467 100644 --- a/pkg/transformers/vat_fold/converter.go +++ b/pkg/transformers/vat_fold/converter.go @@ -50,6 +50,7 @@ func (VatFoldConverter) ToModels(ethLogs []types.Log) ([]VatFoldModel, error) { Ilk: ilk, Urn: urn, Rate: rate, + LogIndex: ethLog.Index, TransactionIndex: ethLog.TxIndex, Raw: raw, } diff --git a/pkg/transformers/vat_fold/model.go b/pkg/transformers/vat_fold/model.go index 9af973b3..10ccc35a 100644 --- a/pkg/transformers/vat_fold/model.go +++ b/pkg/transformers/vat_fold/model.go @@ -4,6 +4,7 @@ type VatFoldModel struct { Ilk string Urn string Rate string + LogIndex uint `db:"log_idx"` TransactionIndex uint `db:"tx_idx"` Raw []byte `db:"raw_log"` } diff --git a/pkg/transformers/vat_fold/repository.go b/pkg/transformers/vat_fold/repository.go index f89b8057..6b60d20b 100644 --- a/pkg/transformers/vat_fold/repository.go +++ b/pkg/transformers/vat_fold/repository.go @@ -42,9 +42,9 @@ func (repository VatFoldRepository) Create(headerID int64, models []VatFoldModel } for _, model := range models { _, err = tx.Exec( - `INSERT into maker.vat_fold (header_id, ilk, urn, rate, tx_idx, raw_log) - VALUES($1, $2, $3, $4::NUMERIC, $5, $6)`, - headerID, model.Ilk, model.Urn, model.Rate, model.TransactionIndex, model.Raw, + `INSERT into maker.vat_fold (header_id, ilk, urn, rate, log_idx, tx_idx, raw_log) + VALUES($1, $2, $3, $4::NUMERIC, $5, $6, $7)`, + headerID, model.Ilk, model.Urn, model.Rate, model.LogIndex, model.TransactionIndex, model.Raw, ) if err != nil { tx.Rollback() diff --git a/pkg/transformers/vat_fold/repository_test.go b/pkg/transformers/vat_fold/repository_test.go index c0e1dae2..5ba12483 100644 --- a/pkg/transformers/vat_fold/repository_test.go +++ b/pkg/transformers/vat_fold/repository_test.go @@ -51,24 +51,41 @@ var _ = Describe("Vat.fold repository", func() { BeforeEach(func() { headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) Expect(err).NotTo(HaveOccurred()) - - err = repository.Create(headerID, []vat_fold.VatFoldModel{test_data.VatFoldModel}) - Expect(err).NotTo(HaveOccurred()) }) It("adds a vat event", func() { + err = repository.Create(headerID, []vat_fold.VatFoldModel{test_data.VatFoldModel}) + + Expect(err).NotTo(HaveOccurred()) var dbVatFold vat_fold.VatFoldModel - err := db.Get(&dbVatFold, `SELECT ilk, urn, rate, tx_idx, raw_log FROM maker.vat_fold WHERE header_id = $1`, headerID) + err := db.Get(&dbVatFold, `SELECT ilk, urn, rate, log_idx, tx_idx, raw_log FROM maker.vat_fold WHERE header_id = $1`, headerID) Expect(err).NotTo(HaveOccurred()) Expect(dbVatFold.Ilk).To(Equal(test_data.VatFoldModel.Ilk)) Expect(dbVatFold.Urn).To(Equal(test_data.VatFoldModel.Urn)) Expect(dbVatFold.Rate).To(Equal(test_data.VatFoldModel.Rate)) + Expect(dbVatFold.LogIndex).To(Equal(test_data.VatFoldModel.LogIndex)) Expect(dbVatFold.TransactionIndex).To(Equal(test_data.VatFoldModel.TransactionIndex)) Expect(dbVatFold.Raw).To(MatchJSON(test_data.VatFoldModel.Raw)) }) It("marks header as checked for logs", func() { + err = repository.Create(headerID, []vat_fold.VatFoldModel{test_data.VatFoldModel}) + + Expect(err).NotTo(HaveOccurred()) + var headerChecked bool + err = db.Get(&headerChecked, `SELECT vat_fold_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 = repository.Create(headerID, []vat_fold.VatFoldModel{test_data.VatFoldModel}) + + Expect(err).NotTo(HaveOccurred()) var headerChecked bool err = db.Get(&headerChecked, `SELECT vat_fold_checked FROM public.checked_headers WHERE header_id = $1`, headerID) Expect(err).NotTo(HaveOccurred()) @@ -76,18 +93,24 @@ var _ = Describe("Vat.fold repository", func() { }) It("does not duplicate vat events", func() { - err := repository.Create(headerID, []vat_fold.VatFoldModel{test_data.VatFoldModel}) + err = repository.Create(headerID, []vat_fold.VatFoldModel{test_data.VatFoldModel}) + Expect(err).NotTo(HaveOccurred()) + + err = repository.Create(headerID, []vat_fold.VatFoldModel{test_data.VatFoldModel}) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) }) It("removes vat if corresponding header is deleted", func() { - _, err := db.Exec(`DELETE FROM headers WHERE id = $1`, headerID) + err = repository.Create(headerID, []vat_fold.VatFoldModel{test_data.VatFoldModel}) Expect(err).NotTo(HaveOccurred()) + _, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID) + + Expect(err).NotTo(HaveOccurred()) var dbVatFold vat_fold.VatFoldModel - err = db.Get(&dbVatFold, `SELECT ilk, tx_idx, raw_log FROM maker.vat_fold WHERE header_id = $1`, headerID) + err = db.Get(&dbVatFold, `SELECT ilk, urn, rate, log_idx, tx_idx, raw_log FROM maker.vat_fold WHERE header_id = $1`, headerID) Expect(err).To(HaveOccurred()) Expect(err).To(MatchError(sql.ErrNoRows)) })