Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
leg100 committed Nov 9, 2024
1 parent 352594f commit 64ca438
Show file tree
Hide file tree
Showing 44 changed files with 896 additions and 601 deletions.
5 changes: 2 additions & 3 deletions internal/configversion/config_uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/leg100/otf/internal/resource"
"github.com/leg100/otf/internal/sql"
"github.com/leg100/otf/internal/sql/sqlc"
)

Expand All @@ -22,7 +21,7 @@ func newConfigUploader(q *sqlc.Queries, id resource.ID) *cvUploader {

func (u *cvUploader) SetErrored(ctx context.Context) error {
// TODO: add status timestamp
_, err := u.q.UpdateConfigurationVersionErroredByID(ctx, sql.ID(u.id))
_, err := u.q.UpdateConfigurationVersionErroredByID(ctx, u.id)
if err != nil {
return err
}
Expand All @@ -32,7 +31,7 @@ func (u *cvUploader) SetErrored(ctx context.Context) error {
func (u *cvUploader) Upload(ctx context.Context, config []byte) (ConfigurationStatus, error) {
// TODO: add status timestamp
_, err := u.q.UpdateConfigurationVersionConfigByID(ctx, sqlc.UpdateConfigurationVersionConfigByIDParams{
ID: sql.ID(u.id),
ID: u.id,
Config: config,
})
if err != nil {
Expand Down
30 changes: 15 additions & 15 deletions internal/configversion/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ type pgdb struct {
func (db *pgdb) CreateConfigurationVersion(ctx context.Context, cv *ConfigurationVersion) error {
return db.Tx(ctx, func(ctx context.Context, q *sqlc.Queries) error {
err := q.InsertConfigurationVersion(ctx, sqlc.InsertConfigurationVersionParams{
ID: sql.ID(cv.ID),
ID: cv.ID,
CreatedAt: sql.Timestamptz(cv.CreatedAt),
AutoQueueRuns: sql.Bool(cv.AutoQueueRuns),
Source: sql.String(string(cv.Source)),
Speculative: sql.Bool(cv.Speculative),
Status: sql.String(string(cv.Status)),
WorkspaceID: sql.ID(cv.WorkspaceID),
WorkspaceID: cv.WorkspaceID,
})
if err != nil {
return err
Expand All @@ -46,7 +46,7 @@ func (db *pgdb) CreateConfigurationVersion(ctx context.Context, cv *Configuratio
Identifier: sql.String(ia.Repo),
IsPullRequest: sql.Bool(ia.IsPullRequest),
OnDefaultBranch: sql.Bool(ia.OnDefaultBranch),
ConfigurationVersionID: sql.ID(cv.ID),
ConfigurationVersionID: cv.ID,
})
if err != nil {
return err
Expand All @@ -64,7 +64,7 @@ func (db *pgdb) CreateConfigurationVersion(ctx context.Context, cv *Configuratio
func (db *pgdb) UploadConfigurationVersion(ctx context.Context, id resource.ID, fn func(*ConfigurationVersion, ConfigUploader) error) error {
return db.Tx(ctx, func(ctx context.Context, q *sqlc.Queries) error {
// select ...for update
result, err := q.FindConfigurationVersionByIDForUpdate(ctx, sql.ID(id))
result, err := q.FindConfigurationVersionByIDForUpdate(ctx, id)
if err != nil {
return err
}
Expand All @@ -80,14 +80,14 @@ func (db *pgdb) UploadConfigurationVersion(ctx context.Context, id resource.ID,
func (db *pgdb) ListConfigurationVersions(ctx context.Context, workspaceID resource.ID, opts ListOptions) (*resource.Page[*ConfigurationVersion], error) {
q := db.Querier(ctx)
rows, err := q.FindConfigurationVersionsByWorkspaceID(ctx, sqlc.FindConfigurationVersionsByWorkspaceIDParams{
WorkspaceID: sql.ID(workspaceID),
WorkspaceID: workspaceID,
Limit: sql.GetLimit(opts.PageOptions),
Offset: sql.GetOffset(opts.PageOptions),
})
if err != nil {
return nil, err
}
count, err := q.CountConfigurationVersionsByWorkspaceID(ctx, sql.ID(workspaceID))
count, err := q.CountConfigurationVersionsByWorkspaceID(ctx, workspaceID)
if err != nil {
return nil, err
}
Expand All @@ -102,13 +102,13 @@ func (db *pgdb) ListConfigurationVersions(ctx context.Context, workspaceID resou
func (db *pgdb) GetConfigurationVersion(ctx context.Context, opts ConfigurationVersionGetOptions) (*ConfigurationVersion, error) {
q := db.Querier(ctx)
if opts.ID != nil {
result, err := q.FindConfigurationVersionByID(ctx, sql.ID(*opts.ID))
result, err := q.FindConfigurationVersionByID(ctx, *opts.ID)
if err != nil {
return nil, sql.Error(err)
}
return pgRow(result).toConfigVersion(), nil
} else if opts.WorkspaceID != nil {
result, err := q.FindConfigurationVersionLatestByWorkspaceID(ctx, sql.ID(*opts.WorkspaceID))
result, err := q.FindConfigurationVersionLatestByWorkspaceID(ctx, *opts.WorkspaceID)
if err != nil {
return nil, sql.Error(err)
}
Expand All @@ -119,15 +119,15 @@ func (db *pgdb) GetConfigurationVersion(ctx context.Context, opts ConfigurationV
}

func (db *pgdb) GetConfig(ctx context.Context, id resource.ID) ([]byte, error) {
cfg, err := db.Querier(ctx).DownloadConfigurationVersion(ctx, sql.ID(id))
cfg, err := db.Querier(ctx).DownloadConfigurationVersion(ctx, id)
if err != nil {
return nil, sql.Error(err)
}
return cfg, nil
}

func (db *pgdb) DeleteConfigurationVersion(ctx context.Context, id resource.ID) error {
_, err := db.Querier(ctx).DeleteConfigurationVersionByID(ctx, sql.ID(id))
_, err := db.Querier(ctx).DeleteConfigurationVersionByID(ctx, id)
if err != nil {
return sql.Error(err)
}
Expand All @@ -140,7 +140,7 @@ func (db *pgdb) insertCVStatusTimestamp(ctx context.Context, cv *ConfigurationVe
return err
}
_, err = db.Querier(ctx).InsertConfigurationVersionStatusTimestamp(ctx, sqlc.InsertConfigurationVersionStatusTimestampParams{
ID: sql.ID(cv.ID),
ID: cv.ID,
Status: sql.String(string(cv.Status)),
Timestamp: sql.Timestamptz(sts),
})
Expand All @@ -149,27 +149,27 @@ func (db *pgdb) insertCVStatusTimestamp(ctx context.Context, cv *ConfigurationVe

// pgRow represents the result of a database query for a configuration version.
type pgRow struct {
ConfigurationVersionID pgtype.Text
ConfigurationVersionID resource.ID
CreatedAt pgtype.Timestamptz
AutoQueueRuns pgtype.Bool
Source pgtype.Text
Speculative pgtype.Bool
Status pgtype.Text
WorkspaceID pgtype.Text
WorkspaceID resource.ID
StatusTimestamps []sqlc.ConfigurationVersionStatusTimestamp
IngressAttributes *sqlc.IngressAttribute
}

func (result pgRow) toConfigVersion() *ConfigurationVersion {
cv := ConfigurationVersion{
ID: resource.ID{Kind: ConfigVersionKind, ID: result.ConfigurationVersionID.String},
ID: result.ConfigurationVersionID,
CreatedAt: result.CreatedAt.Time.UTC(),
AutoQueueRuns: result.AutoQueueRuns.Bool,
Speculative: result.Speculative.Bool,
Source: Source(result.Source.String),
Status: ConfigurationStatus(result.Status.String),
StatusTimestamps: unmarshalStatusTimestampRows(result.StatusTimestamps),
WorkspaceID: resource.ID{Kind: ConfigVersionKind, ID: result.WorkspaceID.String},
WorkspaceID: result.WorkspaceID,
}
if result.IngressAttributes != nil {
cv.IngressAttributes = NewIngressFromRow(result.IngressAttributes)
Expand Down
19 changes: 8 additions & 11 deletions internal/organization/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type (

// row is the row result of a database query for organizations
type row struct {
OrganizationID pgtype.Text
OrganizationID resource.ID
CreatedAt pgtype.Timestamptz
UpdatedAt pgtype.Timestamptz
Name pgtype.Text
Expand All @@ -37,7 +37,7 @@ type row struct {
// organization.
func (r row) toOrganization() *Organization {
org := &Organization{
ID: resource.ParseID(r.OrganizationID.String),
ID: r.OrganizationID,
CreatedAt: r.CreatedAt.Time.UTC(),
UpdatedAt: r.UpdatedAt.Time.UTC(),
Name: r.Name.String,
Expand Down Expand Up @@ -68,7 +68,7 @@ type pgdb struct {

func (db *pgdb) create(ctx context.Context, org *Organization) error {
err := db.Querier(ctx).InsertOrganization(ctx, sqlc.InsertOrganizationParams{
ID: sql.ID(org.ID),
ID: org.ID,
CreatedAt: sql.Timestamptz(org.CreatedAt),
UpdatedAt: sql.Timestamptz(org.UpdatedAt),
Name: sql.String(org.Name),
Expand Down Expand Up @@ -151,7 +151,7 @@ func (db *pgdb) get(ctx context.Context, name string) (*Organization, error) {
}

func (db *pgdb) getByID(ctx context.Context, id resource.ID) (*Organization, error) {
r, err := db.Querier(ctx).FindOrganizationByID(ctx, sql.ID(id))
r, err := db.Querier(ctx).FindOrganizationByID(ctx, id)
if err != nil {
return nil, sql.Error(err)
}
Expand All @@ -172,18 +172,15 @@ func (db *pgdb) delete(ctx context.Context, name string) error {

// tokenRow is the row result of a database query for organization tokens
type tokenRow struct {
OrganizationTokenID pgtype.Text `json:"organization_token_id"`
OrganizationTokenID resource.ID `json:"organization_token_id"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
OrganizationName pgtype.Text `json:"organization_name"`
Expiry pgtype.Timestamptz `json:"expiry"`
}

func (result tokenRow) toToken() *OrganizationToken {
ot := &OrganizationToken{
ID: resource.ID{
Kind: resource.OrganizationTokenKind,
ID: result.OrganizationTokenID.String,
},
ID: result.OrganizationTokenID,
CreatedAt: result.CreatedAt.Time.UTC(),
Organization: result.OrganizationName.String,
}
Expand All @@ -195,7 +192,7 @@ func (result tokenRow) toToken() *OrganizationToken {

func (db *pgdb) upsertOrganizationToken(ctx context.Context, token *OrganizationToken) error {
err := db.Querier(ctx).UpsertOrganizationToken(ctx, sqlc.UpsertOrganizationTokenParams{
OrganizationTokenID: sql.ID(token.ID),
OrganizationTokenID: token.ID,
OrganizationName: sql.String(token.Organization),
CreatedAt: sql.Timestamptz(token.CreatedAt),
Expiry: sql.TimestamptzPtr(token.Expiry),
Expand Down Expand Up @@ -224,7 +221,7 @@ func (db *pgdb) listOrganizationTokens(ctx context.Context, organization string)
}

func (db *pgdb) getOrganizationTokenByID(ctx context.Context, tokenID resource.ID) (*OrganizationToken, error) {
result, err := db.Querier(ctx).FindOrganizationTokensByID(ctx, sql.ID(tokenID))
result, err := db.Querier(ctx).FindOrganizationTokensByID(ctx, tokenID)
if err != nil {
return nil, sql.Error(err)
}
Expand Down
16 changes: 16 additions & 0 deletions internal/resource/id.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resource

import (
"database/sql/driver"
"fmt"
"math/rand"
"regexp"
Expand Down Expand Up @@ -64,6 +65,21 @@ func (id *ID) UnmarshalText(text []byte) error {
return nil
}

func (id *ID) Scan(text any) error {
s, ok := text.(string)
if !ok {
return fmt.Errorf("expected database value to be a string: %#v", text)
}
// string also makes a copy which is necessary in order to retain the data
// after returning
*id = ParseID(s)
return nil
}

func (id ID) Value() (driver.Value, error) {
return id.String(), nil
}

// GetID allows the user of an interface to retrieve the ID.
func (id ID) GetID() ID {
return id
Expand Down
Loading

0 comments on commit 64ca438

Please sign in to comment.