add db.updateAccountTx transaction (#31)

This commit is contained in:
itsscb 2023-09-25 00:28:16 +02:00 committed by GitHub
parent 800132d683
commit 1ad3aa8d4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 112 additions and 7 deletions

View File

@ -130,7 +130,7 @@ func (server *Server) updateAccount(ctx *gin.Context) {
return return
} }
arg := db.UpdateAccountParams{ arg := db.UpdateAccountTxParams{
ID: req.ID, ID: req.ID,
Changer: req.Changer, Changer: req.Changer,
Passwordhash: sql.NullString{ Passwordhash: sql.NullString{
@ -175,7 +175,7 @@ func (server *Server) updateAccount(ctx *gin.Context) {
}, },
} }
account, err := server.store.UpdateAccount(ctx, arg) account, err := server.store.UpdateAccountTx(ctx, arg)
if err != nil { if err != nil {
ctx.JSON(http.StatusInternalServerError, errorResponse(err)) ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return return

View File

@ -279,7 +279,7 @@ func TestGetAccountAPI(t *testing.T) {
} }
} }
func TestUpdateAccountAPI(t *testing.T) { func TestUpdateAccountTxAPI(t *testing.T) {
account := randomAccount() account := randomAccount()
changer := util.RandomName() changer := util.RandomName()
newPassword := util.RandomString(30) newPassword := util.RandomString(30)
@ -302,7 +302,7 @@ func TestUpdateAccountAPI(t *testing.T) {
accountTemp := account accountTemp := account
accountTemp.Passwordhash = newPassword accountTemp.Passwordhash = newPassword
accountTemp.Changer = changer accountTemp.Changer = changer
arg := db.UpdateAccountParams{ arg := db.UpdateAccountTxParams{
ID: account.ID, ID: account.ID,
Passwordhash: sql.NullString{ Passwordhash: sql.NullString{
Valid: true, Valid: true,
@ -312,7 +312,7 @@ func TestUpdateAccountAPI(t *testing.T) {
} }
store.EXPECT(). store.EXPECT().
UpdateAccount(gomock.Any(), gomock.Eq(arg)). UpdateAccountTx(gomock.Any(), gomock.Eq(arg)).
Times(1). Times(1).
Return(accountTemp, nil) Return(accountTemp, nil)
}, },
@ -334,7 +334,7 @@ func TestUpdateAccountAPI(t *testing.T) {
"changer": changer, "changer": changer,
}, },
buildStubs: func(store *mockdb.MockStore) { buildStubs: func(store *mockdb.MockStore) {
arg := db.UpdateAccountParams{ arg := db.UpdateAccountTxParams{
ID: account.ID, ID: account.ID,
Email: sql.NullString{ Email: sql.NullString{
Valid: true, Valid: true,
@ -344,7 +344,7 @@ func TestUpdateAccountAPI(t *testing.T) {
} }
store.EXPECT(). store.EXPECT().
UpdateAccount(gomock.Any(), gomock.Eq(arg)). UpdateAccountTx(gomock.Any(), gomock.Eq(arg)).
Times(1). Times(1).
Return(account, nil) Return(account, nil)
}, },

View File

@ -586,6 +586,21 @@ func (mr *MockStoreMockRecorder) UpdateAccount(arg0, arg1 any) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateAccount", reflect.TypeOf((*MockStore)(nil).UpdateAccount), arg0, arg1) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateAccount", reflect.TypeOf((*MockStore)(nil).UpdateAccount), arg0, arg1)
} }
// UpdateAccountTx mocks base method.
func (m *MockStore) UpdateAccountTx(arg0 context.Context, arg1 db.UpdateAccountTxParams) (db.Account, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "UpdateAccountTx", arg0, arg1)
ret0, _ := ret[0].(db.Account)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// UpdateAccountTx indicates an expected call of UpdateAccountTx.
func (mr *MockStoreMockRecorder) UpdateAccountTx(arg0, arg1 any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateAccountTx", reflect.TypeOf((*MockStore)(nil).UpdateAccountTx), arg0, arg1)
}
// UpdateDocument mocks base method. // UpdateDocument mocks base method.
func (m *MockStore) UpdateDocument(arg0 context.Context, arg1 db.UpdateDocumentParams) (db.Document, error) { func (m *MockStore) UpdateDocument(arg0 context.Context, arg1 db.UpdateDocumentParams) (db.Document, error) {
m.ctrl.T.Helper() m.ctrl.T.Helper()

View File

@ -2,6 +2,11 @@
SELECT * FROM accounts SELECT * FROM accounts
WHERE "ID" = $1 LIMIT 1; WHERE "ID" = $1 LIMIT 1;
-- name: GetAccountForUpdate :one
SELECT * FROM accounts
WHERE "ID" = $1 LIMIT 1
FOR NO KEY UPDATE;
-- name: CreateAccount :one -- name: CreateAccount :one
INSERT INTO accounts ( INSERT INTO accounts (
passwordhash, passwordhash,

View File

@ -127,6 +127,40 @@ func (q *Queries) GetAccount(ctx context.Context, id int64) (Account, error) {
return i, err return i, err
} }
const getAccountForUpdate = `-- name: GetAccountForUpdate :one
SELECT "ID", passwordhash, firstname, lastname, birthday, "privacyAccepted", "privacyAcceptedDate", email, phone, city, zip, street, country, token, "tokenValid", "tokenExpiration", creator, created, changer, changed FROM accounts
WHERE "ID" = $1 LIMIT 1
FOR NO KEY UPDATE
`
func (q *Queries) GetAccountForUpdate(ctx context.Context, id int64) (Account, error) {
row := q.db.QueryRowContext(ctx, getAccountForUpdate, id)
var i Account
err := row.Scan(
&i.ID,
&i.Passwordhash,
&i.Firstname,
&i.Lastname,
&i.Birthday,
&i.PrivacyAccepted,
&i.PrivacyAcceptedDate,
&i.Email,
&i.Phone,
&i.City,
&i.Zip,
&i.Street,
&i.Country,
&i.Token,
&i.TokenValid,
&i.TokenExpiration,
&i.Creator,
&i.Created,
&i.Changer,
&i.Changed,
)
return i, err
}
const listAccounts = `-- name: ListAccounts :many const listAccounts = `-- name: ListAccounts :many
SELECT "ID", passwordhash, firstname, lastname, birthday, "privacyAccepted", "privacyAcceptedDate", email, phone, city, zip, street, country, token, "tokenValid", "tokenExpiration", creator, created, changer, changed FROM accounts SELECT "ID", passwordhash, firstname, lastname, birthday, "privacyAccepted", "privacyAcceptedDate", email, phone, city, zip, street, country, token, "tokenValid", "tokenExpiration", creator, created, changer, changed FROM accounts
ORDER BY lastname, firstname ORDER BY lastname, firstname

View File

@ -20,6 +20,19 @@ type Querier interface {
CreateReturnsLog(ctx context.Context, arg CreateReturnsLogParams) (ReturnsLog, error) CreateReturnsLog(ctx context.Context, arg CreateReturnsLogParams) (ReturnsLog, error)
DeleteAccount(ctx context.Context, id int64) error DeleteAccount(ctx context.Context, id int64) error
DeleteDocument(ctx context.Context, id int64) error DeleteDocument(ctx context.Context, id int64) error
// -- name: UpdateMail :one
// UPDATE mails
// SET
// "from" = COALESCE(sqlc.narg(from), "from"),
// "to" = COALESCE(sqlc.narg(to), "to"),
// cc = COALESCE(sqlc.narg(cc), cc),
// "subject" = COALESCE(sqlc.narg(subject), "subject"),
// body = COALESCE(sqlc.narg(body), body),
// "timestamp" = COALESCE(sqlc.narg(timestamp), "timestamp"),
// changer = $2,
// changed = now()
// WHERE "ID" = $1
// RETURNING *;
DeleteMail(ctx context.Context, id int64) error DeleteMail(ctx context.Context, id int64) error
DeletePayment(ctx context.Context, id int64) error DeletePayment(ctx context.Context, id int64) error
DeletePerson(ctx context.Context, id int64) error DeletePerson(ctx context.Context, id int64) error
@ -27,6 +40,7 @@ type Querier interface {
DeleteReturn(ctx context.Context, id int64) error DeleteReturn(ctx context.Context, id int64) error
DeleteReturnsLog(ctx context.Context, id int64) error DeleteReturnsLog(ctx context.Context, id int64) error
GetAccount(ctx context.Context, id int64) (Account, error) GetAccount(ctx context.Context, id int64) (Account, error)
GetAccountForUpdate(ctx context.Context, id int64) (Account, error)
GetDocument(ctx context.Context, id int64) (Document, error) GetDocument(ctx context.Context, id int64) (Document, error)
GetMail(ctx context.Context, id int64) (Mail, error) GetMail(ctx context.Context, id int64) (Mail, error)
GetPayment(ctx context.Context, id int64) (Payment, error) GetPayment(ctx context.Context, id int64) (Payment, error)

View File

@ -9,6 +9,7 @@ import (
type Store interface { type Store interface {
Querier Querier
CreateAccountTx(ctx context.Context, arg CreateAccountTxParams) (CreateAccountTxResult, error) CreateAccountTx(ctx context.Context, arg CreateAccountTxParams) (CreateAccountTxResult, error)
UpdateAccountTx(ctx context.Context, arg UpdateAccountTxParams) (Account, error)
} }
// Store provides all functions to execute db queries and transactions // Store provides all functions to execute db queries and transactions

View File

@ -0,0 +1,36 @@
package db
import (
"context"
"database/sql"
)
type UpdateAccountTxParams struct {
ID int64 `json:"ID"`
Changer string `json:"changer"`
Passwordhash sql.NullString `json:"passwordhash"`
Firstname sql.NullString `json:"firstname"`
Lastname sql.NullString `json:"lastname"`
Birthday sql.NullTime `json:"birthday"`
Email sql.NullString `json:"email"`
Phone sql.NullString `json:"phone"`
City sql.NullString `json:"city"`
Zip sql.NullString `json:"zip"`
Street sql.NullString `json:"street"`
Country sql.NullString `json:"country"`
}
type UpdateAccountTxResult struct {
Account Account `json:"account"`
}
func (store *SQLStore) UpdateAccountTx(ctx context.Context, arg UpdateAccountTxParams) (Account, error) {
var result UpdateAccountTxResult
err := store.execTx(ctx, func(q *Queries) error {
var err error
result.Account, err = q.UpdateAccount(ctx, UpdateAccountParams(arg))
return err
})
return result.Account, err
}