Skip to content

Commit

Permalink
Merge branch '5542-2' of github.com:paf31/graphql-engine into 5542-2
Browse files Browse the repository at this point in the history
  • Loading branch information
paf31 committed Aug 10, 2020
2 parents ac5e610 + 7e3397e commit 874fe0e
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ If you do have such headers configured, then you must update the header configur
- console: handle nested fragments in allowed queries (close #5137) (#5252)
- console: update sidebar icons for different action and trigger types (#5445)
- console: make add column UX consistent with others (#5486)
- cli: improve error messages thrown when metadata apply fails (#5513)
- cli: fix issue with creating seed migrations while using tables with capital letters (closes #5532) (#5549)
- build: introduce additional log kinds for cli-migrations image (#5529)

## `v1.3.0`
Expand Down
10 changes: 5 additions & 5 deletions cli/commands/seed_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,26 @@ func (o *SeedNewOptions) Run() error {
// If we are initializing from a database table
// create a hasura client and add table name opts
if createSeedOpts.Data == nil {
var body []byte
if len(o.FromTableNames) > 0 {
migrateDriver, err := migrate.NewMigrate(ec, true)
if err != nil {
return errors.Wrap(err, "cannot initialize migrate driver")
}
// Send the query
body, err := migrateDriver.ExportDataDump(o.FromTableNames)
body, err = migrateDriver.ExportDataDump(o.FromTableNames)
if err != nil {
return errors.Wrap(err, "exporting seed data")
}

createSeedOpts.Data = bytes.NewReader(body)
} else {
const defaultText = ""
data, err := editor.CaptureInputFromEditor(editor.GetPreferredEditorFromEnvironment, defaultText, "*.sql")
var err error
body, err = editor.CaptureInputFromEditor(editor.GetPreferredEditorFromEnvironment, defaultText, "*.sql")
if err != nil {
return errors.Wrap(err, "cannot find default editor from env")
}
createSeedOpts.Data = bytes.NewReader(data)
}
createSeedOpts.Data = bytes.NewReader(body)
}

fs := afero.NewOsFs()
Expand Down
2 changes: 1 addition & 1 deletion cli/migrate/database/hasuradb/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (h *HasuraDB) ApplyMetadata() error {
if err != nil {
return err
}
herror.migrationQuery = "offending object: \n\r\n\r" + string(queryData)
h.logger.Debugf("offending object: \n\r\n\r" + string(queryData))
}
}
return herror
Expand Down
66 changes: 52 additions & 14 deletions cli/migrate/database/hasuradb/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,35 @@ type HasuraError struct {
Code string `json:"code"`
}

type InconsistentMetadataError struct {
Definition interface{} `json:"definition,omitempty" mapstructure:"definition,omitempty"`
Reason string `json:"reason,omitempty" mapstructure:"reason,omitempty"`
Type string `json:"type,omitempty" mapstructure:"type,omitempty"`
}

func (mderror *InconsistentMetadataError) String() string {
var out string
if mderror.Reason != "" {
out = fmt.Sprintf("\nreason: %v\n", mderror.Reason)
}
if mderror.Type != "" {
out = fmt.Sprintf("%stype: %v\n", out, mderror.Type)
}
if mderror.Definition != nil {
m, err := json.MarshalIndent(mderror.Definition, "", " ")
if err == nil {
out = fmt.Sprintf("%sdefinition: \n%s", out, string(m))
}
}
return out
}

type SQLInternalError struct {
Arguments []string `json:"arguments" mapstructure:"arguments,omitempty"`
Error PostgresError `json:"error" mapstructure:"error,omitempty"`
Prepared bool `json:"prepared" mapstructure:"prepared,omitempty"`
Statement string `json:"statement" mapstructure:"statement,omitempty"`
Arguments []string `json:"arguments" mapstructure:"arguments,omitempty"`
Error *PostgresError `json:"error" mapstructure:"error,omitempty"`
Prepared bool `json:"prepared" mapstructure:"prepared,omitempty"`
Statement string `json:"statement" mapstructure:"statement,omitempty"`
InconsistentMetadataError `mapstructure:",squash"`
}
type PostgresError struct {
StatusCode string `json:"status_code" mapstructure:"status_code,omitempty"`
Expand Down Expand Up @@ -323,12 +347,17 @@ func (h HasuraError) Error() string {
err := mapstructure.Decode(v, &internalError)
if err == nil {
// postgres error
errorStrings = append(errorStrings, fmt.Sprintf("[%s] %s: %s", internalError.Error.StatusCode, internalError.Error.ExecStatus, internalError.Error.Message))
if len(internalError.Error.Description) > 0 {
errorStrings = append(errorStrings, fmt.Sprintf("Description: %s", internalError.Error.Description))
if internalError.Error != nil {
errorStrings = append(errorStrings, fmt.Sprintf("[%s] %s: %s", internalError.Error.StatusCode, internalError.Error.ExecStatus, internalError.Error.Message))
if len(internalError.Error.Description) > 0 {
errorStrings = append(errorStrings, fmt.Sprintf("Description: %s", internalError.Error.Description))
}
if len(internalError.Error.Hint) > 0 {
errorStrings = append(errorStrings, fmt.Sprintf("Hint: %s", internalError.Error.Hint))
}
}
if len(internalError.Error.Hint) > 0 {
errorStrings = append(errorStrings, fmt.Sprintf("Hint: %s", internalError.Error.Hint))
if e := internalError.InconsistentMetadataError.String(); e != "" {
errorStrings = append(errorStrings, e)
}
}
}
Expand All @@ -337,16 +366,25 @@ func (h HasuraError) Error() string {
if err == nil {
for _, internalError := range internalErrors {
// postgres error
errorStrings = append(errorStrings, fmt.Sprintf("[%s] %s: %s", internalError.Error.StatusCode, internalError.Error.ExecStatus, internalError.Error.Message))
if len(internalError.Error.Description) > 0 {
errorStrings = append(errorStrings, fmt.Sprintf("Description: %s", internalError.Error.Description))
if internalError.Error != nil {
errorStrings = append(errorStrings, fmt.Sprintf("[%s] %s: %s", internalError.Error.StatusCode, internalError.Error.ExecStatus, internalError.Error.Message))
if len(internalError.Error.Description) > 0 {
errorStrings = append(errorStrings, fmt.Sprintf("Description: %s", internalError.Error.Description))
}
if len(internalError.Error.Hint) > 0 {
errorStrings = append(errorStrings, fmt.Sprintf("Hint: %s", internalError.Error.Hint))
}
}
if len(internalError.Error.Hint) > 0 {
errorStrings = append(errorStrings, fmt.Sprintf("Hint: %s", internalError.Error.Hint))

if e := internalError.InconsistentMetadataError.String(); e != "" {
errorStrings = append(errorStrings, e)
}
}
}
}
if len(errorStrings) == 0 {
return ""
}
return strings.Join(errorStrings, "\r\n")
}

Expand Down
91 changes: 91 additions & 0 deletions cli/migrate/database/hasuradb/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,94 @@ func TestHasuraError_Error(t *testing.T) {
})
}
}

func TestInconsistentMetadataError_String(t *testing.T) {
type fields struct {
Definition interface{}
Reason string
Type string
}
tests := []struct {
name string
fields fields
want string
}{
{
"can generate error correctly when all fields are given",
fields{
Reason: "test reason",
Type: "test",
Definition: func() interface{} {
var m interface{}
err := json.Unmarshal([]byte(`{"test": "test"}`), &m)
if err != nil {
t.Error(err)
}
return m
}(),
},
`
reason: test reason
type: test
definition:
{
"test": "test"
}`,
},
{
"will not panic when Definition is not a valid json (string)",
fields{
Definition: func() interface{} {
return "test"
}(),
Reason: "",
Type: "",
},
`definition:
"test"`,
},
{
"will not panic when Definition is not a valid json (Int)",
fields{
Definition: func() interface{} {
return 1
}(),
Reason: "",
Type: "",
},
`definition:
1`,
},
{
"will not panic when Definition is (struct Array)",
fields{
Definition: func() interface{} {
return []struct{Name string}{ { "test" } , { "test"} }
}(),
Reason: "",
Type: "",
},
`definition:
[
{
"Name": "test"
},
{
"Name": "test"
}
]`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mderror := &InconsistentMetadataError{
Definition: tt.fields.Definition,
Reason: tt.fields.Reason,
Type: tt.fields.Type,
}
if got := mderror.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
})
}
}
7 changes: 6 additions & 1 deletion cli/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -1847,7 +1847,12 @@ func (m *Migrate) ApplySeed(q interface{}) error {
}

func (m *Migrate) ExportDataDump(tableNames []string) ([]byte, error) {
return m.databaseDrv.ExportDataDump(tableNames)
// to support tables starting with capital letters
modifiedTableNames := make([]string, len(tableNames))
for idx, val := range tableNames {
modifiedTableNames[idx] = fmt.Sprintf(`"%s"`, val)
}
return m.databaseDrv.ExportDataDump(modifiedTableNames)
}

func printDryRunStatus(migrations []*Migration) *bytes.Buffer {
Expand Down

0 comments on commit 874fe0e

Please sign in to comment.