2021-06-18 09:24:16 +03:00
|
|
|
package migrate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/hasura/graphql-engine/cli/v2"
|
|
|
|
"github.com/hasura/graphql-engine/cli/v2/commands"
|
2022-10-27 19:16:07 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
|
2021-06-18 09:24:16 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/metadatautil"
|
|
|
|
"github.com/hasura/graphql-engine/cli/v2/migrate"
|
|
|
|
)
|
|
|
|
|
|
|
|
type databaseMigration struct {
|
|
|
|
DatabaseName string `json:"databaseName"`
|
|
|
|
Status migrate.Status `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type projectMigrationsStatus struct {
|
|
|
|
ec *cli.ExecutionContext
|
|
|
|
allDatabases bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *projectMigrationsStatus) Status(opts ...ProjectMigrationStatusOption) ([]databaseMigration, error) {
|
2022-10-27 19:16:07 +03:00
|
|
|
var op errors.Op = "migrate.projectMigrationsStatus.Status"
|
2021-06-18 09:24:16 +03:00
|
|
|
var migrateStatus []databaseMigration
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(p)
|
|
|
|
}
|
|
|
|
if p.allDatabases {
|
|
|
|
metadataOps := cli.GetCommonMetadataOps(p.ec)
|
2021-08-16 09:43:11 +03:00
|
|
|
sources, err := metadatautil.GetSourcesAndKindStrict(metadataOps.ExportMetadata)
|
2021-06-18 09:24:16 +03:00
|
|
|
if err != nil {
|
2022-10-27 19:16:07 +03:00
|
|
|
return nil, errors.E(op, err)
|
2021-06-18 09:24:16 +03:00
|
|
|
}
|
|
|
|
for _, source := range sources {
|
|
|
|
opts := commands.MigrateStatusOptions{
|
|
|
|
EC: p.ec,
|
|
|
|
Source: cli.Source{
|
|
|
|
Name: source.Name,
|
|
|
|
Kind: source.Kind,
|
|
|
|
},
|
|
|
|
}
|
2021-12-23 18:58:53 +03:00
|
|
|
status, err := opts.RunOnSource()
|
2021-06-18 09:24:16 +03:00
|
|
|
if err != nil {
|
2022-10-27 19:16:07 +03:00
|
|
|
return nil, errors.E(op, err)
|
2021-06-18 09:24:16 +03:00
|
|
|
}
|
|
|
|
migrateStatus = append(
|
|
|
|
migrateStatus,
|
|
|
|
databaseMigration{
|
|
|
|
DatabaseName: source.Name,
|
|
|
|
Status: *status,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return migrateStatus, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *projectMigrationsStatus) StatusJSON(opts ...ProjectMigrationStatusOption) (io.Reader, error) {
|
2022-10-27 19:16:07 +03:00
|
|
|
var op errors.Op = "migrate.projectMigrationsStatus.StatusJSON"
|
2021-06-18 09:24:16 +03:00
|
|
|
d, err := p.Status(opts...)
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
if err != nil {
|
2022-10-27 19:16:07 +03:00
|
|
|
return nil, errors.E(op, err)
|
2021-06-18 09:24:16 +03:00
|
|
|
}
|
|
|
|
if err := json.NewEncoder(b).Encode(d); err != nil {
|
2022-10-27 19:16:07 +03:00
|
|
|
return nil, errors.E(op, fmt.Errorf("error encoding migration status as json: %w", err))
|
2021-06-18 09:24:16 +03:00
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectMigrationStatusOption func(applier *projectMigrationsStatus)
|
|
|
|
|
|
|
|
func newProjectMigrationsStatus(ec *cli.ExecutionContext) *projectMigrationsStatus {
|
|
|
|
p := &projectMigrationsStatus{ec: ec}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
func StatusAllDatabases() ProjectMigrationStatusOption {
|
|
|
|
return func(p *projectMigrationsStatus) {
|
|
|
|
p.allDatabases = true
|
|
|
|
}
|
|
|
|
}
|