in kong/kong/db/strategies/cassandra/connector.lua [877:972]
function CassandraConnector:schema_bootstrap(kong_config, default_locks_ttl)
local cql_replication
local cass_repl_strategy = kong_config.cassandra_repl_strategy
if cass_repl_strategy == "SimpleStrategy" then
cql_replication = string.format([[
{'class': 'SimpleStrategy', 'replication_factor': %d}
]], kong_config.cassandra_repl_factor)
elseif cass_repl_strategy == "NetworkTopologyStrategy" then
local dcs = {}
for _, dc_conf in ipairs(kong_config.cassandra_data_centers) do
local dc_name, dc_repl = string.match(dc_conf, "([^:]+):(%d+)")
if dc_name and dc_repl then
table.insert(dcs, string.format("'%s': %s", dc_name, dc_repl))
end
end
if #dcs < 1 then
return nil, "invalid cassandra_data_centers configuration"
end
cql_replication = string.format([[
{'class': 'NetworkTopologyStrategy', %s}
]], table.concat(dcs, ", "))
else
error("unknown replication_strategy: " .. tostring(cass_repl_strategy))
end
local conn = self:get_stored_connection()
if not conn then
error("no connection")
end
local keyspace = kong_config.cassandra_keyspace
local ok = conn:change_keyspace(keyspace)
if not ok then
log.debug("creating '%s' keyspace if not existing...", keyspace)
local res, err = conn:execute(string.format([[
CREATE KEYSPACE IF NOT EXISTS %q
WITH REPLICATION = %s
]], keyspace, cql_replication))
if not res then
return nil, err
end
log.debug("successfully created '%s' keyspace", keyspace)
end
local ok, err = conn:change_keyspace(keyspace)
if not ok then
return nil, err
end
log.debug("creating 'schema_meta' table if not existing...")
local res, err = conn:execute([[
CREATE TABLE IF NOT EXISTS schema_meta(
key text,
subsystem text,
last_executed text,
executed set<text>,
pending set<text>,
PRIMARY KEY (key, subsystem)
)
]])
if not res then
return nil, err
end
log.debug("successfully created 'schema_meta' table")
ok, err = self:setup_locks(default_locks_ttl, true)
if not ok then
return nil, err
end
ok, err = self:wait_for_schema_consensus()
if not ok then
return nil, err
end
return true
end