Skip to content

Commit

Permalink
NEOS-1446: update personal new accounts to have a default record limit (
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzelei authored Sep 13, 2024
1 parent 3401c8d commit 309afb7
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 35 deletions.
30 changes: 15 additions & 15 deletions backend/gen/go/db/mock_Querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/gen/go/db/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions backend/gen/go/db/users.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions backend/internal/cmds/mgmt/serve/connect/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,9 @@ func serve(ctx context.Context) error {
return err
}
useraccountService := v1alpha1_useraccountservice.New(&v1alpha1_useraccountservice.Config{
IsAuthEnabled: isAuthEnabled,
IsNeosyncCloud: getIsNeosyncCloud(),
IsAuthEnabled: isAuthEnabled,
IsNeosyncCloud: getIsNeosyncCloud(),
DefaultMaxAllowedRecords: getDefaultMaxAllowedRecords(),
}, db, tfwfmgr, authclient, authadminclient, promv1.NewAPI(promclient))
api.Handle(
mgmtv1alpha1connect.NewUserAccountServiceHandler(
Expand Down Expand Up @@ -904,3 +905,11 @@ func getKubernetesNamespace() string {
func getKubernetesWorkerAppName() string {
return viper.GetString("KUBERNETES_WORKER_APP_NAME")
}

func getDefaultMaxAllowedRecords() *int64 {
val := viper.GetInt64("MAX_ALLOWED_RECORDS")
if val <= 0 {
return nil
}
return &val
}
10 changes: 9 additions & 1 deletion backend/internal/nucleusdb/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,22 @@ func (d *NucleusDb) SetUserByAuthSub(
func (d *NucleusDb) SetPersonalAccount(
ctx context.Context,
userId pgtype.UUID,
maxAllowedRecords *int64, // only used when personal account is created
) (*db_queries.NeosyncApiAccount, error) {
var personalAccount *db_queries.NeosyncApiAccount
if err := d.WithTx(ctx, nil, func(dbtx BaseDBTX) error {
account, err := d.Q.GetPersonalAccountByUserId(ctx, dbtx, userId)
if err != nil && !IsNoRows(err) {
return err
} else if err != nil && IsNoRows(err) {
account, err = d.Q.CreatePersonalAccount(ctx, dbtx, "personal")
pgMaxAllowedRecords := pgtype.Int8{}
if maxAllowedRecords != nil && *maxAllowedRecords > 0 {
err := pgMaxAllowedRecords.Scan(*maxAllowedRecords)
if err != nil {
return fmt.Errorf("maxAllowedRecords was not scannable to pgtype.Int8: %w", err)
}
}
account, err = d.Q.CreatePersonalAccount(ctx, dbtx, db_queries.CreatePersonalAccountParams{AccountSlug: "personal", MaxAllowedRecords: pgMaxAllowedRecords})
if err != nil {
return err
}
Expand Down
16 changes: 10 additions & 6 deletions backend/internal/nucleusdb/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func Test_SetPersonalAccount(t *testing.T) {
mockTx.On("Commit", ctx).Return(nil)
mockTx.On("Rollback", ctx).Return(nil)

resp, err := service.SetPersonalAccount(ctx, userUuid)
resp, err := service.SetPersonalAccount(ctx, userUuid, ptr(int64(123)))

assert.NoError(t, err)
assert.NotNil(t, resp)
Expand Down Expand Up @@ -214,13 +214,17 @@ func Test_SetPersonalAccount_CreateUserAssociation(t *testing.T) {
mockTx.On("Commit", ctx).Return(nil)
mockTx.On("Rollback", ctx).Return(nil)

resp, err := service.SetPersonalAccount(ctx, userUuid)
resp, err := service.SetPersonalAccount(ctx, userUuid, ptr(int64(123)))

assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Equal(t, accountUuid, resp.ID)
}

func ptr[T any](val T) *T {
return &val
}

func Test_SetPersonalAccount_CreateAccount(t *testing.T) {
dbtxMock := NewMockDBTX(t)
querierMock := db_queries.NewMockQuerier(t)
Expand All @@ -235,15 +239,15 @@ func Test_SetPersonalAccount_CreateAccount(t *testing.T) {

dbtxMock.On("Begin", ctx).Return(mockTx, nil)
querierMock.On("GetPersonalAccountByUserId", ctx, mockTx, userUuid).Return(nilAccount, sql.ErrNoRows)
querierMock.On("CreatePersonalAccount", ctx, mockTx, "personal").Return(db_queries.NeosyncApiAccount{ID: accountUuid}, nil)
querierMock.On("CreatePersonalAccount", ctx, mockTx, db_queries.CreatePersonalAccountParams{AccountSlug: "personal", MaxAllowedRecords: pgtype.Int8{Int64: 123, Valid: true}}).Return(db_queries.NeosyncApiAccount{ID: accountUuid}, nil)
querierMock.On("CreateAccountUserAssociation", ctx, mockTx, db_queries.CreateAccountUserAssociationParams{
AccountID: accountUuid,
UserID: userUuid,
}).Return(db_queries.NeosyncApiAccountUserAssociation{AccountID: accountUuid, UserID: userUuid}, nil)
mockTx.On("Commit", ctx).Return(nil)
mockTx.On("Rollback", ctx).Return(nil)

resp, err := service.SetPersonalAccount(ctx, userUuid)
resp, err := service.SetPersonalAccount(ctx, userUuid, ptr(int64(123)))

assert.NoError(t, err)
assert.NotNil(t, resp)
Expand All @@ -264,14 +268,14 @@ func Test_SetPersonalAccount_Rollback(t *testing.T) {

dbtxMock.On("Begin", ctx).Return(mockTx, nil)
querierMock.On("GetPersonalAccountByUserId", ctx, mockTx, userUuid).Return(nilAccount, sql.ErrNoRows)
querierMock.On("CreatePersonalAccount", ctx, mockTx, "personal").Return(db_queries.NeosyncApiAccount{ID: accountUuid}, nil)
querierMock.On("CreatePersonalAccount", ctx, mockTx, db_queries.CreatePersonalAccountParams{AccountSlug: "personal", MaxAllowedRecords: pgtype.Int8{Int64: 123, Valid: true}}).Return(db_queries.NeosyncApiAccount{ID: accountUuid}, nil)
querierMock.On("CreateAccountUserAssociation", ctx, mockTx, db_queries.CreateAccountUserAssociationParams{
AccountID: accountUuid,
UserID: userUuid,
}).Return(db_queries.NeosyncApiAccountUserAssociation{AccountID: accountUuid, UserID: userUuid}, errors.New("boo"))
mockTx.On("Rollback", ctx).Return(nil)

resp, err := service.SetPersonalAccount(ctx, userUuid)
resp, err := service.SetPersonalAccount(ctx, userUuid, ptr(int64(123)))

mockTx.AssertNotCalled(t, "Commit", ctx)
assert.Error(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ func (s *IntegrationTestSuite) SetupSuite() {
prometheusclient: mockPromV1.NewMockAPI(s.T()),
}

maxAllowed := int64(10000)
unauthdUserService := v1alpha1_useraccountservice.New(
&v1alpha1_useraccountservice.Config{IsAuthEnabled: false, IsNeosyncCloud: false},
&v1alpha1_useraccountservice.Config{IsAuthEnabled: false, IsNeosyncCloud: false, DefaultMaxAllowedRecords: &maxAllowed},
nucleusdb.New(pool, db_queries.New()),
s.mocks.temporalClientManager,
s.mocks.authclient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ type Service struct {
}

type Config struct {
IsAuthEnabled bool
IsNeosyncCloud bool
IsAuthEnabled bool
IsNeosyncCloud bool
DefaultMaxAllowedRecords *int64
}

func New(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (s *Service) SetPersonalAccount(
return nil, err
}

account, err := s.db.SetPersonalAccount(ctx, userId)
account, err := s.db.SetPersonalAccount(ctx, userId, s.cfg.DefaultMaxAllowedRecords)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions backend/sql/postgresql/queries/users.sql
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ WHERE u.id = sqlc.arg('userId') AND a.account_type = 1;

-- name: CreatePersonalAccount :one
INSERT INTO neosync_api.accounts (
account_type, account_slug
account_type, account_slug, max_allowed_records
) VALUES (
0, $1
0, $1, $2
)
RETURNING *;

Expand Down
1 change: 1 addition & 0 deletions docs/docs/deploy/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ These environment variables are loaded when running the `mgmt serve connect` com
| OTEL_SERVICE_VERSION | The version of the service that will be used with for reporting | false | |
| OTEL_TRACES_EXPORTER | The exporter that will be used. Allowed: otlp, console, none. If otlp, uses grpc. | false | otlp |
| OTEL_METRICS_EXPORTER | The exporter that will be used. Allowed: otlp, console, none. If otlp, uses grpc | false | otlp |
| MAX_ALLOWED_RECORDS | The max allowed records that are allowed for an account to ingest (currently only applies to Personal accounts in Neosync Cloud) | false | NULL |

## Backend API Database Migrations

Expand Down

0 comments on commit 309afb7

Please sign in to comment.