Convert Bite event field types to numeric

- Enables arithmetic operations when constructing views
- Also removes application-level references to DB ID
This commit is contained in:
Rob Mulholand 2018-10-16 14:51:26 -05:00
parent d3df685c46
commit d0226dbce5
11 changed files with 23 additions and 53 deletions

View File

@ -1,13 +1,13 @@
CREATE TABLE maker.bite (
id SERIAL PRIMARY KEY,
header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE,
ilk bytea,
lad bytea,
ink VARCHAR,
art VARCHAR,
iArt VARCHAR,
ilk TEXT,
urn TEXT,
ink NUMERIC,
art NUMERIC,
iArt NUMERIC,
tab NUMERIC,
flip VARCHAR,
nflip NUMERIC,
tx_idx INTEGER NOT NUll,
raw_log JSONB,
UNIQUE (header_id, tx_idx)

View File

@ -1 +0,0 @@
ALTER TABLE maker.bite RENAME COLUMN urn TO lad;

View File

@ -1 +0,0 @@
ALTER TABLE maker.bite RENAME COLUMN lad TO urn;

View File

@ -66,11 +66,11 @@ CREATE TABLE maker.bite (
header_id integer NOT NULL,
ilk text,
urn text,
ink character varying,
art character varying,
iart character varying,
ink numeric,
art numeric,
iart numeric,
tab numeric,
flip character varying,
nflip numeric,
tx_idx integer NOT NULL,
raw_log jsonb
);

View File

@ -61,7 +61,6 @@ func (BiteConverter) ToEntities(contractAbi string, ethLogs []types.Log) ([]Bite
func (converter BiteConverter) ToModels(entities []BiteEntity) ([]BiteModel, error) {
var models []BiteModel
for _, entity := range entities {
id := entity.Id
ilk := common.BytesToAddress(entity.Ilk[:common.AddressLength]).String()
urn := common.BytesToAddress(entity.Urn[:common.AddressLength]).String()
ink := entity.Ink
@ -77,14 +76,13 @@ func (converter BiteConverter) ToModels(entities []BiteEntity) ([]BiteModel, err
}
model := BiteModel{
Id: shared.BigIntToString(id),
Ilk: ilk,
Urn: urn,
Ink: shared.BigIntToString(ink),
Art: shared.BigIntToString(art),
IArt: shared.BigIntToString(iArt),
Tab: shared.BigIntToString(tab),
Flip: shared.BigIntToString(flip),
NFlip: shared.BigIntToString(flip),
TransactionIndex: txIdx,
Raw: rawLogString,
}

View File

@ -18,8 +18,6 @@ package bite_test
import (
"encoding/json"
"math/big"
"github.com/ethereum/go-ethereum/core/types"
"github.com/vulcanize/vulcanizedb/pkg/transformers/bite"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
@ -60,10 +58,6 @@ var _ = Describe("Bite Converter", func() {
Describe("ToModel", func() {
var emptyEntity = bite.BiteEntity{}
BeforeEach(func() {
emptyEntity.Id = big.NewInt(1)
})
It("converts an Entity to a Model", func() {
models, err := converter.ToModels([]bite.BiteEntity{test_data.BiteEntity})
@ -71,21 +65,19 @@ var _ = Describe("Bite Converter", func() {
Expect(len(models)).To(Equal(1))
model := models[0]
Expect(model).To(Equal(test_data.BiteModel))
Expect(model.TransactionIndex).To(Equal(test_data.BiteModel.TransactionIndex))
})
It("handles nil values", func() {
emptyLog, err := json.Marshal(types.Log{})
Expect(err).NotTo(HaveOccurred())
expectedModel := bite.BiteModel{
Id: "1",
Ilk: "0x0000000000000000000000000000000000000000",
Urn: "0x0000000000000000000000000000000000000000",
Ink: "",
Art: "",
IArt: "",
Tab: "",
Flip: "",
NFlip: "",
TransactionIndex: 0,
Raw: string(emptyLog),
}

View File

@ -20,7 +20,6 @@ import (
)
type BiteEntity struct {
Id *big.Int
Ilk [32]byte
Urn [32]byte
Ink *big.Int

View File

@ -15,14 +15,13 @@
package bite
type BiteModel struct {
Id string
Ilk string
Urn string
Ink string
Art string
IArt string
Tab string
Flip string
NFlip string
TransactionIndex uint `db:"tx_idx"`
Raw string `db:"raw_log"`
}

View File

@ -40,9 +40,9 @@ func (repository BiteRepository) Create(headerID int64, models []BiteModel) erro
}
for _, model := range models {
_, err := tx.Exec(
`INSERT into maker.bite (header_id, id, ilk, urn, ink, art, iart, tab, flip, tx_idx, raw_log)
VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)`,
headerID, model.Id, model.Ilk, model.Urn, model.Ink, model.Art, model.IArt, model.Tab, model.Flip, model.TransactionIndex, model.Raw,
`INSERT into maker.bite (header_id, ilk, urn, ink, art, iart, tab, nflip, tx_idx, raw_log)
VALUES($1, $2, $3, $4::NUMERIC, $5::NUMERIC, $6::NUMERIC, $7::NUMERIC, $8::NUMERIC, $9, $10)`,
headerID, model.Ilk, model.Urn, model.Ink, model.Art, model.IArt, model.Tab, model.NFlip, model.TransactionIndex, model.Raw,
)
if err != nil {
tx.Rollback()

View File

@ -58,14 +58,15 @@ var _ = Describe("Bite repository", func() {
It("persists a bite record", func() {
var dbBite bite.BiteModel
err = db.Get(&dbBite, `SELECT id, ilk, urn, ink, art, tab, flip, tx_idx, raw_log FROM maker.bite WHERE header_id = $1`, headerID)
err = db.Get(&dbBite, `SELECT ilk, urn, ink, art, tab, nflip, iart, tx_idx, raw_log FROM maker.bite WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(dbBite.Id).To(Equal(test_data.BiteModel.Id))
Expect(dbBite.Ilk).To(Equal(test_data.BiteModel.Ilk))
Expect(dbBite.Urn).To(Equal(test_data.BiteModel.Urn))
Expect(dbBite.Ink).To(Equal(test_data.BiteModel.Ink))
Expect(dbBite.Art).To(Equal(test_data.BiteModel.Art))
Expect(dbBite.Tab).To(Equal(test_data.BiteModel.Tab))
Expect(dbBite.Flip).To(Equal(test_data.BiteModel.Flip))
Expect(dbBite.NFlip).To(Equal(test_data.BiteModel.NFlip))
Expect(dbBite.IArt).To(Equal(test_data.BiteModel.IArt))
Expect(dbBite.TransactionIndex).To(Equal(test_data.BiteModel.TransactionIndex))
Expect(dbBite.Raw).To(MatchJSON(test_data.BiteModel.Raw))
})
@ -78,20 +79,7 @@ var _ = Describe("Bite repository", func() {
})
It("does not duplicate bite events", func() {
var anotherBiteModel = bite.BiteModel{
Id: "11",
Ilk: test_data.BiteModel.Ilk,
Urn: test_data.BiteModel.Urn,
Ink: test_data.BiteModel.Ink,
Art: test_data.BiteModel.Art,
Tab: test_data.BiteModel.Tab,
Flip: test_data.BiteModel.Flip,
IArt: test_data.BiteModel.IArt,
TransactionIndex: test_data.BiteModel.TransactionIndex,
Raw: test_data.BiteModel.Raw,
}
err = biteRepository.Create(headerID, []bite.BiteModel{anotherBiteModel})
err = biteRepository.Create(headerID, []bite.BiteModel{test_data.BiteModel})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
@ -102,7 +90,7 @@ var _ = Describe("Bite repository", func() {
Expect(err).NotTo(HaveOccurred())
var dbBite bite.BiteModel
err = db.Get(&dbBite, `SELECT id, ilk, urn, ink, art, tab, flip, tx_idx, raw_log FROM maker.bite WHERE header_id = $1`, headerID)
err = db.Get(&dbBite, `SELECT ilk, urn, ink, art, tab, nflip, iart, tx_idx, raw_log FROM maker.bite WHERE header_id = $1`, headerID)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(sql.ErrNoRows))
})

View File

@ -22,7 +22,6 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/transformers/bite"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"math/big"
"strconv"
)
var (
@ -44,7 +43,6 @@ var (
biteLad = [32]byte{102, 97, 107, 101, 32, 108, 97, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
biteIlkString = "0x66616b6520696c6B000000000000000000000000"
biteLadString = "0x66616B65206c6164000000000000000000000000"
biteId = int64(1)
)
var EthBiteLog = types.Log{
@ -64,7 +62,6 @@ var EthBiteLog = types.Log{
}
var BiteEntity = bite.BiteEntity{
Id: big.NewInt(biteId),
Ilk: biteIlk,
Urn: biteLad,
Ink: biteInk,
@ -77,13 +74,12 @@ var BiteEntity = bite.BiteEntity{
}
var BiteModel = bite.BiteModel{
Id: strconv.FormatInt(biteId, 10),
Ilk: biteIlkString,
Urn: biteLadString,
Ink: biteInk.String(),
Art: biteArt.String(),
Tab: biteTab.String(),
Flip: biteFlip.String(),
NFlip: biteFlip.String(),
IArt: biteIArt.String(),
TransactionIndex: EthBiteLog.TxIndex,
Raw: biteRawString,