Skip to content

Commit

Permalink
Starting to move apply to the migrations object
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed Jan 19, 2020
1 parent 0bc3052 commit b711043
Show file tree
Hide file tree
Showing 17 changed files with 454 additions and 471 deletions.
32 changes: 32 additions & 0 deletions config/crds/v1/schemas.schemahero.io_migrations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,41 @@ spec:
type: object
spec:
description: MigrationSpec defines the desired state of Migration
properties:
editedDDL:
type: string
generatedDDL:
type: string
tableName:
type: string
tableNamespace:
type: string
required:
- tableName
- tableNamespace
type: object
status:
description: MigrationStatus defines the observed state of Migration
properties:
approvedAt:
format: int64
type: integer
executedAt:
format: int64
type: integer
invalidatedAt:
description: InvalidatedAt is the unix nano timestamp when this plan
was determined to be invalid or outdated
format: int64
type: integer
plannedAt:
description: PlannedAt is the unix nano timestamp when the plan was
generated
format: int64
type: integer
rejectedAt:
format: int64
type: integer
type: object
type: object
served: true
Expand Down
29 changes: 0 additions & 29 deletions config/crds/v1/schemas.schemahero.io_tables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -204,35 +204,6 @@ spec:
type: object
status:
description: TableStatus defines the observed state of Table
properties:
plans:
items:
properties:
approvedAt:
format: int64
type: integer
ddl:
type: string
executedAt:
format: int64
type: integer
invalidatedAt:
description: InvalidatedAt is the unix nano timestamp when this
plan was determined to be invalid or outdated
format: int64
type: integer
name:
type: string
plannedAt:
description: PlannedAt is the unix nano timestamp when the plan
was generated
format: int64
type: integer
rejectedAt:
format: int64
type: integer
type: object
type: array
type: object
type: object
served: true
Expand Down
32 changes: 32 additions & 0 deletions config/crds/v1beta1/schemas.schemahero.io_migrations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,41 @@ spec:
type: object
spec:
description: MigrationSpec defines the desired state of Migration
properties:
editedDDL:
type: string
generatedDDL:
type: string
tableName:
type: string
tableNamespace:
type: string
required:
- tableName
- tableNamespace
type: object
status:
description: MigrationStatus defines the observed state of Migration
properties:
approvedAt:
format: int64
type: integer
executedAt:
format: int64
type: integer
invalidatedAt:
description: InvalidatedAt is the unix nano timestamp when this plan
was determined to be invalid or outdated
format: int64
type: integer
plannedAt:
description: PlannedAt is the unix nano timestamp when the plan was
generated
format: int64
type: integer
rejectedAt:
format: int64
type: integer
type: object
type: object
version: v1alpha3
Expand Down
29 changes: 0 additions & 29 deletions config/crds/v1beta1/schemas.schemahero.io_tables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -202,35 +202,6 @@ spec:
type: object
status:
description: TableStatus defines the observed state of Table
properties:
plans:
items:
properties:
approvedAt:
format: int64
type: integer
ddl:
type: string
executedAt:
format: int64
type: integer
invalidatedAt:
description: InvalidatedAt is the unix nano timestamp when this
plan was determined to be invalid or outdated
format: int64
type: integer
name:
type: string
plannedAt:
description: PlannedAt is the unix nano timestamp when the plan
was generated
format: int64
type: integer
rejectedAt:
format: int64
type: integer
type: object
type: array
type: object
type: object
version: v1alpha3
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,12 @@ github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQL
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA=
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4=
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
Expand Down Expand Up @@ -458,6 +460,7 @@ google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyac
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
Expand Down
49 changes: 43 additions & 6 deletions pkg/apis/schemas/v1alpha3/migration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,59 @@ limitations under the License.
package v1alpha3

import (
"encoding/json"
"time"

"github.com/pkg/errors"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
func (s Migration) GetOutput(format string) ([]byte, error) {
output := map[string]interface{}{
"Name": s.Name,
"PlannedAt": time.Unix(s.Status.PlannedAt, 0).Format(time.RFC3339),
"Migration": s.Spec.GeneratedDDL,
}

result := []byte("")
if format == "json" {
b, err := json.Marshal(output)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal output to json")
}
result = b
} else if format == "yaml" {
b, err := yaml.Marshal(output)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal output to yaml")
}
result = b
}

