Skip to content

Commit

Permalink
fix: Database model definitions
Browse files Browse the repository at this point in the history
Signed-off-by: jay-dee7 <[email protected]>
  • Loading branch information
jay-dee7 committed Dec 30, 2023
1 parent 8efb411 commit a88203f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 56 deletions.
4 changes: 2 additions & 2 deletions store/v1/emails/emails_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func (es *emailStore) DeleteVerifyEmail(ctx context.Context, userID uuid.UUID) e
}

func (es *emailStore) GetVerifyEmail(ctx context.Context, userID uuid.UUID) (uuid.UUID, error) {
var email types.Email
var email *types.Email

if err := es.db.NewSelect().Model(&email).Where("user_id = ?", userID).Scan(ctx); err != nil {
if err := es.db.NewSelect().Model(email).Where("user_id = ?", userID).Scan(ctx); err != nil {
return uuid.UUID{}, v2.WrapDatabaseError(err, v2.DatabaseOperationRead)
}

Expand Down
20 changes: 10 additions & 10 deletions store/v1/permissions/permissions_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ func (p *permissionStore) GetUserPermissionsForOrg(
orgID uuid.UUID,
userID uuid.UUID,
) (*types.Permissions, error) {
var perm types.Permissions
perm := &types.Permissions{}

err := p.
db.
NewSelect().
Model(&perm).
Model(perm).
Where("user_id = ?", userID.String()).
Where("organization_id = ?", orgID).
Scan(ctx)
Expand All @@ -79,7 +79,7 @@ func (p *permissionStore) GetUserPermissionsForOrg(
return nil, v1.WrapDatabaseError(err, v1.DatabaseOperationRead)
}

return &perm, nil
return perm, nil
}

func (p *permissionStore) UpdatePermissions(ctx context.Context, perm *types.Permissions) error {
Expand Down Expand Up @@ -137,11 +137,11 @@ func (p *permissionStore) validateAddOrgMembersInput(input *types.AddUsersToOrgR
}

func (p *permissionStore) RemoveUserFromOrg(ctx context.Context, orgID, userID uuid.UUID) error {
var perm types.Permissions
perm := &types.Permissions{}
_, err := p.
db.
NewDelete().
Model(&perm).
Model(perm).
Where("organization_id = ?", orgID.String()).
Where("user_id = ?", userID.String()).
Exec(ctx)
Expand All @@ -157,18 +157,18 @@ func (p *permissionStore) GetUserPermissionsForNamespace(
ns string,
userID uuid.UUID,
) *types.Permissions {
perms := types.Permissions{}
perms := &types.Permissions{}

nsParts := strings.Split(ns, "/")
if len(nsParts) != 2 {
return &perms
return perms
}
orgName := nsParts[0]

q := p.
db.
NewSelect().
Model(&perms).
Model(perms).
Relation("Organization", func(sq *bun.SelectQuery) *bun.SelectQuery {
return sq.
Where(`"organization"."username" = ?`, orgName).
Expand All @@ -181,9 +181,9 @@ func (p *permissionStore) GetUserPermissionsForNamespace(
})

if err := q.Scan(ctx); err != nil {
return &perms
return perms
}

return &perms
return perms

}
52 changes: 26 additions & 26 deletions store/v1/registry/registry_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ func (s *registryStore) GetRepositoryByName(
Str("name", name).
Str("user_id", userId.String())

var repository types.ContainerImageRepository
repository := &types.ContainerImageRepository{}
err := s.
db.
NewSelect().
Model(&repository).
Model(repository).
WhereGroup(" AND ", func(sq *bun.SelectQuery) *bun.SelectQuery {
return sq.Where("name = ?", name)
}).
Expand All @@ -132,7 +132,7 @@ func (s *registryStore) GetRepositoryByName(
}

logEvent.Bool("success", true).Send()
return &repository, nil
return repository, nil
}

func (s *registryStore) DeleteLayerByDigestWithTxn(ctx context.Context, txn *bun.Tx, digest string) error {
Expand Down Expand Up @@ -207,7 +207,7 @@ func (s *registryStore) GetCatalog(
pageSize int,
offset int,
) ([]string, error) {
var catalog []types.ContainerImageRepository
var catalog []*types.ContainerImageRepository

repositoryName := strings.Split(namespace, "/")[1]
err := s.
Expand Down Expand Up @@ -444,27 +444,27 @@ func (s *registryStore) GetImageTags(ctx context.Context, namespace string) ([]s
// GetLayer implements registry.RegistryStore.
func (s *registryStore) GetLayer(ctx context.Context, digest string) (*types.ContainerImageLayer, error) {
logEvent := s.logger.Debug().Str("method", "GetLayer").Str("digest", digest)
var layer types.ContainerImageLayer
if err := s.db.NewSelect().Model(&layer).Where("digest = ?", digest).Scan(ctx); err != nil {
layer := &types.ContainerImageLayer{}
if err := s.db.NewSelect().Model(layer).Where("digest = ?", digest).Scan(ctx); err != nil {
logEvent.Err(err).Send()
return nil, v1.WrapDatabaseError(err, v1.DatabaseOperationRead)
}

logEvent.Bool("success", true).Send()
return &layer, nil
return layer, nil
}

// GetManifest implements registry.RegistryStore.
func (s *registryStore) GetManifest(ctx context.Context, id string) (*types.ImageManifest, error) {
logEvent := s.logger.Debug().Str("method", "GetManifest").Str("id", id)
var manifest types.ImageManifest
if err := s.db.NewSelect().Model(&manifest).Where("id = ?", id).Scan(ctx); err != nil {
manifest := &types.ImageManifest{}
if err := s.db.NewSelect().Model(manifest).Where("id = ?", id).Scan(ctx); err != nil {
logEvent.Err(err).Send()
return nil, v1.WrapDatabaseError(err, v1.DatabaseOperationRead)
}

logEvent.Bool("success", true).Send()
return &manifest, nil
return manifest, nil
}

// GetManifestByReference implements registry.RegistryStore.
Expand All @@ -479,11 +479,11 @@ func (s *registryStore) GetManifestByReference(
nsParts := strings.Split(namespace, "/")
username, repoName := nsParts[0], nsParts[1]

var manifest types.ImageManifest
manifest := &types.ImageManifest{}
q := s.
db.
NewSelect().
Model(&manifest).
Model(manifest).
Relation("Repository", func(sq *bun.SelectQuery) *bun.SelectQuery {
return sq.Column("name")
}).
Expand All @@ -507,7 +507,7 @@ func (s *registryStore) GetManifestByReference(
}

logEvent.Bool("success", true).Send()
return &manifest, nil
return manifest, nil
}

// GetRepoDetail implements registry.RegistryStore.
Expand All @@ -518,13 +518,13 @@ func (s *registryStore) GetRepoDetail(
offset int,
) (*types.ContainerImageRepository, error) {
logEvent := s.logger.Debug().Str("method", "GetRepoDetail")
var repoDetail types.ContainerImageRepository
repoDetail := &types.ContainerImageRepository{}

repositoryName := strings.Split(namespace, "/")[1]
err := s.
db.
NewSelect().
Model(&repoDetail).
Model(repoDetail).
Relation("ImageManifests").
Where("name = ?", repositoryName).
Limit(pageSize).
Expand All @@ -537,7 +537,7 @@ func (s *registryStore) GetRepoDetail(
}

logEvent.Bool("success", true).Send()
return &repoDetail, nil
return repoDetail, nil
}

// SetContainerImageVisibility implements registry.RegistryStore.
Expand Down Expand Up @@ -740,11 +740,11 @@ func (s *registryStore) GetImageSizeByLayerIds(ctx context.Context, layerIDs []s
}

func (s *registryStore) IncrementRepositoryPullCounter(ctx context.Context, repoID uuid.UUID) error {
repo := types.ContainerImageRepository{
repo := &types.ContainerImageRepository{
ID: repoID,
}

_, err := s.db.NewUpdate().Model(&repo).WherePK().Set("pull_count = pull_count + 1").Exec(ctx)
_, err := s.db.NewUpdate().Model(repo).WherePK().Set("pull_count = pull_count + 1").Exec(ctx)
if err != nil {
return v1.WrapDatabaseError(err, v1.DatabaseOperationUpdate)
}
Expand All @@ -753,12 +753,12 @@ func (s *registryStore) IncrementRepositoryPullCounter(ctx context.Context, repo
}

func (s *registryStore) AddRepositoryToFavorites(ctx context.Context, repoID uuid.UUID, userID uuid.UUID) error {
user := types.User{}
user := &types.User{}

q := s.
db.
NewUpdate().
Model(&user).
Model(user).
Set("favorite_repositories = array_append(favorite_repositories, ?)", repoID).
Where("id = ?", userID).
Where("NOT (? = ANY(favorite_repositories))", repoID)
Expand All @@ -774,11 +774,11 @@ func (s *registryStore) AddRepositoryToFavorites(ctx context.Context, repoID uui
}

if rowsAffected == 1 {
repo := types.ContainerImageRepository{
repo := &types.ContainerImageRepository{
ID: repoID,
}

_, err = s.db.NewUpdate().Model(&repo).WherePK().Set("favorite_count = favorite_count + 1").Exec(ctx)
_, err = s.db.NewUpdate().Model(repo).WherePK().Set("favorite_count = favorite_count + 1").Exec(ctx)
if err != nil {
return v1.WrapDatabaseError(err, v1.DatabaseOperationUpdate)
}
Expand All @@ -790,11 +790,11 @@ func (s *registryStore) AddRepositoryToFavorites(ctx context.Context, repoID uui
}

func (s *registryStore) RemoveRepositoryFromFavorites(ctx context.Context, repoID uuid.UUID, userID uuid.UUID) error {
user := types.User{}
user := &types.User{}
q := s.
db.
NewUpdate().
Model(&user).
Model(user).
Set("favorite_repositories = array_remove(favorite_repositories, ?)", repoID).
Where("id = ?", userID).
Where("? = ANY(favorite_repositories)", repoID)
Expand All @@ -806,11 +806,11 @@ func (s *registryStore) RemoveRepositoryFromFavorites(ctx context.Context, repoI

rowsAffected, err := result.RowsAffected()
if err == nil && rowsAffected == 1 {
repo := types.ContainerImageRepository{
repo := &types.ContainerImageRepository{
ID: repoID,
}

_, err = s.db.NewUpdate().Model(&repo).WherePK().Set("favorite_count = favorite_count - 1").Exec(ctx)
_, err = s.db.NewUpdate().Model(repo).WherePK().Set("favorite_count = favorite_count - 1").Exec(ctx)
if err != nil {
return v1.WrapDatabaseError(err, v1.DatabaseOperationUpdate)
}
Expand Down
11 changes: 0 additions & 11 deletions store/v1/sessions/sessions_impl.og

This file was deleted.

12 changes: 6 additions & 6 deletions store/v1/users/users_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ func (us *userStore) GetUserByUsernameWithTxn(ctx context.Context, username stri
}

func (us *userStore) GetIPFSUser(ctx context.Context) (*types.User, error) {
var user types.User
if err := us.db.NewSelect().Model(&user).Where("username = ?", types.SystemUsernameIPFS).Scan(ctx); err != nil {
user := &types.User{}
if err := us.db.NewSelect().Model(user).Where("username = ?", types.SystemUsernameIPFS).Scan(ctx); err != nil {
return nil, v1.WrapDatabaseError(err, v1.DatabaseOperationRead)
}

return &user, nil
return user, nil
}

// GetUserWithSession implements UserStore.
Expand Down Expand Up @@ -225,12 +225,12 @@ func (us *userStore) webAuthnUserExists(ctx context.Context, username, email str
}

func (us *userStore) ConvertUserToOrg(ctx context.Context, userID uuid.UUID) error {
user := types.User{ID: userID}
user := &types.User{ID: userID}

_, err := us.
db.
NewUpdate().
Model(&user).
Model(user).
WherePK().
Set("user_type = ?", types.UserTypeOrganization.String()).
Set("is_org_owner = ?", true).
Expand Down Expand Up @@ -370,7 +370,7 @@ func (us *userStore) GetAuthToken(
}

if token.ExpiresAt.Unix() < time.Now().Unix() {
return nil, fmt.Errorf("Token has expired, please generate a new one")
return nil, fmt.Errorf("token has expired, please generate a new one")
}

return &token, nil
Expand Down
2 changes: 1 addition & 1 deletion store/v1/webauthn/webauthn_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (ws *webauthnStore) GetWebAuthnSessionData(
userId uuid.UUID,
sessionType string,
) (*webauthn.SessionData, error) {
var session types.WebauthnSession
session := &types.WebauthnSession{}

err := ws.
db.
Expand Down

0 comments on commit a88203f

Please sign in to comment.