From 9edad16f740aeee66bcbfaced26976d226b36036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Omar=20Vergara=20P=C3=A9rez?= Date: Sat, 8 May 2021 00:57:04 -0500 Subject: [PATCH] refactor(seeder): add a way to figure out how to use the specific bind vars --- cmd/seeder/main.go | 2 +- db/seeds/customer.go | 12 +++++++++++- db/seeds/product.go | 12 +++++++++++- db/seeds/seeder.go | 8 +++++--- db/seeds/user.go | 12 +++++++++++- 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/cmd/seeder/main.go b/cmd/seeder/main.go index d636b71..6d29d24 100644 --- a/cmd/seeder/main.go +++ b/cmd/seeder/main.go @@ -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) } } diff --git a/db/seeds/customer.go b/db/seeds/customer.go index bc18146..aa724d9 100644 --- a/db/seeds/customer.go +++ b/db/seeds/customer.go @@ -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) } diff --git a/db/seeds/product.go b/db/seeds/product.go index c3f21bb..d91aa41 100644 --- a/db/seeds/product.go +++ b/db/seeds/product.go @@ -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) } diff --git a/db/seeds/seeder.go b/db/seeds/seeder.go index a048e61..2856a2d 100644 --- a/db/seeds/seeder.go +++ b/db/seeds/seeder.go @@ -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) diff --git a/db/seeds/user.go b/db/seeds/user.go index 795b236..ebdb997 100644 --- a/db/seeds/user.go +++ b/db/seeds/user.go @@ -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) }