-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update seeder to make idempotent (to rework)
- Loading branch information
Showing
2 changed files
with
28 additions
and
5 deletions.
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
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 |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
|
||
"github.com/mikestefanello/pagoda/config" | ||
"github.com/mikestefanello/pagoda/ent" | ||
"github.com/mikestefanello/pagoda/ent/user" | ||
"github.com/mikestefanello/pagoda/pkg/domain" | ||
"github.com/mikestefanello/pagoda/pkg/repos/emailsmanager" | ||
"github.com/mikestefanello/pagoda/pkg/repos/notifierrepo" | ||
|
@@ -65,6 +66,17 @@ func SeedUsers(cfg *config.Config, client *ent.Client, useS3 bool) error { | |
|
||
// Wrapper function to create a user and ignore if exists | ||
createUser := func(name, email, password string) *ent.User { | ||
// Check if the user already exists | ||
_, err := client.User. | ||
Query(). | ||
Where(user.EmailEQ(email)). | ||
Only(ctx) | ||
|
||
if err == nil { | ||
// User already exists | ||
log.Printf("User with email %s already exists. Skipping.", email) | ||
return nil | ||
} | ||
|
||
user, err := client.User. | ||
Create(). | ||
|
@@ -125,6 +137,13 @@ func SeedUsers(cfg *config.Config, client *ent.Client, useS3 bool) error { | |
// TODO: for prod, use passwords set in env var | ||
// Create users | ||
alice := createUser("Alice Bonjovi", "[email protected]", hashPassword("password")) | ||
if alice == nil { | ||
// createUser returns nil if user already exists | ||
// NOTE: to clean up later, but right now it's the mechanism to make this seeder idempotent (yes, it's ugly, to rework) | ||
|
||
return nil | ||
} | ||
|
||
bob := createUser("Bob Lupin", "[email protected]", hashPassword("password")) | ||
sandrine := createUser("Sandrine Bonnaire", "[email protected]", hashPassword("password")) | ||
luca := createUser("Luca George", "[email protected]", hashPassword("password")) | ||
|