Skip to content

Commit

Permalink
Merge pull request #22 from isd-sgcu/sunioatm/joh-55-database-seeding
Browse files Browse the repository at this point in the history
Sunioatm/joh 55 database seeding
  • Loading branch information
Sunioatm authored Jan 20, 2024
2 parents eabf86d + 573e2fa commit 1160b70
Show file tree
Hide file tree
Showing 10 changed files with 440 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ test:

server:
. ./tools/export-env.sh ; go run ./src/.

seed:
. ./tools/export-env.sh ; go run ./src/. seed
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Johnjud is a pet adoption web application of the [CUVET For Animal Welfare Club]
1. Run `docker-compose up -d`
2. Run `make server` or `go run ./src/.`

### Data Seeding
1. Run `make seed` or `. ./tools/export-env.sh ; go run ./src/. seed`

### Testing
1. Run `make test` or `go test -v -coverpkg ./... -coverprofile coverage.out -covermode count ./...`

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/bxcodec/faker/v3 v3.8.1
github.com/google/uuid v1.5.0
github.com/isd-sgcu/johnjud-go-proto v0.5.2
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.31.0
github.com/spf13/viper v1.18.1
github.com/stretchr/testify v1.8.4
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
Expand Down
12 changes: 11 additions & 1 deletion src/app/repository/pet/pet.repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package pet

import (
"errors"

"github.com/isd-sgcu/johnjud-backend/src/app/model/pet"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -30,5 +32,13 @@ func (r *Repository) Update(id string, result *pet.Pet) error {
}

func (r *Repository) Delete(id string) error {
return r.db.Where("id = ?", id).Delete(&pet.Pet{}).Error
var pet pet.Pet
err := r.db.Where("id = ? AND deleted_at IS NULL", id).First(&pet).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return gorm.ErrRecordNotFound
}
return err
}
return r.db.Delete(&pet).Error
}
12 changes: 12 additions & 0 deletions src/database/seeds/1705074984036-pet.seed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package seed

func (s Seed) PetSeed1705074984036() error {
for _, b := range pets {
err := s.db.Save(&b).Error

if err != nil {
return err
}
}
return nil
}
25 changes: 25 additions & 0 deletions src/database/seeds/1705075734828-user.seed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package seed

import (
"github.com/isd-sgcu/johnjud-backend/src/app/model/user"

"github.com/bxcodec/faker/v3"
)

func (s Seed) UserSeed1705075734828() error {
for i := 0; i < 3; i++ {
usr := user.User{
Email: faker.Email(),
Password: faker.Password(),
Firstname: faker.FirstName(),
Lastname: faker.LastName(),
Role: "admin",
}
err := s.db.Create(&usr).Error

if err != nil {
return err
}
}
return nil
}
266 changes: 266 additions & 0 deletions src/database/seeds/constant.seed.go

Large diffs are not rendered by default.

95 changes: 95 additions & 0 deletions src/database/seeds/seeder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package seed

import (
"log"
"reflect"
"sort"
"strconv"
"strings"

"github.com/pkg/errors"
"gorm.io/gorm"
)

type Seed struct {
db *gorm.DB
}

type Method struct {
Name string // actually name
Timestamp string
}

func seed(s Seed, seedMethodName string) error {
m := reflect.ValueOf(s).MethodByName(seedMethodName)

if !m.IsValid() {
return errors.New("invalid seed")
}

err := m.Call(nil)
if !err[0].IsNil() {
log.Fatalf("Cannot seed %v , got an error %v", seedMethodName, err[0])
}

log.Println("✔️Seed", seedMethodName, "succeed")

return nil
}

func Execute(db *gorm.DB, seedMethodNames ...string) error {
s := Seed{db}

seedType := reflect.TypeOf(s)

var seedMethods []Method
seeds := make(map[string]reflect.Method)

for i := 0; i < seedType.NumMethod(); i++ {
method := seedType.Method(i)

name := strings.Split(method.Name, "Seed")

seedMethod := Method{
Name: name[0],
Timestamp: name[1],
}

seedMethods = append(seedMethods, seedMethod)
seeds[name[0]] = method
}

sort.Slice(seedMethods, func(p, q int) bool {
timestamp1, err := strconv.Atoi(seedMethods[p].Timestamp)
if err != nil {
log.Fatalln(err)
}

timestamp2, err := strconv.Atoi(seedMethods[q].Timestamp)
if err != nil {
log.Fatalln(err)
}

return timestamp1 < timestamp2
})

// Execute all
if len(seedMethodNames) == 0 {
log.Println("Running all seeder...")
for _, seedMethod := range seedMethods {
err := seed(s, seeds[seedMethod.Name].Name)
if err != nil {
return err
}
}
}

// Execute only the given names
for _, item := range seedMethodNames {
err := seed(s, seeds[item].Name)
if err != nil {
return err
}
}
return nil
}
23 changes: 23 additions & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"flag"
"fmt"
"net"
"os"
Expand All @@ -17,6 +18,7 @@ import (
petSrv "github.com/isd-sgcu/johnjud-backend/src/app/service/pet"
"github.com/isd-sgcu/johnjud-backend/src/config"
"github.com/isd-sgcu/johnjud-backend/src/database"
seed "github.com/isd-sgcu/johnjud-backend/src/database/seeds"
likePb "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/like/v1"
petPb "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1"
imagePb "github.com/isd-sgcu/johnjud-go-proto/johnjud/file/image/v1"
Expand All @@ -26,8 +28,27 @@ import (
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
"gorm.io/gorm"
)

func handleArgs(db *gorm.DB) {
flag.Parse()
args := flag.Args()

if len(args) >= 1 {
switch args[0] {
case "seed":
err := seed.Execute(db, args[1:]...)
if err != nil {
log.Fatal().
Str("service", "seeder").
Msg("Not found seed")
}
os.Exit(0)
}
}
}

type operation func(ctx context.Context) error

func gracefulShutdown(ctx context.Context, timeout time.Duration, ops map[string]operation) <-chan struct{} {
Expand Down Expand Up @@ -101,6 +122,8 @@ func main() {
Msg("Failed to init postgres connection")
}

handleArgs(db)

fileConn, err := grpc.Dial(conf.Service.File, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatal().
Expand Down

0 comments on commit 1160b70

Please sign in to comment.