Skip to content

Commit

Permalink
imager: fix style warnings and remove old schema file
Browse files Browse the repository at this point in the history
  • Loading branch information
jsec committed Nov 18, 2024
1 parent c460b89 commit c8832ee
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 217 deletions.
4 changes: 2 additions & 2 deletions internal/database/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func Connect(ctx context.Context) (*pgxpool.Pool, error) {

config, err := pgxpool.ParseConfig(connString)
if err != nil {
return nil, fmt.Errorf("Error parsing connection string: %w", err)
return nil, fmt.Errorf("error parsing connection string: %w", err)
}

config.AfterConnect = func(ctx context.Context, c *pgx.Conn) error {
Expand All @@ -25,7 +25,7 @@ func Connect(ctx context.Context) (*pgxpool.Pool, error) {

pool, err := pgxpool.NewWithConfig(ctx, config)
if err != nil {
return nil, fmt.Errorf("Error creating connection: %w", err)
return nil, fmt.Errorf("error creating connection: %w", err)
}

return pool, nil
Expand Down
6 changes: 3 additions & 3 deletions internal/imager/circuit.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ type circuit struct {
func (i Imager) loadCircuits(ctx context.Context, tx pgx.Tx) error {
file, err := os.OpenFile("data/circuits.csv", os.O_RDONLY, os.ModePerm)
if err != nil {
return fmt.Errorf("Error opening circuits CSV file: %w", err)
return fmt.Errorf("error opening circuits CSV file: %w", err)
}
defer file.Close()

var circuits []*circuit

if err = gocsv.UnmarshalFile(file, &circuits); err != nil {
return fmt.Errorf("Error marshaling circuits CSV file: %w", err)
return fmt.Errorf("error marshaling circuits CSV file: %w", err)
}

records := []database.SaveCircuitsParams{}
Expand All @@ -54,7 +54,7 @@ func (i Imager) loadCircuits(ctx context.Context, tx pgx.Tx) error {

_, err = i.db.WithTx(tx).SaveCircuits(ctx, records)
if err != nil {
return fmt.Errorf("Error saving circuits: %w", err)
return fmt.Errorf("error saving circuits: %w", err)
}

fmt.Println("[Circuits] seeding complete")
Expand Down
18 changes: 9 additions & 9 deletions internal/imager/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ type constructor struct {
func (i Imager) loadConstructors(ctx context.Context, tx pgx.Tx) error {
file, err := os.OpenFile("data/constructors.csv", os.O_RDONLY, os.ModePerm)
if err != nil {
return fmt.Errorf("Error opening constructors CSV file: %w", err)
return fmt.Errorf("error opening constructors CSV file: %w", err)
}
defer file.Close()

var constructors []*constructor

if err = gocsv.UnmarshalFile(file, &constructors); err != nil {
return fmt.Errorf("Error marshaling constructors CSV file: %w", err)
return fmt.Errorf("error marshaling constructors CSV file: %w", err)
}

records := []database.SaveConstructorsParams{}
Expand All @@ -46,7 +46,7 @@ func (i Imager) loadConstructors(ctx context.Context, tx pgx.Tx) error {

_, err = i.db.WithTx(tx).SaveConstructors(ctx, records)
if err != nil {
return fmt.Errorf("Error saving constructors: %w", err)
return fmt.Errorf("error saving constructors: %w", err)
}

return nil
Expand All @@ -63,14 +63,14 @@ type constructorResult struct {
func (i Imager) loadConstructorResults(ctx context.Context, tx pgx.Tx) error {
file, err := os.OpenFile("data/constructor_results.csv", os.O_RDONLY, os.ModePerm)
if err != nil {
return fmt.Errorf("Error opening constructor results CSV file: %w", err)
return fmt.Errorf("error opening constructor results CSV file: %w", err)
}
defer file.Close()

var results []*constructorResult

if err = gocsv.UnmarshalFile(file, &results); err != nil {
return fmt.Errorf("Error marshaling constructor results CSV file: %w", err)
return fmt.Errorf("error marshaling constructor results CSV file: %w", err)
}

records := []database.SaveConstructorResultsParams{}
Expand All @@ -87,7 +87,7 @@ func (i Imager) loadConstructorResults(ctx context.Context, tx pgx.Tx) error {

_, err = i.db.WithTx(tx).SaveConstructorResults(ctx, records)
if err != nil {
return fmt.Errorf("Error saving constructor results: %w", err)
return fmt.Errorf("error saving constructor results: %w", err)
}

fmt.Println("[Constructor Results] seeding complete")
Expand All @@ -107,14 +107,14 @@ type constructorStanding struct {
func (i Imager) loadConstructorStandings(ctx context.Context, tx pgx.Tx) error {
file, err := os.OpenFile("data/constructor_standings.csv", os.O_RDONLY, os.ModePerm)
if err != nil {
return fmt.Errorf("Error opening constructor standings CSV file: %w", err)
return fmt.Errorf("error opening constructor standings CSV file: %w", err)
}
defer file.Close()

var standings []*constructorStanding

if err = gocsv.UnmarshalFile(file, &standings); err != nil {
return fmt.Errorf("Error marshaling constructor standings CSV file: %w", err)
return fmt.Errorf("error marshaling constructor standings CSV file: %w", err)
}

records := []database.SaveConstructorStandingsParams{}
Expand All @@ -133,7 +133,7 @@ func (i Imager) loadConstructorStandings(ctx context.Context, tx pgx.Tx) error {

_, err = i.db.WithTx(tx).SaveConstructorStandings(ctx, records)
if err != nil {
return fmt.Errorf("Error saving constructor standings: %w", err)
return fmt.Errorf("error saving constructor standings: %w", err)
}

fmt.Println("[Constructor Standings] seeding complete")
Expand Down
16 changes: 8 additions & 8 deletions internal/imager/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ const zipName = "seed.zip"
func FetchSeedData() error {
out, err := os.Create(zipName)
if err != nil {
return fmt.Errorf("Error creating seed file: %w", err)
return fmt.Errorf("error creating seed file: %w", err)
}
defer out.Close()

resp, err := http.Get("http://ergast.com/downloads/f1db_csv.zip")
if err != nil {
return fmt.Errorf("Error fetching seed data: %w", err)
return fmt.Errorf("error fetching seed data: %w", err)
}
defer resp.Body.Close()

_, err = io.Copy(out, resp.Body)
if err != nil {
return fmt.Errorf("Error saving seed data: %w", err)
return fmt.Errorf("error saving seed data: %w", err)
}

return UnzipSeedData()
Expand All @@ -34,31 +34,31 @@ func FetchSeedData() error {
func UnzipSeedData() error {
r, err := zip.OpenReader(zipName)
if err != nil {
return fmt.Errorf("Error opening zipfile: %w", err)
return fmt.Errorf("error opening zipfile: %w", err)
}
defer r.Close()

err = os.MkdirAll("data", 0755)
if err != nil {
return fmt.Errorf("Error creating data dir: %w", err)
return fmt.Errorf("error creating data dir: %w", err)
}

for _, f := range r.File {
rc, err := f.Open()
if err != nil {
return fmt.Errorf("Error extracting file %s: %w", f.Name, err)
return fmt.Errorf("error extracting file %s: %w", f.Name, err)
}
defer rc.Close()

filePath := fmt.Sprintf("data/%s", f.Name)
file, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("Error creating uncompressed file %s: %w", f.Name, err)
return fmt.Errorf("error creating uncompressed file %s: %w", f.Name, err)
}

_, err = io.Copy(file, rc)
if err != nil {
return fmt.Errorf("Error decompressing file %s: %w", f.Name, err)
return fmt.Errorf("error decompressing file %s: %w", f.Name, err)
}
}

Expand Down
12 changes: 6 additions & 6 deletions internal/imager/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ type driver struct {
func (i Imager) loadDrivers(ctx context.Context, tx pgx.Tx) error {
file, err := os.OpenFile("data/drivers.csv", os.O_RDONLY, os.ModePerm)
if err != nil {
return fmt.Errorf("Error opening driver CSV file: %w", err)
return fmt.Errorf("error opening driver CSV file: %w", err)
}
defer file.Close()

var drivers []*driver

if err = gocsv.UnmarshalFile(file, &drivers); err != nil {
return fmt.Errorf("Error marshaling driver CSV file: %w", err)
return fmt.Errorf("error marshaling driver CSV file: %w", err)
}

records := []database.SaveDriversParams{}
Expand All @@ -54,7 +54,7 @@ func (i Imager) loadDrivers(ctx context.Context, tx pgx.Tx) error {

_, err = i.db.WithTx(tx).SaveDrivers(ctx, records)
if err != nil {
return fmt.Errorf("Error saving drivers: %w", err)
return fmt.Errorf("error saving drivers: %w", err)
}

fmt.Println("[Drivers] seeding complete")
Expand All @@ -74,14 +74,14 @@ type driverStanding struct {
func (i Imager) loadDriverStandings(ctx context.Context, tx pgx.Tx) error {
file, err := os.OpenFile("data/driver_standings.csv", os.O_RDONLY, os.ModePerm)
if err != nil {
return fmt.Errorf("Error opening driver standings CSV file: %w", err)
return fmt.Errorf("error opening driver standings CSV file: %w", err)
}
defer file.Close()

var standings []*driverStanding

if err = gocsv.UnmarshalFile(file, &standings); err != nil {
return fmt.Errorf("Error marshaling driver standings CSV file: %w", err)
return fmt.Errorf("error marshaling driver standings CSV file: %w", err)
}

records := []database.SaveDriverStandingsParams{}
Expand All @@ -100,7 +100,7 @@ func (i Imager) loadDriverStandings(ctx context.Context, tx pgx.Tx) error {

_, err = i.db.WithTx(tx).SaveDriverStandings(ctx, records)
if err != nil {
return fmt.Errorf("Error saving driver standings: %w", err)
return fmt.Errorf("error saving driver standings: %w", err)
}

fmt.Println("[Driver Standings] seeding complete")
Expand Down
13 changes: 11 additions & 2 deletions internal/imager/imager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,23 @@ func Run(ctx context.Context) error {

tx, err := pool.Begin(ctx)
if err != nil {
return fmt.Errorf("Error acquiring transaction: %w", err)
return fmt.Errorf("error acquiring transaction: %w", err)
}
defer tx.Rollback(ctx)

imager := Imager{
db: database.New(pool),
}

return imager.Seed(ctx, tx)
if err = imager.Seed(ctx, tx); err != nil {
return err
}

if err = tx.Commit(ctx); err != nil {
return err
}

return nil
}

func (i Imager) Seed(ctx context.Context, tx pgx.Tx) error {
Expand Down
6 changes: 3 additions & 3 deletions internal/imager/laptime.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ type lapTime struct {
func (i Imager) loadLapTimes(ctx context.Context, tx pgx.Tx) error {
file, err := os.OpenFile("data/lap_times.csv", os.O_RDONLY, os.ModePerm)
if err != nil {
return fmt.Errorf("Error opening lap times CSV file: %w", err)
return fmt.Errorf("error opening lap times CSV file: %w", err)
}
defer file.Close()

var lapTimes []*lapTime

if err = gocsv.UnmarshalFile(file, &lapTimes); err != nil {
return fmt.Errorf("Error marshaling lap times CSV file: %w", err)
return fmt.Errorf("error marshaling lap times CSV file: %w", err)
}

records := []database.SaveLapTimesParams{}
Expand All @@ -47,7 +47,7 @@ func (i Imager) loadLapTimes(ctx context.Context, tx pgx.Tx) error {

_, err = i.db.WithTx(tx).SaveLapTimes(ctx, records)
if err != nil {
return fmt.Errorf("Error saving lap times: %w", err)
return fmt.Errorf("error saving lap times: %w", err)
}

fmt.Println("[Lap Times] seeding complete")
Expand Down
6 changes: 3 additions & 3 deletions internal/imager/pitstop.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ type pitStop struct {
func (i Imager) loadPitStops(ctx context.Context, tx pgx.Tx) error {
file, err := os.OpenFile("data/pit_stops.csv", os.O_RDONLY, os.ModePerm)
if err != nil {
return fmt.Errorf("Error opening pit stops CSV file: %w", err)
return fmt.Errorf("error opening pit stops CSV file: %w", err)
}
defer file.Close()

var pitStops []*pitStop

if err = gocsv.UnmarshalFile(file, &pitStops); err != nil {
return fmt.Errorf("Error marshaling pit stops CSV file: %w", err)
return fmt.Errorf("error marshaling pit stops CSV file: %w", err)
}

records := []database.SavePitStopsParams{}
Expand All @@ -49,7 +49,7 @@ func (i Imager) loadPitStops(ctx context.Context, tx pgx.Tx) error {

_, err = i.db.WithTx(tx).SavePitStops(ctx, records)
if err != nil {
return fmt.Errorf("Error saving pit stops: %w", err)
return fmt.Errorf("error saving pit stops: %w", err)
}

fmt.Println("[Pit Stops] seeding complete")
Expand Down
6 changes: 3 additions & 3 deletions internal/imager/qualifying.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ type qualifying struct {
func (i Imager) loadQualifying(ctx context.Context, tx pgx.Tx) error {
file, err := os.OpenFile("data/qualifying.csv", os.O_RDONLY, os.ModePerm)
if err != nil {
return fmt.Errorf("Error opening qualifying CSV file: %w", err)
return fmt.Errorf("error opening qualifying CSV file: %w", err)
}
defer file.Close()

var results []*qualifying

if err = gocsv.UnmarshalFile(file, &results); err != nil {
return fmt.Errorf("Error marshaling qualifying CSV file: %w", err)
return fmt.Errorf("error marshaling qualifying CSV file: %w", err)
}

records := []database.SaveQualifyingResultsParams{}
Expand All @@ -53,7 +53,7 @@ func (i Imager) loadQualifying(ctx context.Context, tx pgx.Tx) error {

_, err = i.db.WithTx(tx).SaveQualifyingResults(ctx, records)
if err != nil {
return fmt.Errorf("Error saving qualifying results: %w", err)
return fmt.Errorf("error saving qualifying results: %w", err)
}

fmt.Println("[Qualifying] seeding complete")
Expand Down
6 changes: 3 additions & 3 deletions internal/imager/race.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ type race struct {
func (i Imager) loadRaces(ctx context.Context, tx pgx.Tx) error {
file, err := os.OpenFile("data/races.csv", os.O_RDONLY, os.ModePerm)
if err != nil {
return fmt.Errorf("Error opening races CSV file: %w", err)
return fmt.Errorf("error opening races CSV file: %w", err)
}
defer file.Close()

var races []*race

if err = gocsv.UnmarshalFile(file, &races); err != nil {
return fmt.Errorf("Error marshaling races CSV file: %w", err)
return fmt.Errorf("error marshaling races CSV file: %w", err)
}

records := []database.SaveRacesParams{}
Expand Down Expand Up @@ -71,7 +71,7 @@ func (i Imager) loadRaces(ctx context.Context, tx pgx.Tx) error {

_, err = i.db.WithTx(tx).SaveRaces(ctx, records)
if err != nil {
return fmt.Errorf("Error saving races: %w", err)
return fmt.Errorf("error saving races: %w", err)
}

fmt.Println("[Races] seeding complete")
Expand Down
Loading

0 comments on commit c8832ee

Please sign in to comment.