feat: use database transactions to ensure atomicity
This commit is contained in:
@@ -57,10 +57,15 @@ func HandleSeedCommand(cfg *config.Config, name string, args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return withDatabase(cfg, func(db *gorm.DB) error {
|
return withDatabase(cfg, func(db *gorm.DB) error {
|
||||||
userRepo := repositories.NewUserRepository(db)
|
return db.Transaction(func(tx *gorm.DB) error {
|
||||||
postRepo := repositories.NewPostRepository(db)
|
userRepo := repositories.NewUserRepository(db).WithTx(tx)
|
||||||
voteRepo := repositories.NewVoteRepository(db)
|
postRepo := repositories.NewPostRepository(db).WithTx(tx)
|
||||||
return runSeedCommand(userRepo, postRepo, voteRepo, fs.Args())
|
voteRepo := repositories.NewVoteRepository(db).WithTx(tx)
|
||||||
|
if err := runSeedCommand(userRepo, postRepo, voteRepo, fs.Args()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user