Skip to content

Commit

Permalink
release v0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
masnann committed Dec 16, 2023
1 parent 764f518 commit 88e4e97
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion module/entities/challenge_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type ChallengeFormModels struct {
ID uint64 `gorm:"column:id;type:BIGINT UNSIGNED;primaryKey" json:"id"`
UserID uint64 `gorm:"column:user_id;type:BIGINT UNSIGNED" json:"user_id"`
ChallengeID uint64 `gorm:"column:challenge_id;type:BIGINT UNSIGNED" json:"challenge_id"`
Username string `gorm:"column:username;type:username" json:"username"`
Username string `gorm:"column:username;type:varchar(255)" json:"username"`
Photo string `gorm:"column:photo;type:varchar(255)" json:"photo"`
Status string `gorm:"column:status;type:varchar(255)" json:"status"`
Exp uint64 `gorm:"column:exp;type:int" json:"exp"`
Expand Down
10 changes: 10 additions & 0 deletions module/entities/seed_models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package entities

type StatusSeederModels struct {
ID uint64 `gorm:"column:id;type:BIGINT UNSIGNED;primaryKey" json:"id"`
IsExecuted bool `gorm:"column:is_executed;default:false" json:"is_executed"`
}

func (StatusSeederModels) TableName() string {
return "status_seeder"
}
1 change: 1 addition & 0 deletions utils/database/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

func Migrate(db *gorm.DB) {
err := db.AutoMigrate(
entities.StatusSeederModels{},
entities.VoucherModels{},
entities.UserModels{},
entities.ArticleBookmarkModels{},
Expand Down
27 changes: 25 additions & 2 deletions utils/database/seeder/seeder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seeder

import (
"github.com/capstone-kelompok-7/backend-disappear/module/entities"
"github.com/capstone-kelompok-7/backend-disappear/utils/database/faker"
"gorm.io/gorm"
)
Expand All @@ -22,9 +23,31 @@ func RegisterSeeders(db *gorm.DB) []Seeder {
}
}

func IsSeederExecuted(db *gorm.DB) bool {
var status entities.StatusSeederModels
result := db.First(&status)
if result.Error != nil {
return false
}
return status.IsExecuted
}

func SetSeederStatus(db *gorm.DB, executed bool) error {
status := entities.StatusSeederModels{IsExecuted: executed}
result := db.Create(&status)
return result.Error
}

func DBSeed(db *gorm.DB) error {
for _, seeder := range RegisterSeeders(db) {
err := db.Debug().Create(seeder.Seeder).Error
if !IsSeederExecuted(db) {
for _, seeder := range RegisterSeeders(db) {
err := db.Debug().Create(seeder.Seeder).Error
if err != nil {
return err
}
}

err := SetSeederStatus(db, true)
if err != nil {
return err
}
Expand Down

0 comments on commit 88e4e97

Please sign in to comment.