Reference ilks foreign key from Cat storage repository

This commit is contained in:
Rob Mulholand 2019-02-21 14:33:59 -06:00
parent e04a72b059
commit 3d9341ea1d
4 changed files with 143 additions and 24 deletions

View File

@ -38,7 +38,7 @@ CREATE TABLE maker.cat_ilk_flip (
id SERIAL PRIMARY KEY,
block_number BIGINT,
block_hash TEXT,
ilk TEXT,
ilk INTEGER NOT NULL REFERENCES maker.ilks (id),
flip TEXT
);
@ -46,7 +46,7 @@ CREATE TABLE maker.cat_ilk_chop (
id SERIAL PRIMARY KEY,
block_number BIGINT,
block_hash TEXT,
ilk TEXT,
ilk INTEGER NOT NULL REFERENCES maker.ilks (id),
chop NUMERIC NOT NULL
);
@ -54,7 +54,7 @@ CREATE TABLE maker.cat_ilk_lump (
id SERIAL PRIMARY KEY,
block_number BIGINT,
block_hash TEXT,
ilk TEXT,
ilk INTEGER NOT NULL REFERENCES maker.ilks (id),
lump NUMERIC NOT NULL
);
@ -63,7 +63,7 @@ CREATE TABLE maker.cat_flip_ilk (
block_number BIGINT,
block_hash TEXT,
flip NUMERIC NOT NULL,
ilk TEXT
ilk INTEGER NOT NULL REFERENCES maker.ilks (id)
);
CREATE TABLE maker.cat_flip_urn (

View File

@ -213,7 +213,7 @@ CREATE TABLE maker.cat_flip_ilk (
block_number bigint,
block_hash text,
flip numeric NOT NULL,
ilk text
ilk integer NOT NULL
);
@ -344,7 +344,7 @@ CREATE TABLE maker.cat_ilk_chop (
id integer NOT NULL,
block_number bigint,
block_hash text,
ilk text,
ilk integer NOT NULL,
chop numeric NOT NULL
);
@ -377,7 +377,7 @@ CREATE TABLE maker.cat_ilk_flip (
id integer NOT NULL,
block_number bigint,
block_hash text,
ilk text,
ilk integer NOT NULL,
flip text
);
@ -410,7 +410,7 @@ CREATE TABLE maker.cat_ilk_lump (
id integer NOT NULL,
block_number bigint,
block_hash text,
ilk text,
ilk integer NOT NULL,
lump numeric NOT NULL
);
@ -4413,6 +4413,38 @@ ALTER TABLE ONLY maker.cat_file_pit_vow
ADD CONSTRAINT cat_file_pit_vow_header_id_fkey FOREIGN KEY (header_id) REFERENCES public.headers(id) ON DELETE CASCADE;
--
-- Name: cat_flip_ilk cat_flip_ilk_ilk_fkey; Type: FK CONSTRAINT; Schema: maker; Owner: -
--
ALTER TABLE ONLY maker.cat_flip_ilk
ADD CONSTRAINT cat_flip_ilk_ilk_fkey FOREIGN KEY (ilk) REFERENCES maker.ilks(id);
--
-- Name: cat_ilk_chop cat_ilk_chop_ilk_fkey; Type: FK CONSTRAINT; Schema: maker; Owner: -
--
ALTER TABLE ONLY maker.cat_ilk_chop
ADD CONSTRAINT cat_ilk_chop_ilk_fkey FOREIGN KEY (ilk) REFERENCES maker.ilks(id);
--
-- Name: cat_ilk_flip cat_ilk_flip_ilk_fkey; Type: FK CONSTRAINT; Schema: maker; Owner: -
--
ALTER TABLE ONLY maker.cat_ilk_flip
ADD CONSTRAINT cat_ilk_flip_ilk_fkey FOREIGN KEY (ilk) REFERENCES maker.ilks(id);
--
-- Name: cat_ilk_lump cat_ilk_lump_ilk_fkey; Type: FK CONSTRAINT; Schema: maker; Owner: -
--
ALTER TABLE ONLY maker.cat_ilk_lump
ADD CONSTRAINT cat_ilk_lump_ilk_fkey FOREIGN KEY (ilk) REFERENCES maker.ilks(id);
--
-- Name: deal deal_header_id_fkey; Type: FK CONSTRAINT; Schema: maker; Owner: -
--

View File

@ -3,6 +3,7 @@ package cat
import (
"fmt"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
shared2 "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/shared"
)
@ -86,10 +87,29 @@ func (repository *CatStorageRepository) insertIlkFlip(blockNumber int, blockHash
if err != nil {
return err
}
_, writeErr := repository.db.Exec(
tx, txErr := repository.db.Begin()
if txErr != nil {
return txErr
}
ilkID, ilkErr := shared2.GetOrCreateIlkInTransaction(ilk, tx)
if ilkErr != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
return fmt.Errorf("failed to rollback transaction after failing to insert ilk: %s", ilkErr.Error())
}
return ilkErr
}
_, writeErr := tx.Exec(
`INSERT INTO maker.cat_ilk_flip (block_number, block_hash, ilk, flip) VALUES ($1, $2, $3, $4)`,
blockNumber, blockHash, ilk, flip)
return writeErr
blockNumber, blockHash, ilkID, flip)
if writeErr != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
return fmt.Errorf("failed to rollback transaction after failing to insert ilk flip: %s", writeErr.Error())
}
return writeErr
}
return tx.Commit()
}
func (repository *CatStorageRepository) insertIlkChop(blockNumber int, blockHash string, metadata shared.StorageValueMetadata, chop string) error {
@ -97,10 +117,29 @@ func (repository *CatStorageRepository) insertIlkChop(blockNumber int, blockHash
if err != nil {
return err
}
_, writeErr := repository.db.Exec(
tx, txErr := repository.db.Begin()
if txErr != nil {
return txErr
}
ilkID, ilkErr := shared2.GetOrCreateIlkInTransaction(ilk, tx)
if ilkErr != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
return fmt.Errorf("failed to rollback transaction after failing to insert ilk: %s", ilkErr.Error())
}
return ilkErr
}
_, writeErr := tx.Exec(
`INSERT INTO maker.cat_ilk_chop (block_number, block_hash, ilk, chop) VALUES ($1, $2, $3, $4)`,
blockNumber, blockHash, ilk, chop)
return writeErr
blockNumber, blockHash, ilkID, chop)
if writeErr != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
return fmt.Errorf("failed to rollback transaction after failing to insert ilk chop: %s", writeErr.Error())
}
return writeErr
}
return tx.Commit()
}
func (repository *CatStorageRepository) insertIlkLump(blockNumber int, blockHash string, metadata shared.StorageValueMetadata, lump string) error {
@ -108,10 +147,29 @@ func (repository *CatStorageRepository) insertIlkLump(blockNumber int, blockHash
if err != nil {
return err
}
_, writeErr := repository.db.Exec(
tx, txErr := repository.db.Begin()
if txErr != nil {
return txErr
}
ilkID, ilkErr := shared2.GetOrCreateIlkInTransaction(ilk, tx)
if ilkErr != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
return fmt.Errorf("failed to rollback transaction after failing to insert ilk: %s", ilkErr.Error())
}
return ilkErr
}
_, writeErr := tx.Exec(
`INSERT INTO maker.cat_ilk_lump (block_number, block_hash, ilk, lump) VALUES ($1, $2, $3, $4)`,
blockNumber, blockHash, ilk, lump)
return writeErr
blockNumber, blockHash, ilkID, lump)
if writeErr != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
return fmt.Errorf("failed to rollback transaction after failing to insert ilk lump: %s", writeErr.Error())
}
return writeErr
}
return tx.Commit()
}
// Flips mapping: uint256 => ilk, urn bytes32; ink, tab uint256 (both wad)
@ -120,10 +178,29 @@ func (repository *CatStorageRepository) insertFlipIlk(blockNumber int, blockHash
if err != nil {
return err
}
_, writeErr := repository.db.Exec(
tx, txErr := repository.db.Begin()
if txErr != nil {
return txErr
}
ilkID, ilkErr := shared2.GetOrCreateIlkInTransaction(ilk, tx)
if ilkErr != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
return fmt.Errorf("failed to rollback transaction after failing to insert ilk: %s", ilkErr.Error())
}
return ilkErr
}
_, writeErr := tx.Exec(
`INSERT INTO maker.cat_flip_ilk (block_number, block_hash, flip, ilk) VALUES ($1, $2, $3, $4)`,
blockNumber, blockHash, flip, ilk)
return writeErr
blockNumber, blockHash, flip, ilkID)
if writeErr != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
return fmt.Errorf("failed to rollback transaction after failing to insert flip ilk: %s", writeErr.Error())
}
return writeErr
}
return tx.Commit()
}
func (repository *CatStorageRepository) insertFlipUrn(blockNumber int, blockHash string, metadata shared.StorageValueMetadata, urn string) error {

View File

@ -4,10 +4,12 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
shared2 "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker/cat"
. "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker/test_helpers"
"github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/shared"
"github.com/vulcanize/vulcanizedb/test_config"
"strconv"
)
var _ = Describe("Cat storage repository", func() {
@ -110,7 +112,9 @@ var _ = Describe("Cat storage repository", func() {
err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key, flip AS value FROM maker.cat_ilk_flip`)
Expect(err).NotTo(HaveOccurred())
AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeIlk, fakeAddress)
ilkID, err := shared2.GetOrCreateIlk(fakeIlk, db)
Expect(err).NotTo(HaveOccurred())
AssertMapping(result, fakeBlockNumber, fakeBlockHash, strconv.Itoa(ilkID), fakeAddress)
})
It("returns an error if metadata missing ilk", func() {
@ -130,7 +134,9 @@ var _ = Describe("Cat storage repository", func() {
err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key, chop AS value FROM maker.cat_ilk_chop`)
Expect(err).NotTo(HaveOccurred())
AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeIlk, fakeUint256)
ilkID, err := shared2.GetOrCreateIlk(fakeIlk, db)
Expect(err).NotTo(HaveOccurred())
AssertMapping(result, fakeBlockNumber, fakeBlockHash, strconv.Itoa(ilkID), fakeUint256)
})
It("returns an error if metadata missing ilk", func() {
@ -150,7 +156,9 @@ var _ = Describe("Cat storage repository", func() {
err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key, lump AS value FROM maker.cat_ilk_lump`)
Expect(err).NotTo(HaveOccurred())
AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeIlk, fakeUint256)
ilkID, err := shared2.GetOrCreateIlk(fakeIlk, db)
Expect(err).NotTo(HaveOccurred())
AssertMapping(result, fakeBlockNumber, fakeBlockHash, strconv.Itoa(ilkID), fakeUint256)
})
It("returns an error if metadata missing ilk", func() {
@ -174,7 +182,9 @@ var _ = Describe("Cat storage repository", func() {
err = db.Get(&result, `SELECT block_number, block_hash, flip AS key, ilk AS value FROM maker.cat_flip_ilk`)
Expect(err).NotTo(HaveOccurred())
AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeUint256, fakeBytes32)
ilkID, err := shared2.GetOrCreateIlk(fakeBytes32, db)
Expect(err).NotTo(HaveOccurred())
AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeUint256, strconv.Itoa(ilkID))
})
It("returns an error if metadata missing flip", func() {