2021-06-18 09:24:16 +03:00
|
|
|
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) {
|
2021-12-23 18:58:53 +03:00
|
|
|
p.opts.EC.AllDatabases = true
|
2021-06-18 09:24:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ApplyOnDatabaseName(databaseName string) ProjectMigrationApplierOption {
|
|
|
|
return func(p *projectMigrationsApplier) {
|
|
|
|
p.opts.Source.Name = databaseName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 07:53:31 +03:00
|
|
|
func ApplyWithSkipExecution() ProjectMigrationApplierOption {
|
|
|
|
return func(p *projectMigrationsApplier) {
|
|
|
|
p.opts.SkipExecution = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 09:24:16 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-16 09:43:11 +03:00
|
|
|
func (p *projectMigrationsApplier) apply(opts ...ProjectMigrationApplierOption) ([]ApplyResult, error) {
|
2021-06-18 09:24:16 +03:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(p)
|
|
|
|
}
|
2021-08-16 09:43:11 +03:00
|
|
|
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
|
2021-06-18 09:24:16 +03:00
|
|
|
}
|