2021-06-18 09:24:16 +03:00
|
|
|
package migrate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2021-08-16 09:43:11 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/commands"
|
|
|
|
|
2021-06-18 09:24:16 +03:00
|
|
|
"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) {
|
|
|
|
lister := newProjectMigrationsStatus(p.ec)
|
|
|
|
if len(opts) == 0 {
|
|
|
|
opts = append(opts, StatusAllDatabases())
|
|
|
|
}
|
|
|
|
return lister.Status(opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProjectMigrate) StatusJSON(opts ...ProjectMigrationStatusOption) (io.Reader, error) {
|
|
|
|
lister := newProjectMigrationsStatus(p.ec)
|
|
|
|
if len(opts) == 0 {
|
|
|
|
opts = append(opts, StatusAllDatabases())
|
|
|
|
}
|
|
|
|
return lister.StatusJSON(opts...)
|
|
|
|
}
|
|
|
|
|
2021-08-16 09:43:11 +03:00
|
|
|
type ApplyResult commands.MigrateApplyResult
|
|
|
|
|
|
|
|
func (p *ProjectMigrate) Apply(opts ...ProjectMigrationApplierOption) ([]ApplyResult, error) {
|
2021-06-18 09:24:16 +03:00
|
|
|
applier := newProjectMigrationsApplier(p.ec)
|
2021-08-16 09:43:11 +03:00
|
|
|
return applier.apply(opts...)
|
2021-06-18 09:24:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectMigrate(projectDirectory string, opts ...ProjectMigrateOption) (*ProjectMigrate, error) {
|
|
|
|
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, err
|
|
|
|
}
|
|
|
|
p.ec = ec
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(p)
|
|
|
|
}
|
|
|
|
if err := ec.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ec.Config.Version <= cli.V1 {
|
|
|
|
return nil, 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)
|
|
|
|
}
|
|
|
|
}
|