Skip to content

Commit

Permalink
refactor: use gorm v2 for postgres storage (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
kushsharma authored Oct 21, 2021
1 parent 1ac1718 commit f98a15a
Show file tree
Hide file tree
Showing 61 changed files with 1,410 additions and 1,329 deletions.
154 changes: 77 additions & 77 deletions api/handler/v1/runtime.go

Large diffs are not rendered by default.

249 changes: 124 additions & 125 deletions api/handler/v1/runtime_test.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ func createResourceSubCommand(l log.Logger, datastoreSpecFs map[string]afero.Fs,
func IsResourceNameUnique(repository store.ResourceSpecRepository) survey.Validator {
return func(val interface{}) error {
if str, ok := val.(string); ok {
if _, err := repository.GetByName(str); err == nil {
if _, err := repository.GetByName(context.Background(), str); err == nil {
return fmt.Errorf("resource with the provided name already exists")
} else if err != models.ErrNoSuchSpec && err != models.ErrNoResources {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func postDeploymentRequest(l log.Logger, projectName string, namespace string, j
return fmt.Errorf("unsupported datastore: %s\n", storeName)
}
resourceSpecRepo := local.NewResourceSpecRepository(repoFS, ds)
resourceSpecs, err := resourceSpecRepo.GetAll()
resourceSpecs, err := resourceSpecRepo.GetAll(context.Background())
if err == models.ErrNoResources {
l.Info(coloredNotice("no resource specifications found"))
continue
Expand Down
12 changes: 10 additions & 2 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
grpctags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/hashicorp/go-multierror"
"github.com/jinzhu/gorm"
v1 "github.com/odpf/optimus/api/handler/v1"
v1handler "github.com/odpf/optimus/api/handler/v1"
pb "github.com/odpf/optimus/api/proto/odpf/optimus"
Expand Down Expand Up @@ -54,6 +53,7 @@ import (
"golang.org/x/oauth2/google"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"gorm.io/gorm"
)

var (
Expand Down Expand Up @@ -338,7 +338,7 @@ func Initialize(l log.Logger, conf config.Provider) error {
hash: appHash,
}
if !conf.GetScheduler().SkipInit {
registeredProjects, err := projectRepoFac.New().GetAll()
registeredProjects, err := projectRepoFac.New().GetAll(context.Background())
if err != nil {
return errors.Wrap(err, "projectRepoFactory.GetAll()")
}
Expand Down Expand Up @@ -598,6 +598,14 @@ func Initialize(l log.Logger, conf config.Provider) error {
clusterPlanner.Close()
clusterServer.Shutdown()

sqlConn, err := dbConn.DB()
if err != nil {
terminalError = multierror.Append(terminalError, errors.Wrap(err, "dbConn.DB"))
}
if err := sqlConn.Close(); err != nil {
terminalError = multierror.Append(terminalError, errors.Wrap(err, "sqlConn.Close"))
}

l.Info("bye")
return terminalError
}
Expand Down
28 changes: 14 additions & 14 deletions datastore/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ type Service struct {
uuidProvider utils.UUIDProvider
}

func (srv Service) GetAll(namespace models.NamespaceSpec, datastoreName string) ([]models.ResourceSpec, error) {
func (srv Service) GetAll(ctx context.Context, namespace models.NamespaceSpec, datastoreName string) ([]models.ResourceSpec, error) {
ds, err := srv.dsRepo.GetByName(datastoreName)
if err != nil {
return nil, err
}
return srv.resourceRepoFactory.New(namespace, ds).GetAll()
return srv.resourceRepoFactory.New(namespace, ds).GetAll(ctx)
}

func (srv Service) CreateResource(ctx context.Context, namespace models.NamespaceSpec, resourceSpecs []models.ResourceSpec, obs progress.Observer) error {
Expand All @@ -55,7 +55,7 @@ func (srv Service) CreateResource(ctx context.Context, namespace models.Namespac
currentSpec := resourceSpec
repo := srv.resourceRepoFactory.New(namespace, currentSpec.Datastore)
runner.Add(func() (interface{}, error) {
if err := repo.Save(currentSpec); err != nil {
if err := repo.Save(ctx, currentSpec); err != nil {
return nil, err
}

Expand Down Expand Up @@ -86,7 +86,7 @@ func (srv Service) UpdateResource(ctx context.Context, namespace models.Namespac
currentSpec := resourceSpec
repo := srv.resourceRepoFactory.New(namespace, currentSpec.Datastore)
runner.Add(func() (interface{}, error) {
if err := repo.Save(currentSpec); err != nil {
if err := repo.Save(ctx, currentSpec); err != nil {
return nil, err
}

Expand Down Expand Up @@ -117,7 +117,7 @@ func (srv Service) ReadResource(ctx context.Context, namespace models.NamespaceS
return models.ResourceSpec{}, err
}
repo := srv.resourceRepoFactory.New(namespace, ds)
dbSpec, err := repo.GetByName(name)
dbSpec, err := repo.GetByName(ctx, name)
if err != nil {
return models.ResourceSpec{}, err
}
Expand All @@ -138,7 +138,7 @@ func (srv Service) DeleteResource(ctx context.Context, namespace models.Namespac
return err
}
repo := srv.resourceRepoFactory.New(namespace, ds)
resourceSpec, err := repo.GetByName(name)
resourceSpec, err := repo.GetByName(ctx, name)
if err != nil {
return err
}
Expand All @@ -151,7 +151,7 @@ func (srv Service) DeleteResource(ctx context.Context, namespace models.Namespac
return err
}

return repo.Delete(name)
return repo.Delete(ctx, name)
}

func generateResourceDestination(ctx context.Context, jobSpec models.JobSpec) (*models.GenerateDestinationResponse, error) {
Expand All @@ -161,9 +161,9 @@ func generateResourceDestination(ctx context.Context, jobSpec models.JobSpec) (*
})
}

func (srv Service) getResourceSpec(datastorer models.Datastorer, namespace models.NamespaceSpec, destinationURN string) (models.ResourceSpec, error) {
func (srv Service) getResourceSpec(ctx context.Context, datastorer models.Datastorer, namespace models.NamespaceSpec, destinationURN string) (models.ResourceSpec, error) {
repo := srv.resourceRepoFactory.New(namespace, datastorer)
return repo.GetByURN(destinationURN)
return repo.GetByURN(ctx, destinationURN)
}

func (srv Service) BackupResourceDryRun(ctx context.Context, backupRequest models.BackupRequest, jobSpecs []models.JobSpec) ([]string, error) {
Expand All @@ -179,7 +179,7 @@ func (srv Service) BackupResourceDryRun(ctx context.Context, backupRequest model
return nil, err
}

resourceSpec, err := srv.getResourceSpec(datastorer, backupRequest.Namespace, destination.URN())
resourceSpec, err := srv.getResourceSpec(ctx, datastorer, backupRequest.Namespace, destination.URN())
if err != nil {
if err == store.ErrResourceNotFound {
continue
Expand Down Expand Up @@ -223,7 +223,7 @@ func (srv Service) BackupResource(ctx context.Context, backupRequest models.Back
return nil, err
}

resourceSpec, err := srv.getResourceSpec(datastorer, backupRequest.Namespace, destination.URN())
resourceSpec, err := srv.getResourceSpec(ctx, datastorer, backupRequest.Namespace, destination.URN())
if err != nil {
if err == store.ErrResourceNotFound {
continue
Expand Down Expand Up @@ -258,21 +258,21 @@ func (srv Service) BackupResource(ctx context.Context, backupRequest models.Back

//save the backup
backupRepo := srv.backupRepoFactory.New(backupRequest.Project, backupSpec.Resource.Datastore)
if err := backupRepo.Save(backupSpec); err != nil {
if err := backupRepo.Save(ctx, backupSpec); err != nil {
return nil, err
}

return backupResult, nil
}

func (srv Service) ListBackupResources(projectSpec models.ProjectSpec, datastoreName string) ([]models.BackupSpec, error) {
func (srv Service) ListBackupResources(ctx context.Context, projectSpec models.ProjectSpec, datastoreName string) ([]models.BackupSpec, error) {
datastorer, err := srv.dsRepo.GetByName(datastoreName)
if err != nil {
return []models.BackupSpec{}, err
}

backupRepo := srv.backupRepoFactory.New(projectSpec, datastorer)
backupSpecs, err := backupRepo.GetAll()
backupSpecs, err := backupRepo.GetAll(ctx)
if err != nil {
if err == store.ErrResourceNotFound {
return []models.BackupSpec{}, nil
Expand Down
Loading

0 comments on commit f98a15a

Please sign in to comment.