diff --git a/CHANGELOG.md b/CHANGELOG.md index d03c1533150..d88855de1c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - [#6065](https://github.com/influxdata/influxdb/pull/6065): Wait for a process termination on influxdb restart @simnv - [#5252](https://github.com/influxdata/influxdb/issues/5252): Release tarballs contain specific attributes on '.' - [#5554](https://github.com/influxdata/influxdb/issues/5554): Can't run in alpine linux +- [#6094](https://github.com/influxdata/influxdb/issues/6094): Ensure CREATE RETENTION POLICY and CREATE CONTINUOUS QUERY are idempotent in the correct way. ## v0.11.0 [2016-03-22] diff --git a/services/meta/client_test.go b/services/meta/client_test.go index 4b1f5d20bad..bae2fb85c0c 100644 --- a/services/meta/client_test.go +++ b/services/meta/client_test.go @@ -550,9 +550,18 @@ func TestMetaClient_ContinuousQueries(t *testing.T) { t.Fatal(err) } - // Recreate an existing CQ - if err := c.CreateContinuousQuery("db0", "cq0", `SELECT max(value) INTO foo_max FROM foo GROUP BY time(10m)`); err == nil || err.Error() != `continuous query already exists` { - t.Fatalf("unexpected error: %s", err) + // Recreating an existing CQ with the exact same query should not + // return an error. + if err := c.CreateContinuousQuery("db0", "cq0", `SELECT count(value) INTO foo_count FROM foo GROUP BY time(10m)`); err != nil { + t.Fatalf("got error %q, but didn't expect one", err) + } + + // Recreating an existing CQ with a different query should return + // an error. + if err := c.CreateContinuousQuery("db0", "cq0", `SELECT min(value) INTO foo_max FROM foo GROUP BY time(20m)`); err == nil { + t.Fatal("didn't get and error, but expected one") + } else if got, exp := err, meta.ErrContinuousQueryExists; got.Error() != exp.Error() { + t.Fatalf("got %v, expected %v", got, exp) } // Create a few more CQ's diff --git a/services/meta/data.go b/services/meta/data.go index eb5ee91f0b3..4505e5ebf4d 100644 --- a/services/meta/data.go +++ b/services/meta/data.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "sort" + "strings" "sync" "time" @@ -417,8 +418,14 @@ func (data *Data) CreateContinuousQuery(database, name, query string) error { } // Ensure the name doesn't already exist. - for i := range di.ContinuousQueries { - if di.ContinuousQueries[i].Name == name { + for _, cq := range di.ContinuousQueries { + if cq.Name == name { + // If the query string is the same, we'll silently return, + // otherwise we'll assume the user might be trying to + // overwrite an existing CQ with a different query. + if strings.ToLower(cq.Query) == strings.ToLower(query) { + return nil + } return ErrContinuousQueryExists } }