Skip to content

Commit

Permalink
get and describe migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed Jan 19, 2020
1 parent b00a390 commit b2ae59e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 78 deletions.
70 changes: 26 additions & 44 deletions pkg/cli/schemaherokubectlcli/describe_migration.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package schemaherokubectlcli

import (
"errors"
"fmt"

schemasv1alpha3 "github.com/schemahero/schemahero/pkg/apis/schemas/v1alpha3"
"github.com/pkg/errors"
schemasclientv1alpha3 "github.com/schemahero/schemahero/pkg/client/schemaheroclientset/typed/schemas/v1alpha3"
"github.com/spf13/cobra"
"github.com/spf13/viper"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client/config"
Expand All @@ -19,12 +20,13 @@ func DescribeMigrationCmd() *cobra.Command {
Long: `...`,
Args: cobra.MinimumNArgs(1),
SilenceErrors: true,
SilenceUsage: true,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
v := viper.GetViper()
// migrationName := args[0]
migrationName := args[0]

cfg, err := config.GetConfig()
if err != nil {
Expand All @@ -36,10 +38,10 @@ func DescribeMigrationCmd() *cobra.Command {
return err
}

// schemasClient, err := schemasclientv1alpha3.NewForConfig(cfg)
// if err != nil {
// return err
// }
schemasClient, err := schemasclientv1alpha3.NewForConfig(cfg)
if err != nil {
return err
}

namespaceNames := []string{}

Expand All @@ -60,48 +62,28 @@ func DescribeMigrationCmd() *cobra.Command {
}
}

matchingTables := []*schemasv1alpha3.Table{}
matchingPlans := []*schemasv1alpha3.Migration{}

// for _, namespaceName := range namespaceNames {
// // TODO this could be rewritten to use a fieldselector and find the table quicker
// tables, err := schemasClient.Tables(namespaceName).List(metav1.ListOptions{})
// if err != nil {
// return err
// }

// for _, table := range tables.Items {
// for _, plan := range table.Status.Plans {
// if strings.HasPrefix(plan.Name, migrationName) {
// matchingTables = append(matchingTables, &table)
// matchingPlans = append(matchingPlans, plan)
// }
// }
// }
// }

if len(matchingTables) == 0 {
fmt.Println("No resources found.")
return nil
}
for _, namespaceName := range namespaceNames {
foundMigration, err := schemasClient.Migrations(namespaceName).Get(migrationName, metav1.GetOptions{})
if kuberneteserrors.IsNotFound(err) {
// next namespace
continue
}
if err != nil {
return err
}

if len(matchingTables) > 1 {
fmt.Println("Ambiguious migration name. Multiple migrations found with prefix.")
return nil
}
b, err := foundMigration.GetOutput(v.GetString("output"))
if err != nil {
return err
}

if len(matchingTables) != len(matchingPlans) {
fmt.Println("Unexpected error, tables and plan counts do not match")
return errors.New("error")
fmt.Printf("%s\n", b)
return nil
}

b, err := matchingPlans[0].GetOutput(v.GetString("output"))
if err != nil {
return err
}
err = errors.Errorf("migration %q not found", migrationName)
return err

fmt.Printf("%s\n", b)
return nil
},
}

Expand Down
71 changes: 37 additions & 34 deletions pkg/cli/schemaherokubectlcli/get_migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,55 +62,58 @@ func GetMigrationsCmd() *cobra.Command {
}
}

matchingTables := []schemasv1alpha3.Table{}

matchingMigrations := []schemasv1alpha3.Migration{}
for _, namespaceName := range namespaceNames {
tables, err := schemasClient.Tables(namespaceName).List(metav1.ListOptions{})
migrations, err := schemasClient.Migrations(namespaceName).List(metav1.ListOptions{})
if err != nil {
return err
}

for _, table := range tables.Items {
if databaseNameFilter != "" {
if table.Spec.Database != databaseNameFilter {
continue
}
for _, migration := range migrations.Items {
if databaseNameFilter == "" {
matchingMigrations = append(matchingMigrations, migration)
continue
}

table, err := schemasClient.Tables(migration.Spec.TableNamespace).Get(migration.Spec.TableName, metav1.GetOptions{})
if err != nil {
return err
}

matchingTables = append(matchingTables, table)
if table.Spec.Database == databaseNameFilter {
matchingMigrations = append(matchingMigrations, migration)
}
}
}

if len(matchingTables) == 0 {
if len(matchingMigrations) == 0 {
fmt.Println("No resources found.")
return nil
}

rows := [][]string{}
// for _, table := range matchingTables {
// for _, plan := range table.Status.Plans {
// // TODO should we show these?
// if plan.ExecutedAt > 0 {
// continue
// }
// if plan.RejectedAt > 0 {
// continue
// }
// if plan.ApprovedAt > 0 {
// continue
// }

// rows = append(rows, []string{
// plan.Name,
// table.Spec.Database,
// table.Name,
// timestampToAge(plan.PlannedAt),
// timestampToAge(plan.ExecutedAt),
// timestampToAge(plan.ApprovedAt),
// timestampToAge(plan.RejectedAt),
// })
// }
// }
for _, migration := range matchingMigrations {
// TODO should we show these?
if migration.Status.ExecutedAt > 0 {
continue
}
if migration.Status.RejectedAt > 0 {
continue
}
if migration.Status.ApprovedAt > 0 {
continue
}

rows = append(rows, []string{
migration.Name,
"??",
migration.Spec.TableName,
timestampToAge(migration.Status.PlannedAt),
timestampToAge(migration.Status.ExecutedAt),
timestampToAge(migration.Status.ApprovedAt),
timestampToAge(migration.Status.RejectedAt),
})
}

if len(rows) == 0 {
fmt.Println("No resources found.")
Expand Down

0 comments on commit b2ae59e

Please sign in to comment.