drone/internal/store/database/principal_info.go
Johannes Batzill 335998505e [MISC] Introduce PathStore and Move PathLogic to Controller Layer (#226)
This change is a follow up on the effort to remove transactions and sync stores from the database layer, and move them in the application layer. It is addressing the repo + space store.
The following changes are included:
- Introduce PathStore storing resource paths (repo + space)
- Add foreign key from path.repo_id/space_id to repo.id/space.id
- Add foreign key from repo/space.parent_id to space.id
- Remove repo_sync and space_sync
- Move path creation logic to repo/space controller (for both creating primary paths for new repos/spaces, as well as creating alias spaces)
- Reimplement repo/space moving on controller layer
2023-01-18 11:34:12 -08:00

93 lines
2.5 KiB
Go

// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package database
import (
"context"
"github.com/harness/gitness/internal/store"
"github.com/harness/gitness/internal/store/database/dbtx"
"github.com/harness/gitness/types"
"github.com/Masterminds/squirrel"
"github.com/jmoiron/sqlx"
)
var _ store.PrincipalInfoView = (*PrincipalInfoView)(nil)
// NewPrincipalInfoView returns a new PrincipalInfoView.
// It's used by the principal info cache.
func NewPrincipalInfoView(db *sqlx.DB) *PrincipalInfoView {
return &PrincipalInfoView{
db: db,
}
}
type PrincipalInfoView struct {
db *sqlx.DB
}
// Find returns a single principal info object by id from the `principals` database table.
func (s *PrincipalInfoView) Find(ctx context.Context, id int64) (*types.PrincipalInfo, error) {
const sqlQuery = `
SELECT principal_uid, principal_email, principal_display_name
FROM principals
WHERE principal_id = $1`
db := dbtx.GetAccessor(ctx, s.db)
v := db.QueryRowContext(ctx, sqlQuery, id)
if err := v.Err(); err != nil {
return nil, processSQLErrorf(err, "failed to find principal info")
}
info := &types.PrincipalInfo{
ID: id,
}
if err := v.Scan(&info.UID, &info.Email, &info.DisplayName); err != nil {
return nil, processSQLErrorf(err, "failed to scan principal info")
}
return info, nil
}
// FindMany returns a several principal info objects by id from the `principals` database table.
func (s *PrincipalInfoView) FindMany(ctx context.Context, ids []int64) ([]*types.PrincipalInfo, error) {
db := dbtx.GetAccessor(ctx, s.db)
stmt := builder.
Select("principal_id, principal_uid, principal_email, principal_display_name").
From("principals").
Where(squirrel.Eq{"principal_id": ids})
sqlQuery, params, err := stmt.ToSql()
if err != nil {
return nil, processSQLErrorf(err, "failed to generate find many principal info SQL query")
}
rows, err := db.QueryContext(ctx, sqlQuery, params...)
if err != nil {
return nil, processSQLErrorf(err, "failed to query find many principal info")
}
defer func() {
_ = rows.Close()
}()
result := make([]*types.PrincipalInfo, 0, len(ids))
for rows.Next() {
info := &types.PrincipalInfo{}
err = rows.Scan(&info.ID, &info.UID, &info.Email, &info.DisplayName)
if err != nil {
return nil, processSQLErrorf(err, "failed to scan principal info")
}
result = append(result, info)
}
return result, nil
}