return result, nil
}

// MigrationSpec defines the desired state of Migration
type MigrationSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
TableName string `json:"tableName"`
TableNamespace string `json:"tableNamespace"`
GeneratedDDL string `json:"generatedDDL,omitempty"`
EditedDDL string `json:"editedDDL,omitempty"`
}

// MigrationStatus defines the observed state of Migration
type MigrationStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
// PlannedAt is the unix nano timestamp when the plan was generated
PlannedAt int64 `json:"plannedAt,omitempty"`

// InvalidatedAt is the unix nano timestamp when this plan was determined to be invalid or outdated
InvalidatedAt int64 `json:"invalidatedAt,omitempty"`

ApprovedAt int64 `json:"approvedAt,omitempty"`
RejectedAt int64 `json:"rejectedAt,omitempty"`

ExecutedAt int64 `json:"executedAt,omitempty"`
}

// +genclient
Expand Down
45 changes: 0 additions & 45 deletions pkg/apis/schemas/v1alpha3/table_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"
"time"

"github.com/pkg/errors"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -41,51 +39,8 @@ type TableSpec struct {
Schema *TableSchema `json:"schema" yaml:"schema"`
}

type TablePlan struct {
Name string `json:"name,omitempty"`
DDL string `json:"ddl,omitempty"`

// PlannedAt is the unix nano timestamp when the plan was generated
PlannedAt int64 `json:"plannedAt,omitempty"`

// InvalidatedAt is the unix nano timestamp when this plan was determined to be invalid or outdated
InvalidatedAt int64 `json:"invalidatedAt,omitempty"`

ApprovedAt int64 `json:"approvedAt,omitempty"`
RejectedAt int64 `json:"rejectedAt,omitempty"`

ExecutedAt int64 `json:"executedAt,omitempty"`
}

func (tp TablePlan) GetOutput(format string) ([]byte, error) {
output := map[string]interface{}{
"Name": tp.Name,
"PlannedAt": time.Unix(tp.PlannedAt, 0).Format(time.RFC3339),
"Migration": tp.DDL,
}

result := []byte("")
if format == "json" {
b, err := json.Marshal(output)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal output to json")
}
result = b
} else if format == "yaml" {
b, err := yaml.Marshal(output)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal output to yaml")
}
result = b
}

return result, nil
}

// TableStatus defines the observed state of Table
type TableStatus struct {
Plans []*TablePlan `json:"plans,omitempty"`
// Plan string `json:"plans,omitempty"`
}

// +genclient
Expand Down
48 changes: 23 additions & 25 deletions pkg/cli/schemaherokubectlcli/approve_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package schemaherokubectlcli

import (
"fmt"
"strings"
"time"

schemasv1alpha3 "github.com/schemahero/schemahero/pkg/apis/schemas/v1alpha3"
schemasclientv1alpha3 "github.com/schemahero/schemahero/pkg/client/schemaheroclientset/typed/schemas/v1alpha3"
Expand All @@ -26,7 +24,7 @@ func ApproveMigrationCmd() *cobra.Command {
},
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 Down Expand Up @@ -66,23 +64,23 @@ func ApproveMigrationCmd() *cobra.Command {
matchingPlanName := ""
matchingNamespace := ""

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)
matchingPlanName = plan.Name
matchingNamespace = namespaceName
}
}
}
}
// 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)
// matchingPlanName = plan.Name
// matchingNamespace = namespaceName
// }
// }
// }
// }

if len(matchingTables) == 0 {
fmt.Println("No resources found.")
Expand All @@ -95,11 +93,11 @@ func ApproveMigrationCmd() *cobra.Command {
}

table := matchingTables[0]
for _, plan := range table.Status.Plans {
if plan.Name == matchingPlanName {
plan.ApprovedAt = time.Now().Unix()
}
}
// for _, plan := range table.Status.Plans {
// if plan.Name == matchingPlanName {
// plan.ApprovedAt = time.Now().Unix()
// }
// }

if _, err := schemasClient.Tables(matchingNamespace).Update(table); err != nil {
return err
Expand Down

0 comments on commit b711043

Please sign in to comment.