From 0b58efb56a2f765ca1f94d49793fd8d5f28a8be8 Mon Sep 17 00:00:00 2001 From: Rob Mulholand Date: Thu, 8 Nov 2018 11:07:54 -0600 Subject: [PATCH] Extract common repository methods - Inject column names to reduce duplication across common behavior - Extract checked headers column names to constants --- .../cat_file/chop_lump/repository.go | 29 +++----------- .../cat_file/chop_lump/repository_test.go | 5 ++- pkg/transformers/cat_file/flip/repository.go | 28 +++---------- .../cat_file/flip/repository_test.go | 5 ++- .../cat_file/pit_vow/repository.go | 29 +++----------- .../cat_file/pit_vow/repository_test.go | 5 ++- pkg/transformers/deal/repository.go | 27 +++---------- pkg/transformers/deal/repository_test.go | 5 ++- pkg/transformers/dent/repository.go | 30 +++----------- pkg/transformers/dent/repository_test.go | 5 ++- pkg/transformers/drip_drip/repository.go | 29 +++----------- pkg/transformers/drip_drip/repository_test.go | 5 ++- pkg/transformers/drip_file/ilk/repository.go | 29 +++----------- .../drip_file/ilk/repository_test.go | 5 ++- pkg/transformers/drip_file/repo/repository.go | 29 +++----------- .../drip_file/repo/repository_test.go | 5 ++- pkg/transformers/drip_file/vow/repository.go | 29 +++----------- .../drip_file/vow/repository_test.go | 5 ++- pkg/transformers/flap_kick/repository.go | 35 +++------------- pkg/transformers/flap_kick/repository_test.go | 5 ++- .../pit_file/debt_ceiling/repository.go | 29 +++----------- .../pit_file/debt_ceiling/repository_test.go | 5 ++- pkg/transformers/pit_file/ilk/repository.go | 29 +++----------- .../pit_file/ilk/repository_test.go | 5 ++- .../pit_file/stability_fee/repository.go | 29 +++----------- .../pit_file/stability_fee/repository_test.go | 5 ++- pkg/transformers/price_feeds/repository.go | 29 +++----------- .../price_feeds/repository_test.go | 5 ++- .../shared/constants/checked_headers.go | 19 +++++++++ pkg/transformers/shared/repository.go | 40 +++++++++++++++++++ pkg/transformers/tend/repository.go | 29 +++----------- pkg/transformers/tend/repository_test.go | 5 ++- 32 files changed, 186 insertions(+), 387 deletions(-) create mode 100644 pkg/transformers/shared/constants/checked_headers.go create mode 100644 pkg/transformers/shared/repository.go diff --git a/pkg/transformers/cat_file/chop_lump/repository.go b/pkg/transformers/cat_file/chop_lump/repository.go index 2de82a6e..20c00f02 100644 --- a/pkg/transformers/cat_file/chop_lump/repository.go +++ b/pkg/transformers/cat_file/chop_lump/repository.go @@ -18,6 +18,8 @@ import ( "fmt" "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" ) type CatFileChopLumpRepository struct { @@ -47,10 +49,8 @@ func (repository CatFileChopLumpRepository) Create(headerID int64, models []inte return err } } - _, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, cat_file_chop_lump_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET cat_file_chop_lump_checked = $2`, headerID, true) + + err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.CatFileChopLumpChecked) if err != nil { tx.Rollback() return err @@ -59,28 +59,11 @@ func (repository CatFileChopLumpRepository) Create(headerID int64, models []inte } func (repository CatFileChopLumpRepository) MarkHeaderChecked(headerID int64) error { - _, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, cat_file_chop_lump_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET cat_file_chop_lump_checked = $2`, headerID, true) - return err + return shared.MarkHeaderChecked(headerID, repository.db, constants.CatFileChopLumpChecked) } func (repository CatFileChopLumpRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { - var result []core.Header - err := repository.db.Select( - &result, - `SELECT headers.id, headers.block_number FROM headers - LEFT JOIN checked_headers on headers.id = header_id - WHERE (header_id ISNULL OR cat_file_chop_lump_checked IS FALSE) - AND headers.block_number >= $1 - AND headers.block_number <= $2 - AND headers.eth_node_fingerprint = $3`, - startingBlockNumber, - endingBlockNumber, - repository.db.Node.ID, - ) - return result, err + return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, repository.db, constants.CatFileChopLumpChecked) } func (repository *CatFileChopLumpRepository) SetDB(db *postgres.DB) { diff --git a/pkg/transformers/cat_file/chop_lump/repository_test.go b/pkg/transformers/cat_file/chop_lump/repository_test.go index 430c4670..a80079f3 100644 --- a/pkg/transformers/cat_file/chop_lump/repository_test.go +++ b/pkg/transformers/cat_file/chop_lump/repository_test.go @@ -21,6 +21,7 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/transformers/cat_file/chop_lump" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors" "github.com/vulcanize/vulcanizedb/test_config" @@ -43,7 +44,7 @@ var _ = Describe("Cat file chop lump repository", func() { modelWithDifferentLogIdx := test_data.CatFileChopLumpModel modelWithDifferentLogIdx.LogIndex++ inputs := shared_behaviors.CreateBehaviorInputs{ - CheckedHeaderColumnName: "cat_file_chop_lump_checked", + CheckedHeaderColumnName: constants.CatFileChopLumpChecked, LogEventTableName: "maker.cat_file_chop_lump", TestModel: test_data.CatFileChopLumpModel, ModelWithDifferentLogIdx: modelWithDifferentLogIdx, @@ -73,7 +74,7 @@ var _ = Describe("Cat file chop lump repository", func() { Describe("MarkHeaderChecked", func() { inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{ - CheckedHeaderColumnName: "cat_file_chop_lump_checked", + CheckedHeaderColumnName: constants.CatFileChopLumpChecked, Repository: &catFileRepository, } diff --git a/pkg/transformers/cat_file/flip/repository.go b/pkg/transformers/cat_file/flip/repository.go index fe09a4bb..8f59c20f 100644 --- a/pkg/transformers/cat_file/flip/repository.go +++ b/pkg/transformers/cat_file/flip/repository.go @@ -18,6 +18,8 @@ import ( "fmt" "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" ) type CatFileFlipRepository struct { @@ -46,11 +48,8 @@ func (repository CatFileFlipRepository) Create(headerID int64, models []interfac return err } } - _, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, cat_file_flip_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET cat_file_flip_checked = $2`, headerID, true) + err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.CatFileFlipChecked) if err != nil { tx.Rollback() return err @@ -59,28 +58,11 @@ func (repository CatFileFlipRepository) Create(headerID int64, models []interfac } func (repository CatFileFlipRepository) MarkHeaderChecked(headerID int64) error { - _, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, cat_file_flip_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET cat_file_flip_checked = $2`, headerID, true) - return err + return shared.MarkHeaderChecked(headerID, repository.db, constants.CatFileFlipChecked) } func (repository CatFileFlipRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { - var result []core.Header - err := repository.db.Select( - &result, - `SELECT headers.id, headers.block_number FROM headers - LEFT JOIN checked_headers on headers.id = header_id - WHERE (header_id ISNULL OR cat_file_flip_checked IS FALSE) - AND headers.block_number >= $1 - AND headers.block_number <= $2 - AND headers.eth_node_fingerprint = $3`, - startingBlockNumber, - endingBlockNumber, - repository.db.Node.ID, - ) - return result, err + return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, repository.db, constants.CatFileFlipChecked) } func (repository *CatFileFlipRepository) SetDB(db *postgres.DB) { diff --git a/pkg/transformers/cat_file/flip/repository_test.go b/pkg/transformers/cat_file/flip/repository_test.go index 7ac2d6ed..c50e3a97 100644 --- a/pkg/transformers/cat_file/flip/repository_test.go +++ b/pkg/transformers/cat_file/flip/repository_test.go @@ -23,6 +23,7 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/transformers/cat_file/flip" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors" "github.com/vulcanize/vulcanizedb/test_config" @@ -47,7 +48,7 @@ var _ = Describe("Cat file flip repository", func() { catFileFlipWithDifferentIdx := test_data.CatFileFlipModel catFileFlipWithDifferentIdx.LogIndex++ inputs := shared_behaviors.CreateBehaviorInputs{ - CheckedHeaderColumnName: "cat_file_flip_checked", + CheckedHeaderColumnName: constants.CatFileFlipChecked, LogEventTableName: "maker.cat_file_flip", TestModel: test_data.CatFileFlipModel, ModelWithDifferentLogIdx: catFileFlipWithDifferentIdx, @@ -76,7 +77,7 @@ var _ = Describe("Cat file flip repository", func() { Describe("MarkHeaderChecked", func() { inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{ - CheckedHeaderColumnName: "cat_file_flip_checked", + CheckedHeaderColumnName: constants.CatFileFlipChecked, Repository: &catFileFlipRepository, } diff --git a/pkg/transformers/cat_file/pit_vow/repository.go b/pkg/transformers/cat_file/pit_vow/repository.go index f5a3d3c0..6aa7a108 100644 --- a/pkg/transformers/cat_file/pit_vow/repository.go +++ b/pkg/transformers/cat_file/pit_vow/repository.go @@ -18,6 +18,8 @@ import ( "fmt" "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" ) type CatFilePitVowRepository struct { @@ -46,10 +48,8 @@ func (repository CatFilePitVowRepository) Create(headerID int64, models []interf return err } } - _, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, cat_file_pit_vow_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET cat_file_pit_vow_checked = $2`, headerID, true) + + err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.CatFilePitVowChecked) if err != nil { tx.Rollback() return err @@ -58,28 +58,11 @@ func (repository CatFilePitVowRepository) Create(headerID int64, models []interf } func (repository CatFilePitVowRepository) MarkHeaderChecked(headerID int64) error { - _, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, cat_file_pit_vow_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET cat_file_pit_vow_checked = $2`, headerID, true) - return err + return shared.MarkHeaderChecked(headerID, repository.db, constants.CatFilePitVowChecked) } func (repository CatFilePitVowRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { - var result []core.Header - err := repository.db.Select( - &result, - `SELECT headers.id, headers.block_number FROM headers - LEFT JOIN checked_headers on headers.id = header_id - WHERE (header_id ISNULL OR cat_file_pit_vow_checked IS FALSE) - AND headers.block_number >= $1 - AND headers.block_number <= $2 - AND headers.eth_node_fingerprint = $3`, - startingBlockNumber, - endingBlockNumber, - repository.db.Node.ID, - ) - return result, err + return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, repository.db, constants.CatFilePitVowChecked) } func (repository *CatFilePitVowRepository) SetDB(db *postgres.DB) { diff --git a/pkg/transformers/cat_file/pit_vow/repository_test.go b/pkg/transformers/cat_file/pit_vow/repository_test.go index 17848ca5..5f97cfe1 100644 --- a/pkg/transformers/cat_file/pit_vow/repository_test.go +++ b/pkg/transformers/cat_file/pit_vow/repository_test.go @@ -23,6 +23,7 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/transformers/cat_file/pit_vow" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors" "github.com/vulcanize/vulcanizedb/test_config" @@ -47,7 +48,7 @@ var _ = Describe("Cat file pit vow repository", func() { modelWithDifferentLogIdx := test_data.CatFilePitVowModel modelWithDifferentLogIdx.LogIndex++ inputs := shared_behaviors.CreateBehaviorInputs{ - CheckedHeaderColumnName: "cat_file_pit_vow_checked", + CheckedHeaderColumnName: constants.CatFilePitVowChecked, LogEventTableName: "maker.cat_file_pit_vow", TestModel: test_data.CatFilePitVowModel, ModelWithDifferentLogIdx: modelWithDifferentLogIdx, @@ -75,7 +76,7 @@ var _ = Describe("Cat file pit vow repository", func() { Describe("MarkHeaderChecked", func() { inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{ - CheckedHeaderColumnName: "cat_file_pit_vow_checked", + CheckedHeaderColumnName: constants.CatFilePitVowChecked, Repository: &catFilePitVowRepository, } shared_behaviors.SharedRepositoryMarkHeaderCheckedBehaviors(&inputs) diff --git a/pkg/transformers/deal/repository.go b/pkg/transformers/deal/repository.go index dca7e41e..1bfa420b 100644 --- a/pkg/transformers/deal/repository.go +++ b/pkg/transformers/deal/repository.go @@ -18,6 +18,8 @@ import ( "fmt" "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" ) type DealRepository struct { @@ -47,11 +49,8 @@ func (repository DealRepository) Create(headerId int64, models []interface{}) er return err } } - _, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, deal_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET deal_checked = $2`, headerId, true) + err = shared.MarkHeaderCheckedInTransaction(headerId, tx, constants.DealChecked) if err != nil { tx.Rollback() return err @@ -60,27 +59,11 @@ func (repository DealRepository) Create(headerId int64, models []interface{}) er } func (repository DealRepository) MarkHeaderChecked(headerID int64) error { - _, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, deal_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET deal_checked = $2`, headerID, true) - return err + return shared.MarkHeaderChecked(headerID, repository.db, constants.DealChecked) } func (repository DealRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { - var missingHeaders []core.Header - err := repository.db.Select(&missingHeaders, - `SELECT headers.id, headers.block_number FROM headers - LEFT JOIN checked_headers on headers.id = header_id - WHERE (header_id ISNULL OR deal_checked IS FALSE) - AND headers.block_number >= $1 - AND headers.block_number <= $2 - AND headers.eth_node_fingerprint = $3`, - startingBlockNumber, - endingBlockNumber, - repository.db.Node.ID, - ) - return missingHeaders, err + return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, repository.db, constants.DealChecked) } func (repository *DealRepository) SetDB(db *postgres.DB) { diff --git a/pkg/transformers/deal/repository_test.go b/pkg/transformers/deal/repository_test.go index c182ff9d..5a58c4ca 100644 --- a/pkg/transformers/deal/repository_test.go +++ b/pkg/transformers/deal/repository_test.go @@ -22,6 +22,7 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/transformers/deal" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors" "github.com/vulcanize/vulcanizedb/test_config" @@ -46,7 +47,7 @@ var _ = Describe("Deal Repository", func() { modelWithDifferentLogIdx := test_data.DealModel modelWithDifferentLogIdx.LogIndex = modelWithDifferentLogIdx.LogIndex + 1 inputs := shared_behaviors.CreateBehaviorInputs{ - CheckedHeaderColumnName: "deal_checked", + CheckedHeaderColumnName: constants.DealChecked, LogEventTableName: "maker.deal", TestModel: test_data.DealModel, ModelWithDifferentLogIdx: modelWithDifferentLogIdx, @@ -77,7 +78,7 @@ var _ = Describe("Deal Repository", func() { Describe("MarkHeaderChecked", func() { inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{ - CheckedHeaderColumnName: "deal_checked", + CheckedHeaderColumnName: constants.DealChecked, Repository: &dealRepository, } diff --git a/pkg/transformers/dent/repository.go b/pkg/transformers/dent/repository.go index 4cb4d76a..a2a27ee8 100644 --- a/pkg/transformers/dent/repository.go +++ b/pkg/transformers/dent/repository.go @@ -18,6 +18,8 @@ import ( "fmt" "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" ) type DentRepository struct { @@ -48,10 +50,7 @@ func (r DentRepository) Create(headerId int64, models []interface{}) error { } } - _, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, dent_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET dent_checked = $2`, headerId, true) + err = shared.MarkHeaderCheckedInTransaction(headerId, tx, constants.DentChecked) if err != nil { tx.Rollback() return err @@ -60,30 +59,11 @@ func (r DentRepository) Create(headerId int64, models []interface{}) error { } func (r DentRepository) MarkHeaderChecked(headerId int64) error { - _, err := r.db.Exec(`INSERT INTO public.checked_headers (header_id, dent_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET dent_checked = $2`, headerId, true) - return err + return shared.MarkHeaderChecked(headerId, r.db, constants.DentChecked) } func (r DentRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { - var missingHeaders []core.Header - - err := r.db.Select( - &missingHeaders, - `SELECT headers.id, headers.block_number FROM headers - LEFT JOIN checked_headers on headers.id = header_id - WHERE (header_id ISNULL OR dent_checked IS FALSE) - AND headers.block_number >= $1 - AND headers.block_number <= $2 - AND headers.eth_node_fingerprint = $3`, - startingBlockNumber, - endingBlockNumber, - r.db.Node.ID, - ) - - return missingHeaders, err + return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, r.db, constants.DentChecked) } func (repository *DentRepository) SetDB(db *postgres.DB) { diff --git a/pkg/transformers/dent/repository_test.go b/pkg/transformers/dent/repository_test.go index cb0e3969..b5d6d67d 100644 --- a/pkg/transformers/dent/repository_test.go +++ b/pkg/transformers/dent/repository_test.go @@ -22,6 +22,7 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/transformers/dent" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors" "github.com/vulcanize/vulcanizedb/test_config" @@ -46,7 +47,7 @@ var _ = Describe("Dent Repository", func() { modelWithDifferentLogIdx := test_data.DentModel modelWithDifferentLogIdx.LogIndex++ inputs := shared_behaviors.CreateBehaviorInputs{ - CheckedHeaderColumnName: "dent_checked", + CheckedHeaderColumnName: constants.DentChecked, LogEventTableName: "maker.dent", TestModel: test_data.DentModel, ModelWithDifferentLogIdx: modelWithDifferentLogIdx, @@ -81,7 +82,7 @@ var _ = Describe("Dent Repository", func() { Describe("MarkHeaderChecked", func() { inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{ - CheckedHeaderColumnName: "dent_checked", + CheckedHeaderColumnName: constants.DentChecked, Repository: &dentRepository, } diff --git a/pkg/transformers/drip_drip/repository.go b/pkg/transformers/drip_drip/repository.go index 68c0a747..95780c74 100644 --- a/pkg/transformers/drip_drip/repository.go +++ b/pkg/transformers/drip_drip/repository.go @@ -18,6 +18,8 @@ import ( "fmt" "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" ) type DripDripRepository struct { @@ -46,10 +48,8 @@ func (repository DripDripRepository) Create(headerID int64, models []interface{} return err } } - _, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, drip_drip_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET drip_drip_checked = $2`, headerID, true) + + err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.DripDripChecked) if err != nil { tx.Rollback() return err @@ -58,28 +58,11 @@ func (repository DripDripRepository) Create(headerID int64, models []interface{} } func (repository DripDripRepository) MarkHeaderChecked(headerID int64) error { - _, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, drip_drip_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET drip_drip_checked = $2`, headerID, true) - return err + return shared.MarkHeaderChecked(headerID, repository.db, constants.DripDripChecked) } func (repository DripDripRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { - var result []core.Header - err := repository.db.Select( - &result, - `SELECT headers.id, headers.block_number FROM headers - LEFT JOIN checked_headers on headers.id = header_id - WHERE (header_id ISNULL OR drip_drip_checked IS FALSE) - AND headers.block_number >= $1 - AND headers.block_number <= $2 - AND headers.eth_node_fingerprint = $3`, - startingBlockNumber, - endingBlockNumber, - repository.db.Node.ID, - ) - return result, err + return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, repository.db, constants.DripDripChecked) } func (repository *DripDripRepository) SetDB(db *postgres.DB) { diff --git a/pkg/transformers/drip_drip/repository_test.go b/pkg/transformers/drip_drip/repository_test.go index 3584a9c7..d66b9fd0 100644 --- a/pkg/transformers/drip_drip/repository_test.go +++ b/pkg/transformers/drip_drip/repository_test.go @@ -22,6 +22,7 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_drip" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors" "github.com/vulcanize/vulcanizedb/test_config" @@ -46,7 +47,7 @@ var _ = Describe("Drip drip repository", func() { modelWithDifferentLogIdx := test_data.DripDripModel modelWithDifferentLogIdx.LogIndex++ inputs := shared_behaviors.CreateBehaviorInputs{ - CheckedHeaderColumnName: "drip_drip_checked", + CheckedHeaderColumnName: constants.DripDripChecked, LogEventTableName: "maker.drip_drip", TestModel: test_data.DripDripModel, ModelWithDifferentLogIdx: modelWithDifferentLogIdx, @@ -73,7 +74,7 @@ var _ = Describe("Drip drip repository", func() { Describe("MarkHeaderChecked", func() { inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{ - CheckedHeaderColumnName: "drip_drip_checked", + CheckedHeaderColumnName: constants.DripDripChecked, Repository: &dripDripRepository, } diff --git a/pkg/transformers/drip_file/ilk/repository.go b/pkg/transformers/drip_file/ilk/repository.go index bde2d152..8c602ef9 100644 --- a/pkg/transformers/drip_file/ilk/repository.go +++ b/pkg/transformers/drip_file/ilk/repository.go @@ -18,6 +18,8 @@ import ( "fmt" "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" ) type DripFileIlkRepository struct { @@ -48,10 +50,8 @@ func (repository DripFileIlkRepository) Create(headerID int64, models []interfac return err } } - _, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, drip_file_ilk_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET drip_file_ilk_checked = $2`, headerID, true) + + err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.DripFileIlkChecked) if err != nil { tx.Rollback() return err @@ -61,28 +61,11 @@ func (repository DripFileIlkRepository) Create(headerID int64, models []interfac } func (repository DripFileIlkRepository) MarkHeaderChecked(headerID int64) error { - _, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, drip_file_ilk_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET drip_file_ilk_checked = $2`, headerID, true) - return err + return shared.MarkHeaderChecked(headerID, repository.db, constants.DripFileIlkChecked) } func (repository DripFileIlkRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { - var result []core.Header - err := repository.db.Select( - &result, - `SELECT headers.id, headers.block_number FROM headers - LEFT JOIN checked_headers on headers.id = header_id - WHERE (header_id ISNULL OR drip_file_ilk_checked IS FALSE) - AND headers.block_number >= $1 - AND headers.block_number <= $2 - AND headers.eth_node_fingerprint = $3`, - startingBlockNumber, - endingBlockNumber, - repository.db.Node.ID, - ) - return result, err + return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, repository.db, constants.DripFileIlkChecked) } func (repository *DripFileIlkRepository) SetDB(db *postgres.DB) { diff --git a/pkg/transformers/drip_file/ilk/repository_test.go b/pkg/transformers/drip_file/ilk/repository_test.go index 485e4aca..6853f291 100644 --- a/pkg/transformers/drip_file/ilk/repository_test.go +++ b/pkg/transformers/drip_file/ilk/repository_test.go @@ -22,6 +22,7 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/ilk" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors" "github.com/vulcanize/vulcanizedb/test_config" @@ -46,7 +47,7 @@ var _ = Describe("Drip file ilk repository", func() { modelWithDifferentLogIdx := test_data.DripFileIlkModel modelWithDifferentLogIdx.LogIndex++ inputs := shared_behaviors.CreateBehaviorInputs{ - CheckedHeaderColumnName: "drip_file_ilk_checked", + CheckedHeaderColumnName: constants.DripFileIlkChecked, LogEventTableName: "maker.drip_file_ilk", TestModel: test_data.DripFileIlkModel, ModelWithDifferentLogIdx: modelWithDifferentLogIdx, @@ -75,7 +76,7 @@ var _ = Describe("Drip file ilk repository", func() { Describe("MarkHeaderChecked", func() { inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{ - CheckedHeaderColumnName: "drip_file_ilk_checked", + CheckedHeaderColumnName: constants.DripFileIlkChecked, Repository: &dripFileIlkRepository, } diff --git a/pkg/transformers/drip_file/repo/repository.go b/pkg/transformers/drip_file/repo/repository.go index e7e03cb1..592e92e6 100644 --- a/pkg/transformers/drip_file/repo/repository.go +++ b/pkg/transformers/drip_file/repo/repository.go @@ -18,6 +18,8 @@ import ( "fmt" "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" ) type DripFileRepoRepository struct { @@ -49,11 +51,7 @@ func (repository DripFileRepoRepository) Create(headerID int64, models []interfa } } - _, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, drip_file_repo_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET drip_file_repo_checked = $2`, headerID, true) - + err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.DripFileRepoChecked) if err != nil { tx.Rollback() return err @@ -63,28 +61,11 @@ func (repository DripFileRepoRepository) Create(headerID int64, models []interfa } func (repository DripFileRepoRepository) MarkHeaderChecked(headerID int64) error { - _, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, drip_file_repo_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET drip_file_repo_checked = $2`, headerID, true) - return err + return shared.MarkHeaderChecked(headerID, repository.db, constants.DripFileRepoChecked) } func (repository DripFileRepoRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { - var result []core.Header - err := repository.db.Select( - &result, - `SELECT headers.id, headers.block_number FROM headers - LEFT JOIN checked_headers on headers.id = header_id - WHERE (header_id ISNULL OR drip_file_repo_checked IS FALSE) - AND headers.block_number >= $1 - AND headers.block_number <= $2 - AND headers.eth_node_fingerprint = $3`, - startingBlockNumber, - endingBlockNumber, - repository.db.Node.ID, - ) - return result, err + return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, repository.db, constants.DripFileRepoChecked) } func (repository *DripFileRepoRepository) SetDB(db *postgres.DB) { diff --git a/pkg/transformers/drip_file/repo/repository_test.go b/pkg/transformers/drip_file/repo/repository_test.go index f7756150..8e297a3c 100644 --- a/pkg/transformers/drip_file/repo/repository_test.go +++ b/pkg/transformers/drip_file/repo/repository_test.go @@ -23,6 +23,7 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/repo" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors" "github.com/vulcanize/vulcanizedb/test_config" @@ -47,7 +48,7 @@ var _ = Describe("Drip file repo repository", func() { modelWithDifferentLogIdx := test_data.DripFileRepoModel modelWithDifferentLogIdx.LogIndex++ inputs := shared_behaviors.CreateBehaviorInputs{ - CheckedHeaderColumnName: "drip_file_repo_checked", + CheckedHeaderColumnName: constants.DripFileRepoChecked, LogEventTableName: "maker.drip_file_repo", TestModel: test_data.DripFileRepoModel, ModelWithDifferentLogIdx: modelWithDifferentLogIdx, @@ -75,7 +76,7 @@ var _ = Describe("Drip file repo repository", func() { Describe("MarkHeaderChecked", func() { inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{ - CheckedHeaderColumnName: "drip_file_repo_checked", + CheckedHeaderColumnName: constants.DripFileRepoChecked, Repository: &dripFileRepoRepository, } diff --git a/pkg/transformers/drip_file/vow/repository.go b/pkg/transformers/drip_file/vow/repository.go index 32a4140a..35b089d0 100644 --- a/pkg/transformers/drip_file/vow/repository.go +++ b/pkg/transformers/drip_file/vow/repository.go @@ -18,6 +18,8 @@ import ( "fmt" "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" ) type DripFileVowRepository struct { @@ -48,11 +50,7 @@ func (repository DripFileVowRepository) Create(headerID int64, models []interfac } } - _, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, drip_file_vow_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET drip_file_vow_checked = $2`, headerID, true) - + err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.DripFileVowChecked) if err != nil { tx.Rollback() return err @@ -62,28 +60,11 @@ func (repository DripFileVowRepository) Create(headerID int64, models []interfac } func (repository DripFileVowRepository) MarkHeaderChecked(headerID int64) error { - _, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, drip_file_vow_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET drip_file_vow_checked = $2`, headerID, true) - return err + return shared.MarkHeaderChecked(headerID, repository.db, constants.DripFileVowChecked) } func (repository DripFileVowRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { - var result []core.Header - err := repository.db.Select( - &result, - `SELECT headers.id, headers.block_number FROM headers - LEFT JOIN checked_headers on headers.id = header_id - WHERE (header_id ISNULL OR drip_file_vow_checked IS FALSE) - AND headers.block_number >= $1 - AND headers.block_number <= $2 - AND headers.eth_node_fingerprint = $3`, - startingBlockNumber, - endingBlockNumber, - repository.db.Node.ID, - ) - return result, err + return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, repository.db, constants.DripFileVowChecked) } func (repository *DripFileVowRepository) SetDB(db *postgres.DB) { diff --git a/pkg/transformers/drip_file/vow/repository_test.go b/pkg/transformers/drip_file/vow/repository_test.go index 7672a4a5..9ad62a49 100644 --- a/pkg/transformers/drip_file/vow/repository_test.go +++ b/pkg/transformers/drip_file/vow/repository_test.go @@ -22,6 +22,7 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/vow" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors" "github.com/vulcanize/vulcanizedb/test_config" @@ -46,7 +47,7 @@ var _ = Describe("Drip file vow repository", func() { modelWithDifferentLogIdx := test_data.DripFileVowModel modelWithDifferentLogIdx.LogIndex++ inputs := shared_behaviors.CreateBehaviorInputs{ - CheckedHeaderColumnName: "drip_file_vow_checked", + CheckedHeaderColumnName: constants.DripFileVowChecked, LogEventTableName: "maker.drip_file_vow", TestModel: test_data.DripFileVowModel, ModelWithDifferentLogIdx: modelWithDifferentLogIdx, @@ -83,7 +84,7 @@ var _ = Describe("Drip file vow repository", func() { Describe("MissingHeaders", func() { inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{ - CheckedHeaderColumnName: "drip_file_vow_checked", + CheckedHeaderColumnName: constants.DripFileVowChecked, Repository: &dripFileVowRepository, } diff --git a/pkg/transformers/flap_kick/repository.go b/pkg/transformers/flap_kick/repository.go index 1d0c6e85..1e623f7a 100644 --- a/pkg/transformers/flap_kick/repository.go +++ b/pkg/transformers/flap_kick/repository.go @@ -18,6 +18,8 @@ import ( "fmt" "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" ) type FlapKickRepository struct { @@ -44,10 +46,8 @@ func (repository *FlapKickRepository) Create(headerID int64, models []interface{ return err } } - _, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, flap_kick_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET flap_kick_checked = $2`, headerID, true) + + err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.FlapKickChecked) if err != nil { tx.Rollback() return err @@ -56,34 +56,11 @@ func (repository *FlapKickRepository) Create(headerID int64, models []interface{ } func (repository *FlapKickRepository) MarkHeaderChecked(headerID int64) error { - _, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, flap_kick_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET flap_kick_checked = $2`, headerID, true) - return err + return shared.MarkHeaderChecked(headerID, repository.db, constants.FlapKickChecked) } func (repository FlapKickRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { - var result []core.Header - err := repository.db.Select( - &result, - `SELECT headers.id, headers.block_number FROM headers - LEFT JOIN checked_headers on headers.id = header_id - WHERE (header_id ISNULL OR flap_kick_checked IS FALSE) - AND headers.block_number >= $1 - AND headers.block_number <= $2 - AND headers.eth_node_fingerprint = $3`, - startingBlockNumber, - endingBlockNumber, - repository.db.Node.ID, - ) - - if err != nil { - fmt.Println("Error:", err) - return result, err - } - - return result, nil + return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, repository.db, constants.FlapKickChecked) } func (repository *FlapKickRepository) SetDB(db *postgres.DB) { diff --git a/pkg/transformers/flap_kick/repository_test.go b/pkg/transformers/flap_kick/repository_test.go index 08ae63b9..1012548b 100644 --- a/pkg/transformers/flap_kick/repository_test.go +++ b/pkg/transformers/flap_kick/repository_test.go @@ -22,6 +22,7 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/transformers/flap_kick" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors" "github.com/vulcanize/vulcanizedb/test_config" @@ -46,7 +47,7 @@ var _ = Describe("Flap Kick Repository", func() { modelWithDifferentLogIdx := test_data.FlapKickModel modelWithDifferentLogIdx.LogIndex = modelWithDifferentLogIdx.LogIndex + 1 inputs := shared_behaviors.CreateBehaviorInputs{ - CheckedHeaderColumnName: "flap_kick_checked", + CheckedHeaderColumnName: constants.FlapKickChecked, LogEventTableName: "maker.flap_kick", TestModel: test_data.FlapKickModel, ModelWithDifferentLogIdx: modelWithDifferentLogIdx, @@ -80,7 +81,7 @@ var _ = Describe("Flap Kick Repository", func() { Describe("MarkHeaderChecked", func() { inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{ - CheckedHeaderColumnName: "flap_kick_checked", + CheckedHeaderColumnName: constants.FlapKickChecked, Repository: &flapKickRepository, } diff --git a/pkg/transformers/pit_file/debt_ceiling/repository.go b/pkg/transformers/pit_file/debt_ceiling/repository.go index cb547052..34e398f8 100644 --- a/pkg/transformers/pit_file/debt_ceiling/repository.go +++ b/pkg/transformers/pit_file/debt_ceiling/repository.go @@ -18,6 +18,8 @@ import ( "fmt" "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" ) type PitFileDebtCeilingRepository struct { @@ -49,11 +51,7 @@ func (repository PitFileDebtCeilingRepository) Create(headerID int64, models []i } } - _, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, pit_file_debt_ceiling_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET pit_file_debt_ceiling_checked = $2`, headerID, true) - + err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.PitFileDebtCeilingChecked) if err != nil { tx.Rollback() return err @@ -63,28 +61,11 @@ func (repository PitFileDebtCeilingRepository) Create(headerID int64, models []i } func (repository PitFileDebtCeilingRepository) MarkHeaderChecked(headerID int64) error { - _, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, pit_file_debt_ceiling_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET pit_file_debt_ceiling_checked = $2`, headerID, true) - return err + return shared.MarkHeaderChecked(headerID, repository.db, constants.PitFileDebtCeilingChecked) } func (repository PitFileDebtCeilingRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { - var result []core.Header - err := repository.db.Select( - &result, - `SELECT headers.id, headers.block_number FROM headers - LEFT JOIN checked_headers on headers.id = header_id - WHERE (header_id ISNULL OR pit_file_debt_ceiling_checked IS FALSE) - AND headers.block_number >= $1 - AND headers.block_number <= $2 - AND headers.eth_node_fingerprint = $3`, - startingBlockNumber, - endingBlockNumber, - repository.db.Node.ID, - ) - return result, err + return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, repository.db, constants.PitFileDebtCeilingChecked) } func (repository *PitFileDebtCeilingRepository) SetDB(db *postgres.DB) { diff --git a/pkg/transformers/pit_file/debt_ceiling/repository_test.go b/pkg/transformers/pit_file/debt_ceiling/repository_test.go index cad9ad17..06a7588b 100644 --- a/pkg/transformers/pit_file/debt_ceiling/repository_test.go +++ b/pkg/transformers/pit_file/debt_ceiling/repository_test.go @@ -22,6 +22,7 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/debt_ceiling" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors" "github.com/vulcanize/vulcanizedb/test_config" @@ -46,7 +47,7 @@ var _ = Describe("Pit file debt ceiling repository", func() { modelWithDifferentLogIdx := test_data.PitFileDebtCeilingModel modelWithDifferentLogIdx.LogIndex = modelWithDifferentLogIdx.LogIndex + 1 inputs := shared_behaviors.CreateBehaviorInputs{ - CheckedHeaderColumnName: "pit_file_debt_ceiling_checked", + CheckedHeaderColumnName: constants.PitFileDebtCeilingChecked, LogEventTableName: "maker.pit_file_debt_ceiling", TestModel: test_data.PitFileDebtCeilingModel, ModelWithDifferentLogIdx: modelWithDifferentLogIdx, @@ -74,7 +75,7 @@ var _ = Describe("Pit file debt ceiling repository", func() { Describe("MarkHeaderChecked", func() { inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{ - CheckedHeaderColumnName: "pit_file_debt_ceiling_checked", + CheckedHeaderColumnName: constants.PitFileDebtCeilingChecked, Repository: &pitFileDebtCeilingRepository, } diff --git a/pkg/transformers/pit_file/ilk/repository.go b/pkg/transformers/pit_file/ilk/repository.go index 3d07d32d..c186a638 100644 --- a/pkg/transformers/pit_file/ilk/repository.go +++ b/pkg/transformers/pit_file/ilk/repository.go @@ -18,6 +18,8 @@ import ( "fmt" "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" ) type PitFileIlkRepository struct { @@ -47,10 +49,8 @@ func (repository PitFileIlkRepository) Create(headerID int64, models []interface return err } } - _, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, pit_file_ilk_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET pit_file_ilk_checked = $2`, headerID, true) + + err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.PitFileIlkChecked) if err != nil { tx.Rollback() return err @@ -59,28 +59,11 @@ func (repository PitFileIlkRepository) Create(headerID int64, models []interface } func (repository PitFileIlkRepository) MarkHeaderChecked(headerID int64) error { - _, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, pit_file_ilk_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET pit_file_ilk_checked = $2`, headerID, true) - return err + return shared.MarkHeaderChecked(headerID, repository.db, constants.PitFileIlkChecked) } func (repository PitFileIlkRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { - var result []core.Header - err := repository.db.Select( - &result, - `SELECT headers.id, headers.block_number FROM headers - LEFT JOIN checked_headers on headers.id = header_id - WHERE (header_id ISNULL OR pit_file_ilk_checked IS FALSE) - AND headers.block_number >= $1 - AND headers.block_number <= $2 - AND headers.eth_node_fingerprint = $3`, - startingBlockNumber, - endingBlockNumber, - repository.db.Node.ID, - ) - return result, err + return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, repository.db, constants.PitFileIlkChecked) } func (repository *PitFileIlkRepository) SetDB(db *postgres.DB) { diff --git a/pkg/transformers/pit_file/ilk/repository_test.go b/pkg/transformers/pit_file/ilk/repository_test.go index 5de1f7bf..b99f6ad5 100644 --- a/pkg/transformers/pit_file/ilk/repository_test.go +++ b/pkg/transformers/pit_file/ilk/repository_test.go @@ -22,6 +22,7 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/ilk" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors" "github.com/vulcanize/vulcanizedb/test_config" @@ -46,7 +47,7 @@ var _ = Describe("Pit file ilk repository", func() { modelWithDifferentLogIdx := test_data.PitFileIlkModel modelWithDifferentLogIdx.LogIndex = modelWithDifferentLogIdx.LogIndex + 1 inputs := shared_behaviors.CreateBehaviorInputs{ - CheckedHeaderColumnName: "pit_file_ilk_checked", + CheckedHeaderColumnName: constants.PitFileIlkChecked, LogEventTableName: "maker.pit_file_ilk", TestModel: test_data.PitFileIlkModel, ModelWithDifferentLogIdx: modelWithDifferentLogIdx, @@ -75,7 +76,7 @@ var _ = Describe("Pit file ilk repository", func() { Describe("MarkHeaderChecked", func() { inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{ - CheckedHeaderColumnName: "pit_file_ilk_checked", + CheckedHeaderColumnName: constants.PitFileIlkChecked, Repository: &pitFileIlkRepository, } diff --git a/pkg/transformers/pit_file/stability_fee/repository.go b/pkg/transformers/pit_file/stability_fee/repository.go index 224e2c53..43ec8b3f 100644 --- a/pkg/transformers/pit_file/stability_fee/repository.go +++ b/pkg/transformers/pit_file/stability_fee/repository.go @@ -18,6 +18,8 @@ import ( "fmt" "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" ) type PitFileStabilityFeeRepository struct { @@ -49,11 +51,7 @@ func (repository PitFileStabilityFeeRepository) Create(headerID int64, models [] } } - _, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, pit_file_stability_fee_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET pit_file_stability_fee_checked = $2`, headerID, true) - + err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.PitFileStabilityFeeChecked) if err != nil { tx.Rollback() return err @@ -63,28 +61,11 @@ func (repository PitFileStabilityFeeRepository) Create(headerID int64, models [] } func (repository PitFileStabilityFeeRepository) MarkHeaderChecked(headerID int64) error { - _, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, pit_file_stability_fee_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET pit_file_stability_fee_checked = $2`, headerID, true) - return err + return shared.MarkHeaderChecked(headerID, repository.db, constants.PitFileStabilityFeeChecked) } func (repository PitFileStabilityFeeRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { - var result []core.Header - err := repository.db.Select( - &result, - `SELECT headers.id, headers.block_number FROM headers - LEFT JOIN checked_headers on headers.id = header_id - WHERE (header_id ISNULL OR pit_file_stability_fee_checked IS FALSE) - AND headers.block_number >= $1 - AND headers.block_number <= $2 - AND headers.eth_node_fingerprint = $3`, - startingBlockNumber, - endingBlockNumber, - repository.db.Node.ID, - ) - return result, err + return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, repository.db, constants.PitFileStabilityFeeChecked) } func (repository *PitFileStabilityFeeRepository) SetDB(db *postgres.DB) { diff --git a/pkg/transformers/pit_file/stability_fee/repository_test.go b/pkg/transformers/pit_file/stability_fee/repository_test.go index d1c5f504..701bc97a 100644 --- a/pkg/transformers/pit_file/stability_fee/repository_test.go +++ b/pkg/transformers/pit_file/stability_fee/repository_test.go @@ -22,6 +22,7 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/stability_fee" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors" "github.com/vulcanize/vulcanizedb/test_config" @@ -46,7 +47,7 @@ var _ = Describe("Pit file stability fee repository", func() { modelWithDifferentLogIdx := test_data.PitFileStabilityFeeModel modelWithDifferentLogIdx.LogIndex = modelWithDifferentLogIdx.LogIndex + 1 inputs := shared_behaviors.CreateBehaviorInputs{ - CheckedHeaderColumnName: "pit_file_stability_fee_checked", + CheckedHeaderColumnName: constants.PitFileStabilityFeeChecked, LogEventTableName: "maker.pit_file_stability_fee", TestModel: test_data.PitFileStabilityFeeModel, ModelWithDifferentLogIdx: modelWithDifferentLogIdx, @@ -74,7 +75,7 @@ var _ = Describe("Pit file stability fee repository", func() { Describe("MarkHeaderChecked", func() { inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{ - CheckedHeaderColumnName: "pit_file_stability_fee_checked", + CheckedHeaderColumnName: constants.PitFileStabilityFeeChecked, Repository: &pitFileStabilityFeeRepository, } diff --git a/pkg/transformers/price_feeds/repository.go b/pkg/transformers/price_feeds/repository.go index 63691b96..59dbab8c 100644 --- a/pkg/transformers/price_feeds/repository.go +++ b/pkg/transformers/price_feeds/repository.go @@ -18,6 +18,8 @@ import ( "fmt" "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" ) type PriceFeedRepository struct { @@ -43,10 +45,8 @@ func (repository PriceFeedRepository) Create(headerID int64, models []interface{ return err } } - _, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, price_feeds_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET price_feeds_checked = $2`, headerID, true) + + err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.PriceFeedsChecked) if err != nil { tx.Rollback() return err @@ -55,28 +55,11 @@ func (repository PriceFeedRepository) Create(headerID int64, models []interface{ } func (repository PriceFeedRepository) MarkHeaderChecked(headerID int64) error { - _, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, price_feeds_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET price_feeds_checked = $2`, headerID, true) - return err + return shared.MarkHeaderChecked(headerID, repository.db, constants.PriceFeedsChecked) } func (repository PriceFeedRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { - var result []core.Header - err := repository.db.Select( - &result, - `SELECT headers.id, headers.block_number FROM headers - LEFT JOIN checked_headers on headers.id = header_id - WHERE (header_id ISNULL OR price_feeds_checked IS FALSE) - AND headers.block_number >= $1 - AND headers.block_number <= $2 - AND headers.eth_node_fingerprint = $3`, - startingBlockNumber, - endingBlockNumber, - repository.db.Node.ID, - ) - return result, err + return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, repository.db, constants.PriceFeedsChecked) } func (repository *PriceFeedRepository) SetDB(db *postgres.DB) { diff --git a/pkg/transformers/price_feeds/repository_test.go b/pkg/transformers/price_feeds/repository_test.go index 1ebb2f5d..52765fd4 100644 --- a/pkg/transformers/price_feeds/repository_test.go +++ b/pkg/transformers/price_feeds/repository_test.go @@ -22,6 +22,7 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/transformers/price_feeds" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors" "github.com/vulcanize/vulcanizedb/test_config" @@ -46,7 +47,7 @@ var _ = Describe("Price feeds repository", func() { modelWithDifferentLogIdx := test_data.PriceFeedModel modelWithDifferentLogIdx.LogIndex = modelWithDifferentLogIdx.LogIndex + 1 inputs := shared_behaviors.CreateBehaviorInputs{ - CheckedHeaderColumnName: "price_feeds_checked", + CheckedHeaderColumnName: constants.PriceFeedsChecked, LogEventTableName: "maker.price_feeds", TestModel: test_data.PriceFeedModel, ModelWithDifferentLogIdx: modelWithDifferentLogIdx, @@ -75,7 +76,7 @@ var _ = Describe("Price feeds repository", func() { Describe("MarkHeaderChecked", func() { inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{ - CheckedHeaderColumnName: "price_feeds_checked", + CheckedHeaderColumnName: constants.PriceFeedsChecked, Repository: &priceFeedRepository, } diff --git a/pkg/transformers/shared/constants/checked_headers.go b/pkg/transformers/shared/constants/checked_headers.go new file mode 100644 index 00000000..b3cea78b --- /dev/null +++ b/pkg/transformers/shared/constants/checked_headers.go @@ -0,0 +1,19 @@ +package constants + +var ( + CatFileChopLumpChecked = "cat_file_chop_lump_checked" + CatFileFlipChecked = "cat_file_flip_checked" + CatFilePitVowChecked = "cat_file_pit_vow_checked" + DealChecked = "deal_checked" + DentChecked = "dent_checked" + DripDripChecked = "drip_drip_checked" + DripFileIlkChecked = "drip_file_ilk_checked" + DripFileRepoChecked = "drip_file_repo_checked" + DripFileVowChecked = "drip_file_vow_checked" + FlapKickChecked = "flap_kick_checked" + PitFileDebtCeilingChecked = "pit_file_debt_ceiling_checked" + PitFileIlkChecked = "pit_file_ilk_checked" + PitFileStabilityFeeChecked = "pit_file_stability_fee_checked" + PriceFeedsChecked = "price_feeds_checked" + TendChecked = "tend_checked" +) diff --git a/pkg/transformers/shared/repository.go b/pkg/transformers/shared/repository.go new file mode 100644 index 00000000..d74e09e5 --- /dev/null +++ b/pkg/transformers/shared/repository.go @@ -0,0 +1,40 @@ +package shared + +import ( + "database/sql" + "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" +) + +func MarkHeaderChecked(headerID int64, db *postgres.DB, checkedHeadersColumn string) error { + _, err := db.Exec(`INSERT INTO public.checked_headers (header_id, `+checkedHeadersColumn+`) + VALUES ($1, $2) + ON CONFLICT (header_id) DO + UPDATE SET `+checkedHeadersColumn+` = $2`, headerID, true) + return err +} + +func MarkHeaderCheckedInTransaction(headerID int64, tx *sql.Tx, checkedHeadersColumn string) error { + _, err := tx.Exec(`INSERT INTO public.checked_headers (header_id, `+checkedHeadersColumn+`) + VALUES ($1, $2) + ON CONFLICT (header_id) DO + UPDATE SET `+checkedHeadersColumn+` = $2`, headerID, true) + return err +} + +func MissingHeaders(startingBlockNumber, endingBlockNumber int64, db *postgres.DB, checkedHeadersColumn string) ([]core.Header, error) { + var result []core.Header + err := db.Select( + &result, + `SELECT headers.id, headers.block_number FROM headers + LEFT JOIN checked_headers on headers.id = header_id + WHERE (header_id ISNULL OR `+checkedHeadersColumn+` IS FALSE) + AND headers.block_number >= $1 + AND headers.block_number <= $2 + AND headers.eth_node_fingerprint = $3`, + startingBlockNumber, + endingBlockNumber, + db.Node.ID, + ) + return result, err +} diff --git a/pkg/transformers/tend/repository.go b/pkg/transformers/tend/repository.go index 77228629..0b0bcd72 100644 --- a/pkg/transformers/tend/repository.go +++ b/pkg/transformers/tend/repository.go @@ -18,6 +18,8 @@ import ( "fmt" "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" ) type TendRepository struct { @@ -48,11 +50,8 @@ func (repository TendRepository) Create(headerId int64, models []interface{}) er return err } } - _, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, tend_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET tend_checked = $2`, headerId, true) + err = shared.MarkHeaderCheckedInTransaction(headerId, tx, constants.TendChecked) if err != nil { tx.Rollback() return err @@ -61,29 +60,11 @@ func (repository TendRepository) Create(headerId int64, models []interface{}) er } func (repository TendRepository) MarkHeaderChecked(headerId int64) error { - _, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, tend_checked) - VALUES ($1, $2) - ON CONFLICT (header_id) DO - UPDATE SET tend_checked = $2`, headerId, true) - return err + return shared.MarkHeaderChecked(headerId, repository.db, constants.TendChecked) } func (repository TendRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { - var result []core.Header - err := repository.db.Select( - &result, - `SELECT headers.id, headers.block_number FROM headers - LEFT JOIN checked_headers on headers.id = header_id - WHERE (header_id ISNULL OR tend_checked IS FALSE) - AND headers.block_number >= $1 - AND headers.block_number <= $2 - AND headers.eth_node_fingerprint = $3`, - startingBlockNumber, - endingBlockNumber, - repository.db.Node.ID, - ) - - return result, err + return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, repository.db, constants.TendChecked) } func (repository *TendRepository) SetDB(db *postgres.DB) { diff --git a/pkg/transformers/tend/repository_test.go b/pkg/transformers/tend/repository_test.go index 65a2672a..8cd61f2f 100644 --- a/pkg/transformers/tend/repository_test.go +++ b/pkg/transformers/tend/repository_test.go @@ -21,6 +21,7 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/fakes" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" "github.com/vulcanize/vulcanizedb/pkg/transformers/tend" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors" @@ -46,7 +47,7 @@ var _ = Describe("TendRepository", func() { modelWithDifferentLogIdx := test_data.TendModel modelWithDifferentLogIdx.LogIndex = modelWithDifferentLogIdx.LogIndex + 1 inputs := shared_behaviors.CreateBehaviorInputs{ - CheckedHeaderColumnName: "tend_checked", + CheckedHeaderColumnName: constants.TendChecked, LogEventTableName: "maker.tend", TestModel: test_data.TendModel, ModelWithDifferentLogIdx: modelWithDifferentLogIdx, @@ -84,7 +85,7 @@ var _ = Describe("TendRepository", func() { Describe("MarkHeaderChecked", func() { inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{ - CheckedHeaderColumnName: "tend_checked", + CheckedHeaderColumnName: constants.TendChecked, Repository: &tendRepository, }