graphql-engine/cli/pkg/migrate/apply.go
Aravind K P 283866526f cli: port pkg/migrate to use internal/errors
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6491
Co-authored-by: Mohd Bilal <24944223+m-Bilal@users.noreply.github.com>
GitOrigin-RevId: 37640b3f888f6ca68df6894fa1d0f9a5368136b6
2022-10-27 16:17:24 +00:00

66 lines
1.7 KiB
Go

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 {
ec *cli.ExecutionContext
opts commands.MigrateApplyOptions
}
func newProjectMigrationsApplier(ec *cli.ExecutionContext) *projectMigrationsApplier {
p := &projectMigrationsApplier{ec: ec, opts: commands.MigrateApplyOptions{EC: ec}}
return p
}
type ProjectMigrationApplierOption func(applier *projectMigrationsApplier)
func ApplyOnAllDatabases() ProjectMigrationApplierOption {
return func(p *projectMigrationsApplier) {
p.opts.EC.AllDatabases = true
}
}
func ApplyOnDatabaseName(databaseName string) ProjectMigrationApplierOption {
return func(p *projectMigrationsApplier) {
p.opts.Source.Name = databaseName
}
}
func ApplyWithSkipExecution() ProjectMigrationApplierOption {
return func(p *projectMigrationsApplier) {
p.opts.SkipExecution = true
}
}
type MigrationDirection string
const MigrationDirectionUp MigrationDirection = "up"
const MigrationDirectionDown MigrationDirection = "down"
func ApplyVersion(version string, direction MigrationDirection) ProjectMigrationApplierOption {
return func(p *projectMigrationsApplier) {
p.opts.VersionMigration = version
p.opts.MigrationType = string(direction)
}
}
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, errors.E(op, err)
}
for v := range resultChan {
results = append(results, ApplyResult(v))
}
return results, nil
}