Skip to content

Commit

Permalink
fix(db) do not assume that the Postgres schema is public (#4198)
Browse files Browse the repository at this point in the history
This makes the Postgres strategy use the default schema and not hardcode `public`. It also supports a configurable `schema` internally, which will be made into a `pg_schema` configuration option in the next minor release of Kong.
  • Loading branch information
bungle authored and hishamhm committed Jan 14, 2019
1 parent b676ae1 commit e89d056
Showing 1 changed file with 25 additions and 8 deletions.
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

0 comments on commit e89d056

Please sign in to comment.