Skip to content

Commit

Permalink
Merge pull request #365 from Mashape/fix/migrations
Browse files Browse the repository at this point in the history
[fix/migrations] handle missing schema_migrations
  • Loading branch information
thibaultcha committed Jun 26, 2015
2 parents d2ad295 + 61d9a20 commit 20490d5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 27 deletions.
1 change: 1 addition & 0 deletions kong-0.4.0-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ build = {

["kong.cli.utils"] = "kong/cli/utils/utils.lua",
["kong.cli.utils.signal"] = "kong/cli/utils/signal.lua",
["kong.cli.utils.input"] = "kong/cli/utils/input.lua",
["kong.cli.db"] = "kong/cli/db.lua",
["kong.cli.config"] = "kong/cli/config.lua",
["kong.cli.quit"] = "kong/cli/quit.lua",
Expand Down
25 changes: 13 additions & 12 deletions kong/cli/migrations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
local Migrations = require "kong.tools.migrations"
local constants = require "kong.constants"
local cutils = require "kong.cli.utils"
local input = require "kong.cli.utils.input"
local IO = require "kong.tools.io"
local lapp = require("lapp")
local lapp = require "lapp"
local args = lapp(string.format([[
Kong datastore migrations.
Expand Down Expand Up @@ -50,7 +51,7 @@ if args.command == "list" then
elseif args.command == "up" then

cutils.logger:info(string.format(
"Migrating %s keyspace: %s",
"Migrating %s keyspace \"%s\"",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace))
)
Expand All @@ -68,7 +69,7 @@ elseif args.command == "up" then
elseif args.command == "down" then

cutils.logger:info(string.format(
"Rolling back %s keyspace: %s",
"Rollbacking %s keyspace \"%s\"",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace)
))
Expand All @@ -85,22 +86,22 @@ elseif args.command == "down" then

elseif args.command == "reset" then

local keyspace = dao_factory._properties.keyspace

cutils.logger:info(string.format(
"Reseting %s keyspace: %s",
"Resetting %s keyspace \"%s\"",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace))
)
cutils.colors.yellow(keyspace)
))

migrations:reset(function(migration, err)
if input.confirm("Are you sure? You will lose all of your data, this operation is irreversible.") then
local res, err = dao_factory.migrations:drop_keyspace(keyspace)
if err then
cutils.logger:error_exit(err)
elseif migration then
cutils.logger:success("Rollbacked: "..cutils.colors.yellow(migration.name))
else
cutils.logger:success("Schema reseted")
cutils.logger:success("Keyspace successfully reset")
end
end)

end
else
lapp.quit("Invalid command: "..args.command)
end
24 changes: 24 additions & 0 deletions kong/cli/utils/input.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
local _M = {}

local ANSWERS = {
y = true,
Y = true,
yes = true,
YES = true,
n = false,
N = false,
no = false,
NO = false
}

function _M.confirm(question)
local answer
repeat
io.write(question.." [Y/n] ")
answer = ANSWERS[io.read("*l")]
until answer ~= nil

return answer
end

return _M
12 changes: 11 additions & 1 deletion kong/dao/cassandra/migrations.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local cassandra = require "cassandra"
local stringy = require "stringy"
local BaseDao = require "kong.dao.cassandra.base_dao"

local Migrations = BaseDao:extend()
Expand Down Expand Up @@ -48,7 +49,9 @@ function Migrations:get_migrations()
end

rows, err = Migrations.super._execute(self, self.queries.get_migrations)
if err then
if err and stringy.find(err.message, "unconfigured columnfamily schema_migrations") ~= nil then
return nil, "Missing mandatory column family \"schema_migrations\" in configured keyspace. Please consider running \"kong migrations reset\" to fix this."
elseif err then
return nil, err
elseif rows and #rows > 0 then
return rows[1].migrations
Expand All @@ -63,6 +66,13 @@ function Migrations:delete_migration(migration_name)
{ cassandra.list({ migration_name }) })
end

-- Drop the entire keyspace
-- @param `keyspace` Name of the keyspace to drop
-- @return query result
function Migrations:drop_keyspace(keyspace)
return Migrations.super._execute(self, string.format("DROP keyspace \"%s\"", keyspace))
end

function Migrations:drop()
-- never drop this
end
Expand Down
14 changes: 0 additions & 14 deletions kong/tools/migrations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,4 @@ function Migrations:rollback(callback)
callback(migration_to_rollback)
end

-- Execute all migrations DOWN
-- @param {function} callback A function to execute on each migration (ie: for logging)
function Migrations:reset(callback)
local done = false
while not done do
self:rollback(function(migration, err)
if not migration and not err then
done = true
end
callback(migration, err)
end)
end
end

return Migrations

0 comments on commit 20490d5

Please sign in to comment.