Add log index to Vat fold

This commit is contained in:
Rob Mulholand 2018-10-22 16:18:40 -05:00
parent db52955669
commit 1e74c73591
7 changed files with 48 additions and 20 deletions

View File

@ -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,
ilk TEXT,
urn TEXT,
rate NUMERIC,
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)
);
ALTER TABLE public.checked_headers

View File

@ -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);
--

View File

@ -47,6 +47,7 @@ var VatFoldModel = vat_fold.VatFoldModel{
Ilk: "REP",
Urn: "0x3728e9777B2a0a611ee0F89e00E01044ce4736d1",
Rate: "2",
LogIndex: EthVatFoldLog.Index,
TransactionIndex: EthVatFoldLog.TxIndex,
Raw: rawVatFoldLog,
}

View File

@ -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,
}

View File

@ -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"`
}

View File

@ -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()

View File

@ -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))
})