Skip to content
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

Trying to change keyspace before creating it (in case it was already … #3203

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions kong/dao/migrations/cassandra.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,32 @@ return {
return "invalid replication_strategy class"
end

-- Format final keyspace creation query
local keyspace_str = string.format([[
CREATE KEYSPACE IF NOT EXISTS "%s"
WITH REPLICATION = {'class': '%s'%s};
]], keyspace_name, strategy, strategy_properties)

local res, err = db:query(keyspace_str, nil, nil, nil, true)
if not res then
return err
end

-- Try to change keyspace, assuming the keyspace is already created
-- (keyspace creation right may not be granted to current user, and keyspace may
-- have been created outside of the Kong migration process, by a DB admin)
local ok, err = db:coordinator_change_keyspace(keyspace_name)

if not ok then
return err
-- In case of error, so let's try to create the keyspace (hoping the current
-- user is granted the keyspace creation right!)

-- Format final keyspace creation query
local keyspace_str = string.format([[
CREATE KEYSPACE IF NOT EXISTS "%s"
WITH REPLICATION = {'class': '%s'%s};
]], keyspace_name, strategy, strategy_properties)

local res, err = db:query(keyspace_str, nil, nil, nil, true)
if not res then
-- If keyspace creation failed, then raise the error
return err
end

-- Otherwise change keyspace to the newly created one
local ok, err = db:coordinator_change_keyspace(keyspace_name)
if not ok then
return err
end
end

local res, err = db:query [[
Expand Down