Skip to content

Commit

Permalink
Scripts improvements + refactor for lua_cliargs
Browse files Browse the repository at this point in the history
- Unified scripts in db.lua
- Friendly error reporting
- Now using cli_args
- Silence flag, on for dev, off for travis
  • Loading branch information
thibaultcha committed Feb 24, 2015
1 parent 23ae818 commit 17c100a
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 190 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ install:
- sudo make install
- make dev

script: "make test-all"
script: "make test-all SILENT_FLAG="
21 changes: 11 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export DIR ?= $(KONG_HOME)/config.dev
export KONG_CONF ?= $(DIR)/kong.yaml
export NGINX_CONF ?= $(DIR)/nginx.conf
export DEV_LUA_LIB ?= lua_package_path \"$(KONG_HOME)/src/?.lua\;\;\"\;
export SILENT_FLAG ?=
# Tests variables
TESTS_DIR ?= $(KONG_HOME)/config.tests
TESTS_KONG_CONF ?= $(TESTS_DIR)/kong.yaml
Expand Down Expand Up @@ -36,28 +37,28 @@ clean:
@rm -rf $(TESTS_DIR)

migrate:
@scripts/migrate migrate --conf=$(KONG_CONF)
@scripts/db.lua $(SILENT_FLAG) migrate $(KONG_CONF)

reset:
@scripts/migrate reset --conf=$(KONG_CONF)
@scripts/db.lua $(SILENT_FLAG) reset $(KONG_CONF)

seed:
@scripts/seed seed --conf=$(KONG_CONF)
@scripts/db.lua $(SILENT_FLAG) seed $(KONG_CONF)

drop:
@scripts/seed drop --conf=$(KONG_CONF)
@scripts/db.lua $(SILENT_FLAG) drop $(KONG_CONF)

test:
@busted spec/unit

run-integration-tests:
@bin/kong -c $(TESTS_KONG_CONF) migrate > /dev/null
@bin/kong -c $(TESTS_KONG_CONF) -n $(TESTS_NGINX_CONF) start > /dev/null
@$(MAKE) migrate KONG_CONF=$(TESTS_KONG_CONF) SILENT_FLAG=-s
@bin/kong -c $(TESTS_KONG_CONF) -n $(TESTS_NGINX_CONF) start
@while ! [ `ps aux | grep nginx | grep -c -v grep` -gt 0 ]; do sleep 1; done # Wait until nginx starts
@$(MAKE) seed KONG_CONF=$(TESTS_KONG_CONF) > /dev/null
@busted $(FOLDER) || (bin/kong stop > /dev/null; make drop KONG_CONF=$(TESTS_KONG_CONF) > /dev/null; exit 1)
@bin/kong stop > /dev/null
@$(MAKE) reset KONG_CONF=$(TESTS_KONG_CONF) > /dev/null
@$(MAKE) seed KONG_CONF=$(TESTS_KONG_CONF) SILENT_FLAG=-s
@busted $(FOLDER) || (bin/kong stop; make drop KONG_CONF=$(TESTS_KONG_CONF) SILENT_FLAG=-s; exit 1)
@bin/kong stop
@$(MAKE) reset KONG_CONF=$(TESTS_KONG_CONF) SILENT_FLAG=-s

test-web:
@$(MAKE) run-integration-tests FOLDER=spec/web
Expand Down
2 changes: 1 addition & 1 deletion bin/kong
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function start {
}

