diff options
author | Julien Dessaux | 2024-11-18 00:10:58 +0100 |
---|---|---|
committer | Julien Dessaux | 2024-12-17 23:19:21 +0100 |
commit | f649f7bbbf978db0ad50b0c6d1432264cfec3f43 (patch) | |
tree | 2e68e6cc116b387865ea2ceee67d1df1c5f50a7b /pkg/database/accounts.go | |
parent | chore(tfstated): refactor helpers to their own package (diff) | |
download | tfstated-f649f7bbbf978db0ad50b0c6d1432264cfec3f43.tar.gz tfstated-f649f7bbbf978db0ad50b0c6d1432264cfec3f43.tar.bz2 tfstated-f649f7bbbf978db0ad50b0c6d1432264cfec3f43.zip |
chore(tfstated): implement a transaction wrapper
Diffstat (limited to 'pkg/database/accounts.go')
-rw-r--r-- | pkg/database/accounts.go | 55 |
1 files changed, 22 insertions, 33 deletions
diff --git a/pkg/database/accounts.go b/pkg/database/accounts.go index f506155..e1c7109 100644 --- a/pkg/database/accounts.go +++ b/pkg/database/accounts.go @@ -50,42 +50,31 @@ func (db *DB) LoadAccountByUsername(username string) (*model.Account, error) { } func (db *DB) InitAdminAccount() error { - tx, err := db.Begin() - if err != nil { - return err - } - defer func() { - if err != nil { - _ = tx.Rollback() - } - }() - var hasAdminAccount bool - if err = tx.QueryRowContext(db.ctx, `SELECT EXISTS (SELECT 1 FROM accounts WHERE is_admin);`).Scan(&hasAdminAccount); err != nil { - return fmt.Errorf("failed to select if there is an admin account in the database: %w", err) - } - if hasAdminAccount { - tx.Rollback() - } else { - var password uuid.UUID - if err = password.Generate(uuid.V4); err != nil { - return fmt.Errorf("failed to generate initial admin password: %w", err) + return db.WithTransaction(func(tx *sql.Tx) error { + var hasAdminAccount bool + if err := tx.QueryRowContext(db.ctx, `SELECT EXISTS (SELECT 1 FROM accounts WHERE is_admin);`).Scan(&hasAdminAccount); err != nil { + return fmt.Errorf("failed to select if there is an admin account in the database: %w", err) } - salt := helpers.GenerateSalt() - hash := helpers.HashPassword(password.String(), salt) - if _, err = tx.ExecContext(db.ctx, - `INSERT INTO accounts(username, salt, password_hash, is_admin) + if !hasAdminAccount { + var password uuid.UUID + if err := password.Generate(uuid.V4); err != nil { + return fmt.Errorf("failed to generate initial admin password: %w", err) + } + salt := helpers.GenerateSalt() + hash := helpers.HashPassword(password.String(), salt) + if _, err := tx.ExecContext(db.ctx, + `INSERT INTO accounts(username, salt, password_hash, is_admin) VALUES ("admin", :salt, :hash, TRUE) ON CONFLICT DO UPDATE SET password_hash = :hash WHERE username = "admin";`, - sql.Named("salt", salt), - sql.Named("hash", hash), - ); err != nil { - return fmt.Errorf("failed to set initial admin password: %w", err) - } - err = tx.Commit() - if err == nil { - AdvertiseAdminPassword(password.String()) + sql.Named("salt", salt), + sql.Named("hash", hash), + ); err == nil { + AdvertiseAdminPassword(password.String()) + } else { + return fmt.Errorf("failed to set initial admin password: %w", err) + } } - } - return err + return nil + }) } |