Skip to content

Commit

Permalink
refactor(seeder): add a way to figure out how to use the specific bin…
Browse files Browse the repository at this point in the history
…d vars
  • Loading branch information
danvergara committed May 8, 2021
1 parent 56284c8 commit 9edad16
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/seeder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func handleArgs() {
if err != nil {
log.Fatalf("Error opening DB: %v", err)
}
seeds.Execute(db, args[1:]...)
seeds.Execute(db, cfg.Driver(), args[1:]...)
os.Exit(0)
}
}
Expand Down
12 changes: 11 additions & 1 deletion db/seeds/customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ import (
// CustomerSeed seeds the database with customers.
func (s Seed) CustomerSeed() {
for i := 0; i < 100; i++ {
var err error

// execute query.
_, err := s.db.Exec(`INSERT INTO customers(name, email) VALUES ($1, $2)`, faker.Name(), faker.Email())
switch s.driver {
case "postgres":
_, err = s.db.Exec(`INSERT INTO customers(name, email) VALUES ($1, $2)`, faker.Name(), faker.Email())
case "mysql":
_, err = s.db.Exec(`INSERT INTO customers(name, email) VALUES (?, ?)`, faker.Name(), faker.Email())
default:
log.Println("unsupported driver")
}

if err != nil {
log.Fatalf("error seeding customers: %v", err)
}
Expand Down
12 changes: 11 additions & 1 deletion db/seeds/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@ import (
// ProductSeed seeds product data.
func (s Seed) ProductSeed() {
for i := 0; i < 100; i++ {
var err error

// execute query.
_, err := s.db.Exec(`INSERT INTO products(name, price) VALUES ($1, $2)`, faker.Word(), rand.Float32())
switch s.driver {
case "postgres":
_, err = s.db.Exec(`INSERT INTO products(name, price) VALUES ($1, $2)`, faker.Word(), rand.Float32())
case "mysql":
_, err = s.db.Exec(`INSERT INTO products(name, price) VALUES (?, ?)`, faker.Word(), rand.Float32())
default:
log.Println("unsupported driver")
}

if err != nil {
log.Fatalf("error seeding products: %v", err)
}
Expand Down
8 changes: 5 additions & 3 deletions db/seeds/seeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import (

// Seed type.
type Seed struct {
db *sqlx.DB
db *sqlx.DB
driver string
}

// Execute will executes the given seeder method.
func Execute(db *sqlx.DB, seedMethodNames ...string) {
func Execute(db *sqlx.DB, driver string, seedMethodNames ...string) {
s := Seed{
db: db,
db: db,
driver: driver,
}

seedType := reflect.TypeOf(s)
Expand Down
12 changes: 11 additions & 1 deletion db/seeds/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ import (
// UserSeed seeds the database with users.
func (s Seed) UserSeed() {
for i := 0; i < 100; i++ {
var err error

// execute query.
_, err := s.db.Exec(`INSERT INTO users(username) VALUES ($1)`, faker.Name())
switch s.driver {
case "postgres":
_, err = s.db.Exec(`INSERT INTO users(username) VALUES ($1)`, faker.Name())
case "mysql":
_, err = s.db.Exec(`INSERT INTO users(username) VALUES (?)`, faker.Name())
default:
log.Println("unsupported driver")
}

if err != nil {
log.Fatalf("error seeding users: %v", err)
}
Expand Down

0 comments on commit 9edad16

Please sign in to comment.