ipld-eth-beacon-indexer/pkg/database/sql/postgres/database.go

48 lines
1.0 KiB
Go
Raw Normal View History

Testing, CI/CD and DB connections (#10) * Update cobra to require `head` or `historic` when using `capture`. * A very generic package for implementing PGX driver I copied most of the code from the `statediff` service from within geth. The idea is that I can create formal DB packages, that can be utilized in other projects down the road. * Put PGX and other future Postgres Drivers behind SQL package This PR makes allows users to provide a config, along with a driver string. It will then provide the respective driver. * Add DB Connection and Logging * Utilize LogRus * Create a DB connection using PGX. * Create an internal boot package for starting the application. * Code clean up + Beacon Chain Connection This concludes all the code needed to connect to the DB and beacon node. We will no longer reference the lighthouse client because this application should work interchangeably with any beacon node. I have also standardized logging. * Last second clean ups * Utilize Ginkgo toreplace `testing` library, and add CI/CD. (#9) * Utilize Ginkgo and replace `testing` library. * Add TOC to docs * Add Docker specific files * Remove -e * Update on-pr-manual.yml * Add depth * Add repositories * Remove $ from path * Update path for make * Setup Go and Ginkgo * Use go mod download * Use go install * Update on-pr-manual.yml * Use latest * Remove install of GINKGO * Add explicit gopath * Explicitly specify the gopath * Update on-pr-manual.yml * Update on-pr-manual.yml * Update on-pr-manual.yml * Update on-pr-manual.yml * Update on-pr-manual.yml * Update on-pr-manual.yml * Use which ginkgo * Try with make now * Final working Make
2022-04-22 12:31:57 +00:00
package postgres
import (
"context"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/vulcanize/ipld-ethcl-indexer/pkg/database/sql"
)
var _ sql.Database = &DB{}
// NewPostgresDB returns a postgres.DB using the provided Config and driver type.
func NewPostgresDB(c Config) (*DB, error) {
var driver *pgxDriver
driver, err := createDriver(c)
if err != nil {
return nil, err
}
return &DB{driver}, nil
}
// Create a driver based on the config
func createDriver(c Config) (*pgxDriver, error) {
switch c.Driver {
case PGX:
log.Debug("Creating New Driver")
driver, err := newPGXDriver(context.Background(), c)
if err != nil {
return nil, fmt.Errorf("Error Creating Driver, err: %e", err)
}
log.Info("Successfully created a driver for PGX")
return driver, nil
default:
log.Error("Couldnt find a driver to create for: ", c.Driver)
return nil, fmt.Errorf("Can't find a driver to create")
}
}
// DB implements sql.Database using a configured driver and Postgres statement syntax
type DB struct {
sql.Driver
}