Skip to content

Commit

Permalink
Reverting back Postgres auto-creation of database
Browse files Browse the repository at this point in the history
  • Loading branch information
subnetmarco committed Apr 8, 2016
1 parent a57d0c3 commit 17676bc
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 38 deletions.
18 changes: 0 additions & 18 deletions kong/dao/migrations/postgres.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,6 @@ return {
{
name = "2015-01-12-175310_skeleton",
up = function(db, properties)
local database_name = properties.database
local user = properties.user

-- Format final database creation query
local database_str = string.format([[
DO $$
BEGIN
CREATE EXTENSION IF NOT EXISTS dblink;
IF NOT EXISTS (SELECT 1 FROM pg_database WHERE datname = '%s') THEN
PERFORM dblink_exec('dbname=' || current_database(), 'CREATE DATABASE %s%s');
END IF;
END$$;
]], database_name, database_name, user and " OWNER "..user or "")
local err = db:queries(database_str, true)
if err then
return err
end

return db:queries [[
CREATE TABLE IF NOT EXISTS schema_migrations(
id text PRIMARY KEY,
Expand Down
20 changes: 3 additions & 17 deletions kong/dao/postgres_db.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,10 @@ end

-- Querying

function PostgresDB:query(query, no_database)
function PostgresDB:query(query)
PostgresDB.super.query(self, query)

local conn_opts = self:_get_conn_options()
if no_database then
conn_opts.database = "postgres"
conn_opts.user = "postgres"
end
local pg = pgmoon.new(conn_opts)
local ok, err = pg:connect()
if not ok then
Expand Down Expand Up @@ -432,9 +428,9 @@ end

-- Migrations

function PostgresDB:queries(queries, no_database)
function PostgresDB:queries(queries)
if utils.strip(queries) ~= "" then
local err = select(2, self:query(queries, no_database))
local err = select(2, self:query(queries))
if err then
return err
end
Expand All @@ -450,16 +446,6 @@ function PostgresDB:truncate_table(table_name)
end

function PostgresDB:current_migrations()
-- Check if database exists
local rows, err = self:query(string.format([[
SELECT 1 FROM pg_database WHERE datname = '%s'
]], self.options.database), true)
if err then
return nil, err
elseif #rows == 0 then
return {}
end

-- Check if schema_migrations table exists
local rows, err = self:query "SELECT to_regclass('public.schema_migrations')"
if err then
Expand Down
2 changes: 1 addition & 1 deletion kong/tools/config_defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ return {
["port"] = {type = "number", default = 5432},
["user"] = {type = "string", default = "kong"},
["database"] = {type = "string", default = "kong"},
["password"] = {type = "string", default = "kong"}
["password"] = {type = "string", nullable = true}
}
},
["cassandra"] = {
Expand Down
10 changes: 8 additions & 2 deletions spec/integration/dao/02-migrations_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ helpers.for_each_dao(function(db_type, default_opts, TYPES)
local xfactory = Factory(db_type, invalid_opts)

local cur_migrations, err = xfactory:current_migrations()
assert.same({}, cur_migrations)
assert.falsy(err)
if db_type == TYPES.CASSANDRA then
assert.same({}, cur_migrations)
elseif db_type == TYPES.POSTGRES then
assert.truthy(err)
assert.falsy(cur_migrations)
assert.True(err.db)
assert.equal('FATAL: database "_inexistent_" does not exist', tostring(err))
end
end)
end)

Expand Down

0 comments on commit 17676bc

Please sign in to comment.