mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 01:12:56 +03:00
00f5df3e47
Closes https://hasurahq.atlassian.net/browse/PLAT-292 PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6955 GitOrigin-RevId: 41d77ebbe830b29e0ba0cdf6c3a67527e5580bdf
103 lines
2.6 KiB
Go
103 lines
2.6 KiB
Go
package migrate
|
|
|
|
import (
|
|
"fmt"
|
|
"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"
|
|
)
|
|
|
|
type ProjectMigrate struct {
|
|
ec *cli.ExecutionContext
|
|
}
|
|
|
|
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())
|
|
}
|
|
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())
|
|
}
|
|
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)
|
|
r, err := applier.apply(opts...)
|
|
if err != nil {
|
|
return nil, errors.E(op, err)
|
|
}
|
|
return r, nil
|
|
}
|
|
|
|
func (p *ProjectMigrate) Delete(opts ...ProjectMigrationDeleterOption) error {
|
|
var op errors.Op = "migrate.ProjectMigrate.Delete"
|
|
deleter := newProjectMigrationsDeleter(p.ec)
|
|
if err := deleter.delete(opts...); err != nil {
|
|
return errors.E(op, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func NewProjectMigrate(projectDirectory string, opts ...ProjectMigrateOption) (*ProjectMigrate, error) {
|
|
var op errors.Op = "migrate.NewProjectMigrate"
|
|
p := &ProjectMigrate{}
|
|
ec := cli.NewExecutionContext()
|
|
ec.ExecutionDirectory = projectDirectory
|
|
ec.Viper = viper.New()
|
|
|
|
ec.IsTerminal = false
|
|
ec.Stderr = io.Discard
|
|
ec.Stdout = io.Discard
|
|
if err := ec.Prepare(); err != nil {
|
|
return nil, errors.E(op, err)
|
|
}
|
|
p.ec = ec
|
|
for _, opt := range opts {
|
|
opt(p)
|
|
}
|
|
if err := ec.Validate(); err != nil {
|
|
return nil, errors.E(op, err)
|
|
}
|
|
if ec.Config.Version <= cli.V1 {
|
|
return nil, errors.E(op, fmt.Errorf("config %v is not supported", ec.Config.Version))
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
type ProjectMigrateOption func(*ProjectMigrate)
|
|
|
|
func WithEndpoint(endpoint string) ProjectMigrateOption {
|
|
return func(m *ProjectMigrate) {
|
|
m.ec.Viper.Set("endpoint", endpoint)
|
|
}
|
|
}
|
|
func WithAdminSecret(adminSecret string) ProjectMigrateOption {
|
|
return func(m *ProjectMigrate) {
|
|
m.ec.Viper.Set("admin_secret", adminSecret)
|
|
}
|
|
}
|