mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-17 12:31:52 +03:00
9d15c07a9d
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4171 GitOrigin-RevId: cd5a4a7159c89e6e2b9e97c4b187049515637145
64 lines
1.6 KiB
Go
64 lines
1.6 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
|
|
}
|
|
}
|
|
|
|
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) {
|
|
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
|
|
}
|