function migrate {
$KONG_HOME/scripts/migrate migrate --conf=$KONG_CONF
$KONG_HOME/scripts/db.lua migrate $KONG_CONF
if [ $? -eq 0 ]; then
exit 0
else
Expand Down
1 change: 1 addition & 0 deletions kong-0.0.1beta-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies = {
"luasocket ~> 2.0.2-5",

"busted ~> 2.0.rc6-0",
"lua_cliargs ~> 2.3-3",
"luafilesystem ~> 1.6.2"
}
build = {
Expand Down
91 changes: 91 additions & 0 deletions scripts/db.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env lua

local cli = require "cliargs"
local utils = require "kong.tools.utils"
local Migrations = require "kong.tools.migrations"

cli:set_name("db.lua")
cli:add_argument("COMMAND", "<create|migrate|rollback|reset|seed|drop>")
cli:add_option("-n, --name=NAME", "If <create>, sets a name to the migration", "new_migration")
cli:add_flag("-r, --random", "If seeding, also seed random entities (1000 for each collection by default)")
cli:add_flag("-s, --silent", "No output")
cli:optarg("CONFIGURATION", "configuration path", "config.dev/kong.yaml")

local args = cli:parse(arg)
if not args then
os.exit(1)
end

local logger = utils.logger:new(args.silent)
local configuration, dao = utils.load_configuration_and_dao(args.CONFIGURATION)

if args.COMMAND == "create" then

Migrations.create(configuration, args.name, function(interface, file_path, file_name)
os.execute("mkdir -p "..file_path)

local file = file_path.."/"..file_name..".lua"
utils.write_to_file(file, interface)
logger:success("New migration: "..file)
end)

elseif args.COMMAND == "migrate" then

logger:log("Migrating "..utils.yellow(dao.type))

dao:migrate(function(migration, err)
if err then
logger:error(err)
else
logger:success("Migrated up to: "..utils.yellow(migration.name))
end
end)

elseif args.COMMAND == "rollback" then

logger:log("Rolling back "..utils.yellow(dao.type))

dao:rollback(function(migration, err)
if err then
logger:error(err)
else
logger:success("Rollbacked to: "..utils.yellow(migration.name))
end
end)

elseif args.COMMAND == "reset" then

logger:log("Resetting "..utils.yellow(dao.type))

dao:reset(function(migration, err)
if err then
logger:error(err)
else
logger:success("Rollbacked: "..utils.yellow(migration.name))
end
end)

elseif args.COMMAND == "seed" then

-- Drop if exists
local err = dao:drop()
if err then
logger:error(err)
end

local err = dao:prepare()
if err then
logger:error(err)
end

dao:seed(args.random)
logger:success("Populated")

elseif args.COMMAND == "drop" then

dao:drop()
logger:success("Dropped")

else
print("Invalid command: "..args.COMMAND)
end
62 changes: 0 additions & 62 deletions scripts/migrate

This file was deleted.

42 changes: 0 additions & 42 deletions scripts/seed

This file was deleted.

13 changes: 8 additions & 5 deletions spec/unit/dao/cassandra_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ end
describe("Cassandra DAO #dao #cassandra", function()

setup(function()
dao_factory:migrate()
local ok, err = dao_factory:migrate()
assert.falsy(err)

dao_factory:prepare()
dao_factory:seed()

Expand All @@ -42,8 +44,9 @@ describe("Cassandra DAO #dao #cassandra", function()
end)

teardown(function()
dao_factory:reset()
session:close()
local ok, err = dao_factory:reset()
assert.falsy(err)
end)

describe("Factory", function()
Expand All @@ -55,9 +58,9 @@ describe("Cassandra DAO #dao #cassandra", function()
keyspace = configuration.cassandra.keyspace
})

assert.has_error(function()
new_factory:prepare()
end, "connection refused")
local err = new_factory:prepare()
assert.truthy(err)
assert.are.same("connection refused", err)
end)

end)
Expand Down
25 changes: 15 additions & 10 deletions src/kong/dao/cassandra/factory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ end
--

function CassandraFactory:migrate(callback)
self._migrations:migrate(callback)
return self._migrations:migrate(callback)
end

function CassandraFactory:rollback(callback)
self._migrations:rollback(callback)
return self._migrations:rollback(callback)
end

function CassandraFactory:reset(callback)
self._migrations:reset(callback)
return self._migrations:reset(callback)
end

--
Expand All @@ -57,7 +57,7 @@ end

function CassandraFactory:drop()
self.faker:clear()
self:execute [[
return self:execute [[
TRUNCATE apis;
TRUNCATE metrics;
TRUNCATE plugins;
Expand Down Expand Up @@ -87,21 +87,25 @@ local function prepare(collection, queries, statements)
q = string.format(q, "")
local kong_stmt, err = collection:prepare_kong_statement(q, query.params)
if err then
error(err)
return err
end
statements[stmt_name] = kong_stmt
end
end
end

-- Prepare all statements of collections
-- @return error if any
function CassandraFactory:prepare()
for _, collection in ipairs({ self.apis,
self.metrics,
self.plugins,
self.accounts,
self.applications }) do
prepare(collection)
local err = prepare(collection)
if err then
return err
end
end
end

Expand All @@ -110,30 +114,31 @@ end
--
-- @param {string} queries Semicolon separated string of queries
-- @param {boolean} no_keyspace Won't set the keyspace if true
-- @return {string} error if any
function CassandraFactory:execute(queries, no_keyspace)
local session = cassandra.new()
session:set_timeout(self._properties.timeout)

local connected, err = session:connect(self._properties.hosts, self._properties.port)
if not connected then
error(err)
return err
end

if no_keyspace == nil then
local ok, err = session:set_keyspace(self._properties.keyspace)
if not ok then
error(err)
return err
end
end

-- Cassandra client only support BATCH on DML statements.
-- Cassandra only supports BATCH on DML statements.
-- We must split commands to execute them individually for migrations and such
queries = stringy.split(queries, ";")
for _,query in ipairs(queries) do
if stringy.strip(query) ~= "" then
local result, err = session:execute(query)
if err then
error("Cassandra execution error: "..err)
return err
end
end
end
Expand Down
Loading

0 comments on commit 17c100a

Please sign in to comment.