Skip to content

Commit

Permalink
feat: Add parameter and jobs database schema (#81)
Browse files Browse the repository at this point in the history
* feat: Add parameter and jobs database schema

This modifies a prior migration which is typically forbidden,
but because we're pre-production deployment I felt grouping
would be helpful to future contributors.

This adds database functions that are required for the provisioner
daemon and job queue logic.

* Add comment to acquire provisioner job query

* PostgreSQL hates running in parallel
  • Loading branch information
kylecarbs committed Jan 29, 2022
1 parent 599ea2a commit b503c8b
Show file tree
Hide file tree
Showing 11 changed files with 1,174 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ jobs:
run:
DB=true gotestsum --jsonfile="gotests.json" --packages="./..." --
-covermode=atomic -coverprofile="gotests.coverage" -timeout=3m
-count=1 -race -parallel=2
-count=1 -race -parallel=1

- uses: codecov/codecov-action@v2
with:
Expand Down
215 changes: 199 additions & 16 deletions database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ func New() database.Store {
organizationMembers: make([]database.OrganizationMember, 0),
users: make([]database.User, 0),

project: make([]database.Project, 0),
projectHistory: make([]database.ProjectHistory, 0),
projectParameter: make([]database.ProjectParameter, 0),
workspace: make([]database.Workspace, 0),
workspaceResource: make([]database.WorkspaceResource, 0),
workspaceHistory: make([]database.WorkspaceHistory, 0),
workspaceAgent: make([]database.WorkspaceAgent, 0),
parameterValue: make([]database.ParameterValue, 0),
project: make([]database.Project, 0),
projectHistory: make([]database.ProjectHistory, 0),
projectParameter: make([]database.ProjectParameter, 0),
provisionerDaemons: make([]database.ProvisionerDaemon, 0),
provisionerJobs: make([]database.ProvisionerJob, 0),
workspace: make([]database.Workspace, 0),
workspaceResource: make([]database.WorkspaceResource, 0),
workspaceHistory: make([]database.WorkspaceHistory, 0),
workspaceAgent: make([]database.WorkspaceAgent, 0),
}
}

Expand All @@ -37,20 +40,48 @@ type fakeQuerier struct {
users []database.User

// New tables
project []database.Project
projectHistory []database.ProjectHistory
projectParameter []database.ProjectParameter
workspace []database.Workspace
workspaceResource []database.WorkspaceResource
workspaceHistory []database.WorkspaceHistory
workspaceAgent []database.WorkspaceAgent
parameterValue []database.ParameterValue
project []database.Project
projectHistory []database.ProjectHistory
projectParameter []database.ProjectParameter
provisionerDaemons []database.ProvisionerDaemon
provisionerJobs []database.ProvisionerJob
workspace []database.Workspace
workspaceResource []database.WorkspaceResource
workspaceHistory []database.WorkspaceHistory
workspaceAgent []database.WorkspaceAgent
}

// InTx doesn't rollback data properly for in-memory yet.
func (q *fakeQuerier) InTx(fn func(database.Store) error) error {
return fn(q)
}

func (q *fakeQuerier) AcquireProvisionerJob(_ context.Context, arg database.AcquireProvisionerJobParams) (database.ProvisionerJob, error) {
for index, provisionerJob := range q.provisionerJobs {
if provisionerJob.StartedAt.Valid {
continue
}
found := false
for _, provisionerType := range arg.Types {
if provisionerJob.Provisioner != provisionerType {
continue
}
found = true
break
}
if !found {
continue
}
provisionerJob.StartedAt = arg.StartedAt
provisionerJob.UpdatedAt = arg.StartedAt.Time
provisionerJob.WorkerID = arg.WorkerID
q.provisionerJobs[index] = provisionerJob
return provisionerJob, nil
}
return database.ProvisionerJob{}, sql.ErrNoRows
}

