support faker
This commit is contained in:
10
cmd/seed.go
10
cmd/seed.go
@@ -11,6 +11,7 @@ import (
|
|||||||
"atom/contracts"
|
"atom/contracts"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"github.com/brianvoe/gofakeit/v6"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"go.uber.org/dig"
|
"go.uber.org/dig"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -22,14 +23,14 @@ var seedCmd = &cobra.Command{
|
|||||||
Short: "seed databases",
|
Short: "seed databases",
|
||||||
Long: `seed your database with data using seeders.`,
|
Long: `seed your database with data using seeders.`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return container.Container.Invoke(func(seedersContainer SeedersContainer) error {
|
return container.Container.Invoke(func(c SeedersContainer) error {
|
||||||
if len(seedersContainer.Seeders) == 0 {
|
if len(c.Seeders) == 0 {
|
||||||
log.Print("no seeder exists")
|
log.Print("no seeder exists")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, seeder := range seedersContainer.Seeders {
|
for _, seeder := range c.Seeders {
|
||||||
seeder.Run(seedersContainer.DB)
|
seeder.Run(c.Faker, c.DB)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@@ -47,5 +48,6 @@ type SeedersContainer struct {
|
|||||||
dig.In
|
dig.In
|
||||||
|
|
||||||
DB *gorm.DB
|
DB *gorm.DB
|
||||||
|
Faker *gofakeit.Faker
|
||||||
Seeders []contracts.Seeder `group:"seeders"`
|
Seeders []contracts.Seeder `group:"seeders"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
package contracts
|
package contracts
|
||||||
|
|
||||||
import "gorm.io/gorm"
|
import (
|
||||||
|
"github.com/brianvoe/gofakeit/v6"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
// Migration route interface
|
// Migration route interface
|
||||||
type Seeder interface {
|
type Seeder interface {
|
||||||
Run(*gorm.DB)
|
Run(*gofakeit.Faker, *gorm.DB)
|
||||||
}
|
}
|
||||||
|
|||||||
44
database/seeders/migration.go
Executable file
44
database/seeders/migration.go
Executable file
@@ -0,0 +1,44 @@
|
|||||||
|
package seeders
|
||||||
|
|
||||||
|
import (
|
||||||
|
"atom/container"
|
||||||
|
"atom/contracts"
|
||||||
|
"atom/database/models"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/brianvoe/gofakeit/v6"
|
||||||
|
"go.uber.org/dig"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
if err := container.Container.Provide(NewMigrationSeeder, dig.Group("seeders")); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type MigrationSeeder struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMigrationSeeder() contracts.Seeder {
|
||||||
|
return &MigrationSeeder{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *MigrationSeeder) Run(faker *gofakeit.Faker, db *gorm.DB) {
|
||||||
|
times := 10
|
||||||
|
for i := 0; i < times; i++ {
|
||||||
|
data := s.Generate(faker, i)
|
||||||
|
if i == 0 {
|
||||||
|
stmt := &gorm.Statement{DB: db}
|
||||||
|
_ = stmt.Parse(&data)
|
||||||
|
log.Printf("seeding %s for %d times", stmt.Schema.Table, times)
|
||||||
|
}
|
||||||
|
db.Create(&data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *MigrationSeeder) Generate(faker *gofakeit.Faker, idx int) models.Migration {
|
||||||
|
return models.Migration{
|
||||||
|
// fill model fields
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"atom/contracts"
|
"atom/contracts"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"github.com/brianvoe/gofakeit/v6"
|
||||||
"go.uber.org/dig"
|
"go.uber.org/dig"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
@@ -24,12 +25,16 @@ func NewPlaceholderSeeder() contracts.Seeder {
|
|||||||
|
|
||||||
type Placeholder struct {
|
type Placeholder struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
|
|
||||||
|
Username string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PlaceholderSeeder) Run(db *gorm.DB) {
|
func (s *PlaceholderSeeder) Run(faker *gofakeit.Faker, db *gorm.DB) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PlaceholderSeeder) Generate(idx int) Placeholder {
|
func (s *PlaceholderSeeder) Generate(faker *gofakeit.Faker, idx int) Placeholder {
|
||||||
return Placeholder{}
|
return Placeholder{
|
||||||
|
Username: faker.Name(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
6
go.mod
6
go.mod
@@ -3,6 +3,7 @@ module atom
|
|||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/brianvoe/gofakeit/v6 v6.20.1
|
||||||
github.com/gin-gonic/gin v1.8.1
|
github.com/gin-gonic/gin v1.8.1
|
||||||
github.com/go-gormigrate/gormigrate/v2 v2.0.2
|
github.com/go-gormigrate/gormigrate/v2 v2.0.2
|
||||||
github.com/go-micro/plugins/v4/client/grpc v1.1.0
|
github.com/go-micro/plugins/v4/client/grpc v1.1.0
|
||||||
@@ -12,7 +13,6 @@ require (
|
|||||||
github.com/rogeecn/gen v1.0.4
|
github.com/rogeecn/gen v1.0.4
|
||||||
github.com/spf13/cobra v1.5.0
|
github.com/spf13/cobra v1.5.0
|
||||||
github.com/spf13/viper v1.15.0
|
github.com/spf13/viper v1.15.0
|
||||||
github.com/stretchr/testify v1.8.1
|
|
||||||
go-micro.dev/v4 v4.8.0
|
go-micro.dev/v4 v4.8.0
|
||||||
go.uber.org/dig v1.15.0
|
go.uber.org/dig v1.15.0
|
||||||
go.uber.org/zap v1.21.0
|
go.uber.org/zap v1.21.0
|
||||||
@@ -20,6 +20,7 @@ require (
|
|||||||
gorm.io/driver/mysql v1.4.0
|
gorm.io/driver/mysql v1.4.0
|
||||||
gorm.io/gen v0.3.19
|
gorm.io/gen v0.3.19
|
||||||
gorm.io/gorm v1.24.0
|
gorm.io/gorm v1.24.0
|
||||||
|
gorm.io/plugin/dbresolver v1.3.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@@ -29,7 +30,6 @@ require (
|
|||||||
github.com/bitly/go-simplejson v0.5.0 // indirect
|
github.com/bitly/go-simplejson v0.5.0 // indirect
|
||||||
github.com/cloudflare/circl v1.2.0 // indirect
|
github.com/cloudflare/circl v1.2.0 // indirect
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
|
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
|
||||||
github.com/emirpasic/gods v1.18.1 // indirect
|
github.com/emirpasic/gods v1.18.1 // indirect
|
||||||
github.com/fsnotify/fsnotify v1.6.0 // indirect
|
github.com/fsnotify/fsnotify v1.6.0 // indirect
|
||||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||||
@@ -63,7 +63,6 @@ require (
|
|||||||
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
|
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
|
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
|
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
|
||||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||||
github.com/sergi/go-diff v1.2.0 // indirect
|
github.com/sergi/go-diff v1.2.0 // indirect
|
||||||
github.com/sirupsen/logrus v1.8.1 // indirect
|
github.com/sirupsen/logrus v1.8.1 // indirect
|
||||||
@@ -94,5 +93,4 @@ require (
|
|||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
gorm.io/datatypes v1.0.7 // indirect
|
gorm.io/datatypes v1.0.7 // indirect
|
||||||
gorm.io/hints v1.1.0 // indirect
|
gorm.io/hints v1.1.0 // indirect
|
||||||
gorm.io/plugin/dbresolver v1.3.0 // indirect
|
|
||||||
)
|
)
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -61,6 +61,8 @@ github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZx
|
|||||||
github.com/bitly/go-simplejson v0.5.0 h1:6IH+V8/tVMab511d5bn4M7EwGXZf9Hj6i2xSwkNEM+Y=
|
github.com/bitly/go-simplejson v0.5.0 h1:6IH+V8/tVMab511d5bn4M7EwGXZf9Hj6i2xSwkNEM+Y=
|
||||||
github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA=
|
github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA=
|
||||||
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
|
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
|
||||||
|
github.com/brianvoe/gofakeit/v6 v6.20.1 h1:8ihJ60OvPnPJ2W6wZR7M+TTeaZ9bml0z6oy4gvyJ/ek=
|
||||||
|
github.com/brianvoe/gofakeit/v6 v6.20.1/go.mod h1:Ow6qC71xtwm79anlwKRlWZW6zVq9D2XHE4QSSMP/rU8=
|
||||||
github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
|
github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
|
||||||
github.com/bwesterb/go-ristretto v1.2.1/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
|
github.com/bwesterb/go-ristretto v1.2.1/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
|
||||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||||
@@ -226,6 +228,7 @@ github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
|
|||||||
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
|
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
|
||||||
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
|
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
|
||||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||||
|
github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0=
|
||||||
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
|
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
|
||||||
github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
|
github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
|
||||||
github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8=
|
github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8=
|
||||||
@@ -245,6 +248,7 @@ github.com/jackc/pgmock v0.0.0-20201204152224-4fe30f7445fd/go.mod h1:hrBW0Enj2AZ
|
|||||||
github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65/go.mod h1:5R2h2EEX+qri8jOWMbJCtaPWkrrNc7OHwsp2TCqp7ak=
|
github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65/go.mod h1:5R2h2EEX+qri8jOWMbJCtaPWkrrNc7OHwsp2TCqp7ak=
|
||||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||||
|
github.com/jackc/pgproto3 v1.1.0 h1:FYYE4yRw+AgI8wXIinMlNjBbp/UitDJwfj5LqqewP1A=
|
||||||
github.com/jackc/pgproto3 v1.1.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78=
|
github.com/jackc/pgproto3 v1.1.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78=
|
||||||
github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db/go.mod h1:bhq50y+xrl9n5mRYyCBFKkpRVTLYJVWeCc+mEAI3yXA=
|
github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db/go.mod h1:bhq50y+xrl9n5mRYyCBFKkpRVTLYJVWeCc+mEAI3yXA=
|
||||||
github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod h1:uH0AWtUmuShn0bcesswc4aBTWGvw0cAxIJp+6OB//Wg=
|
github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod h1:uH0AWtUmuShn0bcesswc4aBTWGvw0cAxIJp+6OB//Wg=
|
||||||
|
|||||||
22
providers/faker/faker.go
Normal file
22
providers/faker/faker.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package faker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"atom/container"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/brianvoe/gofakeit/v6"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
if err := container.Container.Provide(NewFaker); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFaker() *gofakeit.Faker {
|
||||||
|
faker := gofakeit.New(time.Now().UnixNano())
|
||||||
|
gofakeit.SetGlobalFaker(faker)
|
||||||
|
|
||||||
|
return faker
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user