Skip to content

Commit

Permalink
cli: port pkg/migrate to use internal/errors
Browse files Browse the repository at this point in the history
PR-URL: hasura/graphql-engine-mono#6491
Co-authored-by: Mohd Bilal <24944223+m-Bilal@users.noreply.github.com>
GitOrigin-RevId: 37640b3f888f6ca68df6894fa1d0f9a5368136b6
  • Loading branch information
2 people authored and hasura-bot committed Oct 27, 2022
1 parent a649967 commit 2838665
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 64 deletions.
4 changes: 3 additions & 1 deletion cli/pkg/migrate/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package migrate
import (
"github.com/hasura/graphql-engine/cli/v2"
"github.com/hasura/graphql-engine/cli/v2/commands"
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
)

type projectMigrationsApplier struct {
Expand Down Expand Up @@ -48,13 +49,14 @@ func ApplyVersion(version string, direction MigrationDirection) ProjectMigration
}

func (p *projectMigrationsApplier) apply(opts ...ProjectMigrationApplierOption) ([]ApplyResult, error) {
var op errors.Op = "migrate.projectMigrationsApplier.apply"
for _, opt := range opts {
opt(p)
}
var results []ApplyResult
resultChan, err := p.opts.Apply()
if err != nil {
return nil, err
return nil, errors.E(op, err)
}
for v := range resultChan {
results = append(results, ApplyResult(v))
Expand Down
29 changes: 23 additions & 6 deletions cli/pkg/migrate/project_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"

"github.com/hasura/graphql-engine/cli/v2/commands"
"github.com/hasura/graphql-engine/cli/v2/internal/errors"

"github.com/hasura/graphql-engine/cli/v2"
"github.com/spf13/viper"
Expand All @@ -15,29 +16,45 @@ type ProjectMigrate struct {
}

func (p *ProjectMigrate) status(opts ...ProjectMigrationStatusOption) ([]databaseMigration, error) {
var op errors.Op = "migrate.ProjectMigrate.status"
lister := newProjectMigrationsStatus(p.ec)
if len(opts) == 0 {
opts = append(opts, StatusAllDatabases())
}
return lister.Status(opts...)
dms, err := lister.Status(opts...)
if err != nil {
return nil, errors.E(op, err)
}
return dms, nil
}

func (p *ProjectMigrate) StatusJSON(opts ...ProjectMigrationStatusOption) (io.Reader, error) {
var op errors.Op = "migrate.ProjectMigrate.StatusJSON"
lister := newProjectMigrationsStatus(p.ec)
if len(opts) == 0 {
opts = append(opts, StatusAllDatabases())
}
return lister.StatusJSON(opts...)
r, err := lister.StatusJSON(opts...)
if err != nil {
return nil, errors.E(op, err)
}
return r, nil
}

type ApplyResult commands.MigrateApplyResult

func (p *ProjectMigrate) Apply(opts ...ProjectMigrationApplierOption) ([]ApplyResult, error) {
var op errors.Op = "migrate.ProjectMigrate.Apply"
applier := newProjectMigrationsApplier(p.ec)
return applier.apply(opts...)
r, err := applier.apply(opts...)
if err != nil {
return nil, errors.E(op, err)
}
return r, nil
}

func NewProjectMigrate(projectDirectory string, opts ...ProjectMigrateOption) (*ProjectMigrate, error) {
var op errors.Op = "migrate.NewProjectMigrate"
p := &ProjectMigrate{}
ec := cli.NewExecutionContext()
ec.ExecutionDirectory = projectDirectory
Expand All @@ -47,17 +64,17 @@ func NewProjectMigrate(projectDirectory string, opts ...ProjectMigrateOption) (*
ec.Stderr = io.Discard
ec.Stdout = io.Discard
if err := ec.Prepare(); err != nil {
return nil, err
return nil, errors.E(op, err)
}
p.ec = ec
for _, opt := range opts {
opt(p)
}
if err := ec.Validate(); err != nil {
return nil, err
return nil, errors.E(op, err)
}
if ec.Config.Version <= cli.V1 {
return nil, fmt.Errorf("config %v is not supported", ec.Config.Version)
return nil, errors.E(op, fmt.Errorf("config %v is not supported", ec.Config.Version))
}
return p, nil
}
Expand Down
Loading

0 comments on commit 2838665

Please sign in to comment.