chore: remove redundant dbtype package (#8014)

* chore: rename store to dbmock for consistency

* chore: remove redundant dbtype package

This wasn't necessary and forked how we do DB types.
This commit is contained in:
Kyle Carberry
2023-06-13 14:14:55 -05:00
committed by GitHub
parent 4f512fb230
commit 8c4b7c01e2
10 changed files with 48 additions and 62 deletions

View File

@ -48,7 +48,6 @@ import (
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/database/dbauthz"
"github.com/coder/coder/coderd/database/dbmetrics"
"github.com/coder/coder/coderd/database/dbtype"
"github.com/coder/coder/coderd/gitauth"
"github.com/coder/coder/coderd/gitsshkey"
"github.com/coder/coder/coderd/healthcheck"
@ -948,7 +947,7 @@ func (api *API) CreateInMemoryProvisionerDaemon(ctx context.Context, debounce ti
CreatedAt: database.Now(),
Name: name,
Provisioners: []database.ProvisionerType{database.ProvisionerTypeEcho, database.ProvisionerTypeTerraform},
Tags: dbtype.StringMap{
Tags: database.StringMap{
provisionerdserver.TagScope: provisionerdserver.ScopeOrganization,
},
})

View File

@ -18,7 +18,6 @@ import (
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/database/dbauthz"
"github.com/coder/coder/coderd/database/dbtype"
"github.com/coder/coder/coderd/rbac"
"github.com/coder/coder/cryptorand"
)
@ -278,7 +277,7 @@ func ProvisionerJob(t testing.TB, db database.Store, orig database.ProvisionerJo
// Always set some tags to prevent Acquire from grabbing jobs it should not.
if !orig.StartedAt.Time.IsZero() {
if orig.Tags == nil {
orig.Tags = make(dbtype.StringMap)
orig.Tags = make(database.StringMap)
}
// Make sure when we acquire the job, we only get this one.
orig.Tags[id.String()] = "true"

View File

@ -1,30 +0,0 @@
package dbtype
import (
"database/sql/driver"
"encoding/json"
"golang.org/x/xerrors"
)
type StringMap map[string]string
func (m *StringMap) Scan(src interface{}) error {
if src == nil {
return nil
}
switch src := src.(type) {
case []byte:
err := json.Unmarshal(src, m)
if err != nil {
return err
}
default:
return xerrors.Errorf("unsupported Scan, storing driver.Value type %T into type %T", src, m)
}
return nil
}
func (m StringMap) Value() (driver.Value, error) {
return json.Marshal(m)
}

View File

@ -11,7 +11,6 @@ import (
"fmt"
"time"
"github.com/coder/coder/coderd/database/dbtype"
"github.com/google/uuid"
"github.com/lib/pq"
"github.com/tabbed/pqtype"
@ -1478,7 +1477,7 @@ type ProvisionerDaemon struct {
Name string `db:"name" json:"name"`
Provisioners []ProvisionerType `db:"provisioners" json:"provisioners"`
ReplicaID uuid.NullUUID `db:"replica_id" json:"replica_id"`
Tags dbtype.StringMap `db:"tags" json:"tags"`
Tags StringMap `db:"tags" json:"tags"`
}
type ProvisionerJob struct {
@ -1497,7 +1496,7 @@ type ProvisionerJob struct {
Input json.RawMessage `db:"input" json:"input"`
WorkerID uuid.NullUUID `db:"worker_id" json:"worker_id"`
FileID uuid.UUID `db:"file_id" json:"file_id"`
Tags dbtype.StringMap `db:"tags" json:"tags"`
Tags StringMap `db:"tags" json:"tags"`
ErrorCode sql.NullString `db:"error_code" json:"error_code"`
TraceMetadata pqtype.NullRawMessage `db:"trace_metadata" json:"trace_metadata"`
}

View File

@ -10,7 +10,6 @@ import (
"encoding/json"
"time"
"github.com/coder/coder/coderd/database/dbtype"
"github.com/google/uuid"
"github.com/lib/pq"
"github.com/tabbed/pqtype"
@ -1999,7 +1998,7 @@ type InsertProvisionerDaemonParams struct {
CreatedAt time.Time `db:"created_at" json:"created_at"`
Name string `db:"name" json:"name"`
Provisioners []ProvisionerType `db:"provisioners" json:"provisioners"`
Tags dbtype.StringMap `db:"tags" json:"tags"`
Tags StringMap `db:"tags" json:"tags"`
}
func (q *sqlQuerier) InsertProvisionerDaemon(ctx context.Context, arg InsertProvisionerDaemonParams) (ProvisionerDaemon, error) {
@ -2365,7 +2364,7 @@ type InsertProvisionerJobParams struct {
FileID uuid.UUID `db:"file_id" json:"file_id"`
Type ProvisionerJobType `db:"type" json:"type"`
Input json.RawMessage `db:"input" json:"input"`
Tags dbtype.StringMap `db:"tags" json:"tags"`
Tags StringMap `db:"tags" json:"tags"`
TraceMetadata pqtype.NullRawMessage `db:"trace_metadata" json:"trace_metadata"`
}

View File

@ -8,9 +8,11 @@ overrides:
go:
overrides:
- column: "provisioner_daemons.tags"
go_type: "github.com/coder/coder/coderd/database/dbtype.StringMap"
go_type:
type: "StringMap"
- column: "provisioner_jobs.tags"
go_type: "github.com/coder/coder/coderd/database/dbtype.StringMap"
go_type:
type: "StringMap"
- column: "users.rbac_roles"
go_type: "github.com/lib/pq.StringArray"
- column: "templates.user_acl"

View File

@ -1,14 +0,0 @@
package database
import "time"
// Now returns a standardized timezone used for database resources.
func Now() time.Time {
return Time(time.Now().UTC())
}
// Time returns a time compatible with Postgres. Postgres only stores dates with
// microsecond precision.
func Time(t time.Time) time.Time {
return t.Round(time.Microsecond)
}

View File

@ -3,6 +3,7 @@ package database
import (
"database/sql/driver"
"encoding/json"
"time"
"golang.org/x/xerrors"
@ -43,3 +44,36 @@ func (t *TemplateACL) Scan(src interface{}) error {
func (t TemplateACL) Value() (driver.Value, error) {
return json.Marshal(t)
}
type StringMap map[string]string
func (m *StringMap) Scan(src interface{}) error {
if src == nil {
return nil
}
switch src := src.(type) {
case []byte:
err := json.Unmarshal(src, m)
if err != nil {
return err
}
default:
return xerrors.Errorf("unsupported Scan, storing driver.Value type %T into type %T", src, m)
}
return nil
}
func (m StringMap) Value() (driver.Value, error) {
return json.Marshal(m)
}
// Now returns a standardized timezone used for database resources.
func Now() time.Time {
return Time(time.Now().UTC())
}
// Time returns a time compatible with Postgres. Postgres only stores dates with
// microsecond precision.
func Time(t time.Time) time.Time {
return t.Round(time.Microsecond)
}

View File

@ -26,7 +26,6 @@ import (
"github.com/coder/coder/coderd/database/dbauthz"
"github.com/coder/coder/coderd/database/dbgen"
"github.com/coder/coder/coderd/database/dbtestutil"
"github.com/coder/coder/coderd/database/dbtype"
"github.com/coder/coder/coderd/parameter"
"github.com/coder/coder/coderd/rbac"
"github.com/coder/coder/coderd/schedule"
@ -589,7 +588,7 @@ func TestWorkspaceFilterAllStatus(t *testing.T) {
InitiatorID: owner.UserID,
WorkerID: uuid.NullUUID{},
FileID: file.ID,
Tags: dbtype.StringMap{
Tags: database.StringMap{
"custom": "true",
},
})
@ -617,7 +616,7 @@ func TestWorkspaceFilterAllStatus(t *testing.T) {
job.Type = database.ProvisionerJobTypeWorkspaceBuild
job.OrganizationID = owner.OrganizationID
// Need to prevent acquire from getting this job.
job.Tags = dbtype.StringMap{
job.Tags = database.StringMap{
jobID.String(): "true",
}
job = dbgen.ProvisionerJob(t, db, job)

View File

@ -15,7 +15,6 @@ import (
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/database/dbmock"
"github.com/coder/coder/coderd/database/dbtype"
"github.com/coder/coder/coderd/provisionerdserver"
"github.com/coder/coder/coderd/wsbuilder"
"github.com/coder/coder/codersdk"
@ -614,7 +613,7 @@ func withActiveVersion(params []database.TemplateVersionParameter) func(mTx *dbm
StorageMethod: database.ProvisionerStorageMethodFile,
Type: database.ProvisionerJobTypeTemplateVersionImport,
Input: nil,
Tags: dbtype.StringMap{
Tags: database.StringMap{
"version": "active",
provisionerdserver.TagScope: provisionerdserver.ScopeUser,
},
@ -654,7 +653,7 @@ func withInactiveVersion(params []database.TemplateVersionParameter) func(mTx *d
StorageMethod: database.ProvisionerStorageMethodFile,
Type: database.ProvisionerJobTypeTemplateVersionImport,
Input: nil,
Tags: dbtype.StringMap{
Tags: database.StringMap{
"version": "inactive",
provisionerdserver.TagScope: provisionerdserver.ScopeUser,
},