graphql-engine/cli/pkg/migrate/apply.go
Kali Vara Purushotham Santhati f0bef2b482 cli: add All option for migrate sub-commands (apply, delete and status) and seed apply command
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/2509
Co-authored-by: Aravind K P <8335904+scriptonist@users.noreply.github.com>
GitOrigin-RevId: 03ee878072b58c6dc7f43af9d26d30a5594d9a50
2021-12-23 15:59:56 +00:00

58 lines
1.5 KiB
Go

package migrate
import (
"github.com/hasura/graphql-engine/cli/v2"
"github.com/hasura/graphql-engine/cli/v2/commands"
)
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
}
}
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) {
for _, opt := range opts {
opt(p)
}
var results []ApplyResult
resultChan, err := p.opts.Apply()
if err != nil {
return nil, err
}
for v := range resultChan {
results = append(results, ApplyResult(v))
}
return results, nil
}