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

fix(db) backport postgres schema fix from next to master (only the fix) #4198

Merged
merged 1 commit into from
Jan 14, 2019
Merged
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
33 changes: 25 additions & 8 deletions kong/db/strategies/postgres/connector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ local WARN = ngx.WARN
local SQL_INFORMATION_SCHEMA_TABLES = [[
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public';
WHERE table_schema = CURRENT_SCHEMA;
]]
local PROTECTED_TABLES = {
schema_migrations = true,
Expand Down Expand Up @@ -148,7 +148,19 @@ local function connect(config)
end

if connection.sock:getreusedtimes() == 0 then
ok, err = connection:query("SET TIME ZONE 'UTC';");
if config.schema == "" then
local res = connection:query("SELECT CURRENT_SCHEMA AS schema")
if res and res[1] and res[1].schema and res[1].schema ~= null then
config.schema = res[1].schema
else
config.schema = "public"
end
end

ok, err = connection:query(concat {
"SET SCHEMA ", connection:escape_literal(config.schema), ";\n",
"SET TIME ZONE ", connection:escape_literal("UTC"), ";",
})
if not ok then
setkeepalive(connection)
return nil, err
Expand Down Expand Up @@ -441,12 +453,14 @@ end


function _mt:reset()
local schema = self:escape_identifier(self.config.schema)
local user = self:escape_identifier(self.config.user)

local ok, err = self:query(concat {
"BEGIN;\n",
" DROP SCHEMA IF EXISTS public CASCADE;\n",
" CREATE SCHEMA IF NOT EXISTS public AUTHORIZATION ", user, ";\n",
" GRANT ALL ON SCHEMA public TO ", user, ";\n",
" DROP SCHEMA IF EXISTS ", schema ," CASCADE;\n",
" CREATE SCHEMA IF NOT EXISTS ", schema, " AUTHORIZATION ", user, ";\n",
" GRANT ALL ON SCHEMA ", schema ," TO ", user, ";\n",
"COMMIT;",
})

Expand Down Expand Up @@ -677,12 +691,14 @@ function _mt:schema_reset()
error("no connection")
end

local schema = self:escape_identifier(self.config.schema)
local user = self:escape_identifier(self.config.user)

local ok, err = self:query(concat {
"BEGIN;\n",
" DROP SCHEMA IF EXISTS public CASCADE;\n",
" CREATE SCHEMA IF NOT EXISTS public AUTHORIZATION ", user, ";\n",
" GRANT ALL ON SCHEMA public TO ", user, ";\n",
" DROP SCHEMA IF EXISTS ", schema, " CASCADE;\n",
" CREATE SCHEMA IF NOT EXISTS ", schema, " AUTHORIZATION ", user, ";\n",
" GRANT ALL ON SCHEMA ", schema ," TO ", user, ";\n",
"COMMIT;",
})

Expand Down Expand Up @@ -995,6 +1011,7 @@ function _M.new(kong_config)
user = kong_config.pg_user,
password = kong_config.pg_password,
database = kong_config.pg_database,
schema = "",
ssl = kong_config.pg_ssl,
ssl_verify = kong_config.pg_ssl_verify,
cafile = kong_config.lua_ssl_trusted_certificate,
Expand Down