mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 17:31:56 +03:00
283866526f
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
66 lines
1.7 KiB
Go
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
|
|
}
|