-
Notifications
You must be signed in to change notification settings - Fork 823
don't exec create keyspace if it exists #2032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
don't exec create keyspace if it exists #2032
Conversation
Signed-off-by: Wing924 <weihe924stephen@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving, based on the https://github.com/gocql/gocql/blob/95d072f1b5bbc604ce10f41ce1c914578592ed00/cassandra_test.go#L85 which shows that ErrNoConnectionsStarted
is expected if keyspace doesn't exist. It's weirdly named error though.
Please update CHANGELOG.md as well. |
And thank you for your contribution! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution! I left a couple of small comments. Please remember to add an entry to the CHANGELOG.md
and mention the PR number too (see the format of other entries there).
@VineethReddy02 May you test this PR against your test cluster based on Cassandra, please?
@@ -70,7 +66,19 @@ func (cfg *Config) session() (*gocql.Session, error) { | |||
cluster.ConnectTimeout = cfg.ConnectTimeout | |||
cfg.setClusterConfig(cluster) | |||
|
|||
return cluster.CreateSession() | |||
session, err := cluster.CreateSession() | |||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would simplify this into a guard:
if err == nil {
return session, nil
}
if err := cfg.createKeyspace(); err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
session, err = cluster.CreateSession() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then you can simplify this into return cluster.CreateSession()
@pracucci I will test this PR and will update here. |
|
@VineethReddy02 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please take a look at my comments. I think the current changes introduce a regression. Moreover, please do some manual tests and report the result (ie. Cortex logs); minimum tests to do:
- Keyspace already created and no
CREATE
privilege - Keyspace NOT created and no
CREATE
privilege - Keyspace NOT created and
CREATE
privilege
@@ -83,6 +79,14 @@ func (cfg *Config) session() (*gocql.Session, error) { | |||
} | |||
cfg.setClusterConfig(cluster) | |||
|
|||
session, err := cluster.CreateSession() | |||
if err == nil || err != gocql.ErrNoConnectionsStarted { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a comment to explain what ErrNoConnectionsStarted
is.
Manual Test ResultSummaryIt works as expected. LogsKeyspace NOT created and no CREATE privilege
Keyspace already created and no CREATE privilege
Keyspace NOT created and CREATE privilege
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🎉 Thanks for the tests results!
test failed but not from part of my changes. |
What this PR does:
Don't execute
CREATE KEYSPACE IF NOT EXISTS
if the given keyspace has already existed.The
CREATE KEYSPACE IF NOT EXISTS
statement requiresCREATE
permission on<all keyspaces>
even if it exists. It's not common that normal users have that privilege.ref. how to check keyspace exist: https://github.com/gocql/gocql/blob/master/cassandra_test.go#L85-L97
Which issue(s) this PR fixes:
Checklist
CHANGELOG.md
updated - the order of entries should be[CHANGE]
,[FEATURE]
,[ENHANCEMENT]
,[BUGFIX]