-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from isd-sgcu/sunioatm/joh-55-database-seeding
Sunioatm/joh 55 database seeding
- Loading branch information
Showing
10 changed files
with
440 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,6 @@ test: | |
|
||
server: | ||
. ./tools/export-env.sh ; go run ./src/. | ||
|
||
seed: | ||
. ./tools/export-env.sh ; go run ./src/. seed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters