From daf098886b0fcc7f19f2c543aa75146fc4b2c444 Mon Sep 17 00:00:00 2001 From: Aravind Date: Wed, 3 Jun 2020 12:13:05 +0530 Subject: [PATCH] cli: fix bug with squashing event triggers (close #4883) (#4949) --- CHANGELOG.md | 1 + cli/migrate/database/hasuradb/squash.go | 29 ++++++++++++++++++------- cli/migrate/database/hasuradb/types.go | 7 ++++-- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e02f501b65..08d064a927d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -82,6 +82,7 @@ Read more about the session argument for computed fields in the [docs](https://h - console: fix visiting view modify page overwriting raw sql content (fix #4798) (#4810) - console: add help button and move about page to settings (#4848) - cli: list all available commands in root command help (fix #4623) (#4628) +- cli: fix bug with squashing event triggers (close #4883) - cli: add support for skipping execution while generating migrations through the migrate REST API - docs: add section on actions vs. remote schemas to actions documentation (#4284) - docs: fix wrong info about excluding scheme in CORS config (#4685) diff --git a/cli/migrate/database/hasuradb/squash.go b/cli/migrate/database/hasuradb/squash.go index 1247c951da5..91f6d702ea1 100644 --- a/cli/migrate/database/hasuradb/squash.go +++ b/cli/migrate/database/hasuradb/squash.go @@ -29,6 +29,7 @@ func (q CustomQuery) MergeEventTriggers(squashList *database.CustomList) error { next := q.Iterate() for item, ok := next(); ok; item, ok = next() { + var wasCreated bool g := item.(linq.Group) if g.Key == "" { continue @@ -42,14 +43,19 @@ func (q CustomQuery) MergeEventTriggers(squashList *database.CustomList) error { element := val.(*list.Element) switch obj := element.Value.(type) { case *createEventTriggerInput: - if obj.Replace { - for _, e := range prevElems { - squashList.Remove(e) - } - err := eventTriggerTransition.Trigger("delete_event_trigger", &evCfg, nil) - if err != nil { - return err + if obj.Replace != nil { + if *obj.Replace { + for _, e := range prevElems { + squashList.Remove(e) + } + err := eventTriggerTransition.Trigger("delete_event_trigger", &evCfg, nil) + if err != nil { + return err + } + obj.Replace = nil } + } else { + wasCreated = true } err := eventTriggerTransition.Trigger("create_event_trigger", &evCfg, nil) if err != nil { @@ -57,11 +63,18 @@ func (q CustomQuery) MergeEventTriggers(squashList *database.CustomList) error { } prevElems = append(prevElems, element) case *deleteEventTriggerInput: + if wasCreated { + // if this is true it means that a trigger was created + // which means their is no point in keeping the delete event trigger around + // + // otherwise it means that it was only updated so we have to keep + // the delete trigger migration + prevElems = append(prevElems, element) + } err := eventTriggerTransition.Trigger("delete_event_trigger", &evCfg, nil) if err != nil { return err } - prevElems = append(prevElems, element) // drop previous elements for _, e := range prevElems { squashList.Remove(e) diff --git a/cli/migrate/database/hasuradb/types.go b/cli/migrate/database/hasuradb/types.go index 08886b661ab..306d71e9aca 100644 --- a/cli/migrate/database/hasuradb/types.go +++ b/cli/migrate/database/hasuradb/types.go @@ -482,8 +482,9 @@ type createEventTriggerInput struct { WebhookFromEnv string `json:"webhook_from_env,omitempty" yaml:"webhook_from_env,omitempty"` Definition *createEventTriggerOperationInput `json:"definition,omitempty" yaml:"definition,omitempty"` Headers interface{} `json:"headers" yaml:"headers"` - Replace bool `json:"replace" yaml:"replace"` + Replace *bool `json:"replace,omitempty" yaml:"replace,omitempty"` RetryConf *createEventTriggerRetryConfInput `json:"retry_conf" yaml:"retry_conf"` + EnableManual *bool `json:"enable_manual,omitempty" yaml:"enable_manual,omitempty"` createEventTriggerOperationInput } @@ -513,11 +514,12 @@ func (c *createEventTriggerInput) MarshalJSON() ([]byte, error) { Webhook string `json:"webhook,omitempty" yaml:"webhook,omitempty"` WebhookFromEnv string `json:"webhook_from_env,omitempty" yaml:"webhook_from_env,omitempty"` Headers interface{} `json:"headers" yaml:"headers"` - Replace bool `json:"replace" yaml:"replace"` + Replace *bool `json:"replace,omitempty" yaml:"replace,omitempty"` RetryConf *createEventTriggerRetryConfInput `json:"retry_conf" yaml:"retry_conf"` Insert interface{} `json:"insert,omitempty" yaml:"insert,omitempty"` Update interface{} `json:"update,omitempty" yaml:"update,omitempty"` Delete interface{} `json:"delete,omitempty" yaml:"delete,omitempty"` + EnableManual *bool `json:"enable_manual,omitempty" yaml:"enable_manual,omitempty"` }{ Name: c.Name, Table: c.Table, @@ -529,6 +531,7 @@ func (c *createEventTriggerInput) MarshalJSON() ([]byte, error) { Insert: c.Insert, Update: c.Update, Delete: c.Delete, + EnableManual: c.EnableManual, }) }