func (q *fakeQuerier) GetAPIKeyByID(_ context.Context, id string) (database.APIKey, error) {
for _, apiKey := range q.apiKeys {
if apiKey.ID == id {
Expand Down Expand Up @@ -97,6 +128,15 @@ func (q *fakeQuerier) GetWorkspaceAgentsByResourceIDs(_ context.Context, ids []u
return agents, nil
}

func (q *fakeQuerier) GetWorkspaceByID(_ context.Context, id uuid.UUID) (database.Workspace, error) {
for _, workspace := range q.workspace {
if workspace.ID.String() == id.String() {
return workspace, nil
}
}
return database.Workspace{}, sql.ErrNoRows
}

func (q *fakeQuerier) GetWorkspaceByUserIDAndName(_ context.Context, arg database.GetWorkspaceByUserIDAndNameParams) (database.Workspace, error) {
for _, workspace := range q.workspace {
if workspace.OwnerID != arg.OwnerID {
Expand All @@ -123,6 +163,15 @@ func (q *fakeQuerier) GetWorkspaceResourcesByHistoryID(_ context.Context, worksp
return resources, nil
}

func (q *fakeQuerier) GetWorkspaceHistoryByID(_ context.Context, id uuid.UUID) (database.WorkspaceHistory, error) {
for _, history := range q.workspaceHistory {
if history.ID.String() == id.String() {
return history, nil
}
}
return database.WorkspaceHistory{}, sql.ErrNoRows
}

func (q *fakeQuerier) GetWorkspaceHistoryByWorkspaceIDWithoutAfter(_ context.Context, workspaceID uuid.UUID) (database.WorkspaceHistory, error) {
for _, workspaceHistory := range q.workspaceHistory {
if workspaceHistory.WorkspaceID.String() != workspaceID.String() {
Expand Down Expand Up @@ -179,6 +228,15 @@ func (q *fakeQuerier) GetWorkspacesByUserID(_ context.Context, ownerID string) (
return workspaces, nil
}

func (q *fakeQuerier) GetOrganizationByID(_ context.Context, id string) (database.Organization, error) {
for _, organization := range q.organizations {
if organization.ID == id {
return organization, nil
}
}
return database.Organization{}, sql.ErrNoRows
}

func (q *fakeQuerier) GetOrganizationByName(_ context.Context, name string) (database.Organization, error) {
for _, organization := range q.organizations {
if organization.Name == name {
Expand Down Expand Up @@ -207,6 +265,23 @@ func (q *fakeQuerier) GetOrganizationsByUserID(_ context.Context, userID string)
return organizations, nil
}

func (q *fakeQuerier) GetParameterValuesByScope(_ context.Context, arg database.GetParameterValuesByScopeParams) ([]database.ParameterValue, error) {
parameterValues := make([]database.ParameterValue, 0)
for _, parameterValue := range q.parameterValue {
if parameterValue.Scope != arg.Scope {
continue
}
if parameterValue.ScopeID != arg.ScopeID {
continue
}
parameterValues = append(parameterValues, parameterValue)
}
if len(parameterValues) == 0 {
return nil, sql.ErrNoRows
}
return parameterValues, nil
}

func (q *fakeQuerier) GetProjectByID(_ context.Context, id uuid.UUID) (database.Project, error) {
for _, project := range q.project {
if project.ID.String() == id.String() {
Expand Down Expand Up @@ -253,6 +328,20 @@ func (q *fakeQuerier) GetProjectHistoryByID(_ context.Context, projectHistoryID
return database.ProjectHistory{}, sql.ErrNoRows
}

func (q *fakeQuerier) GetProjectParametersByHistoryID(_ context.Context, projectHistoryID uuid.UUID) ([]database.ProjectParameter, error) {
parameters := make([]database.ProjectParameter, 0)
for _, projectParameter := range q.projectParameter {
if projectParameter.ProjectHistoryID.String() != projectHistoryID.String() {
continue
}
parameters = append(parameters, projectParameter)
}
if len(parameters) == 0 {
return nil, sql.ErrNoRows
}
return parameters, nil
}

func (q *fakeQuerier) GetProjectsByOrganizationIDs(_ context.Context, ids []string) ([]database.Project, error) {
projects := make([]database.Project, 0)
for _, project := range q.project {
Expand Down Expand Up @@ -282,6 +371,26 @@ func (q *fakeQuerier) GetOrganizationMemberByUserID(_ context.Context, arg datab
return database.OrganizationMember{}, sql.ErrNoRows
}

func (q *fakeQuerier) GetProvisionerDaemonByID(_ context.Context, id uuid.UUID) (database.ProvisionerDaemon, error) {
for _, provisionerDaemon := range q.provisionerDaemons {
if provisionerDaemon.ID.String() != id.String() {
continue
}
return provisionerDaemon, nil
}
return database.ProvisionerDaemon{}, sql.ErrNoRows
}

func (q *fakeQuerier) GetProvisionerJobByID(_ context.Context, id uuid.UUID) (database.ProvisionerJob, error) {
for _, provisionerJob := range q.provisionerJobs {
if provisionerJob.ID.String() != id.String() {
continue
}
return provisionerJob, nil
}
return database.ProvisionerJob{}, sql.ErrNoRows
}

func (q *fakeQuerier) InsertAPIKey(_ context.Context, arg database.InsertAPIKeyParams) (database.APIKey, error) {
//nolint:gosimple
key := database.APIKey{
Expand Down Expand Up @@ -329,6 +438,24 @@ func (q *fakeQuerier) InsertOrganizationMember(_ context.Context, arg database.I
return organizationMember, nil
}

func (q *fakeQuerier) InsertParameterValue(_ context.Context, arg database.InsertParameterValueParams) (database.ParameterValue, error) {
//nolint:gosimple
parameterValue := database.ParameterValue{
ID: arg.ID,
Name: arg.Name,
CreatedAt: arg.CreatedAt,
UpdatedAt: arg.UpdatedAt,
Scope: arg.Scope,
ScopeID: arg.ScopeID,
SourceScheme: arg.SourceScheme,
SourceValue: arg.SourceValue,
DestinationScheme: arg.DestinationScheme,
DestinationValue: arg.DestinationValue,
}
q.parameterValue = append(q.parameterValue, parameterValue)
return parameterValue, nil
}

func (q *fakeQuerier) InsertProject(_ context.Context, arg database.InsertProjectParams) (database.Project, error) {
project := database.Project{
ID: arg.ID,
Expand Down Expand Up @@ -367,9 +494,11 @@ func (q *fakeQuerier) InsertProjectParameter(_ context.Context, arg database.Ins
ProjectHistoryID: arg.ProjectHistoryID,
Name: arg.Name,
Description: arg.Description,
DefaultSource: arg.DefaultSource,
DefaultSourceScheme: arg.DefaultSourceScheme,
DefaultSourceValue: arg.DefaultSourceValue,
AllowOverrideSource: arg.AllowOverrideSource,
DefaultDestination: arg.DefaultDestination,
DefaultDestinationScheme: arg.DefaultDestinationScheme,
DefaultDestinationValue: arg.DefaultDestinationValue,
AllowOverrideDestination: arg.AllowOverrideDestination,
DefaultRefresh: arg.DefaultRefresh,
RedisplayValue: arg.RedisplayValue,
Expand All @@ -382,6 +511,32 @@ func (q *fakeQuerier) InsertProjectParameter(_ context.Context, arg database.Ins
return param, nil
}

func (q *fakeQuerier) InsertProvisionerDaemon(_ context.Context, arg database.InsertProvisionerDaemonParams) (database.ProvisionerDaemon, error) {
daemon := database.ProvisionerDaemon{
ID: arg.ID,
CreatedAt: arg.CreatedAt,
Name: arg.Name,
Provisioners: arg.Provisioners,
}
q.provisionerDaemons = append(q.provisionerDaemons, daemon)
return daemon, nil
}

func (q *fakeQuerier) InsertProvisionerJob(_ context.Context, arg database.InsertProvisionerJobParams) (database.ProvisionerJob, error) {
job := database.ProvisionerJob{
ID: arg.ID,
CreatedAt: arg.CreatedAt,
UpdatedAt: arg.UpdatedAt,
InitiatorID: arg.InitiatorID,
Provisioner: arg.Provisioner,
ProjectID: arg.ProjectID,
Type: arg.Type,
Input: arg.Input,
}
q.provisionerJobs = append(q.provisionerJobs, job)
return job, nil
}

func (q *fakeQuerier) InsertUser(_ context.Context, arg database.InsertUserParams) (database.User, error) {
user := database.User{
ID: arg.ID,
Expand Down Expand Up @@ -470,6 +625,34 @@ func (q *fakeQuerier) UpdateAPIKeyByID(_ context.Context, arg database.UpdateAPI
return sql.ErrNoRows
}

func (q *fakeQuerier) UpdateProvisionerDaemonByID(_ context.Context, arg database.UpdateProvisionerDaemonByIDParams) error {
for index, daemon := range q.provisionerDaemons {
if arg.ID.String() != daemon.ID.String() {
continue
}
daemon.UpdatedAt = arg.UpdatedAt
daemon.Provisioners = arg.Provisioners
q.provisionerDaemons[index] = daemon
return nil
}
return sql.ErrNoRows
}

func (q *fakeQuerier) UpdateProvisionerJobByID(_ context.Context, arg database.UpdateProvisionerJobByIDParams) error {
for index, job := range q.provisionerJobs {
if arg.ID.String() != job.ID.String() {
continue
}
job.CompletedAt = arg.CompletedAt
job.CancelledAt = arg.CancelledAt
job.UpdatedAt = arg.UpdatedAt
job.Error = arg.Error
q.provisionerJobs[index] = job
return nil
}
return sql.ErrNoRows
}

func (q *fakeQuerier) UpdateWorkspaceHistoryByID(_ context.Context, arg database.UpdateWorkspaceHistoryByIDParams) error {
for index, workspaceHistory := range q.workspaceHistory {
if workspaceHistory.ID.String() != arg.ID.String() {
Expand Down

0 comments on commit b503c8b

Please sign in to comment.