mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-17 20:41:49 +03:00
50 lines
1.3 KiB
Go
50 lines
1.3 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.AllDatabases = true
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func ApplyOnDatabaseName(databaseName string) ProjectMigrationApplierOption {
|
||
|
return func(p *projectMigrationsApplier) {
|
||
|
p.opts.Source.Name = databaseName
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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) error {
|
||
|
for _, opt := range opts {
|
||
|
opt(p)
|
||
|
}
|
||
|
return p.opts.Run()
|
||
|
}
|