Skip to content

Commit

Permalink
feat: hide migrations and seeding commands
Browse files Browse the repository at this point in the history
Those commands should not be visible to lambda users, because seeding is
for development and migrations is currently handled for the user.

Seeding and migrations have been separated because they don't really fit
together. If for a new release, we choose to document and encourage the
use of the migrations command, seeding should stay hidden.
  • Loading branch information
thibaultcha committed Apr 9, 2015
1 parent 18f6f64 commit f2bbba3
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 91 deletions.
8 changes: 5 additions & 3 deletions bin/kong
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
#!/usr/bin/env lua

local cutils = require "kong.cli.utils"
local infos = cutils.get_infos()
local infos = cutils.get_kong_infos()
local commands = {
db = "kong.cli.db",
stop = "kong.cli.stop",
quit = "kong.cli.quit",
start = "kong.cli.start",
reload = "kong.cli.reload",
config = "kong.cli.config",
restart = "kong.cli.restart",
version = "kong.cli.version",
["--version"] = "kong.cli.version",
restart = "kong.cli.restart"
migrations = "kong.cli.migrations"
}

local help_message = string.format([[
Usage: kong <command>
where <command> is one of:
start, restart, reload, stop, quit, db, version
start, restart, reload, stop, quit, version
kong --help print this message
kong <command> --help print the help message of a command
Expand All @@ -38,4 +39,5 @@ elseif not commands[cmd] then
os.exit(1)
end

-- Load and execute desired command
require(commands[cmd])
1 change: 1 addition & 0 deletions kong-0.2.0-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ build = {
["kong.cli.reload"] = "src/cli/reload.lua",
["kong.cli.restart"] = "src/cli/restart.lua",
["kong.cli.version"] = "src/cli/version.lua",
["kong.cli.migrations"] = "src/cli/migrations.lua",

["kong.tools.utils"] = "src/tools/utils.lua",
["kong.tools.io"] = "src/tools/io.lua",
Expand Down
89 changes: 7 additions & 82 deletions src/cli/db.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
#!/usr/bin/env lua

local Faker = require "kong.tools.faker"
local Migrations = require "kong.tools.migrations"

local constants = require "kong.constants"
local cutils = require "kong.cli.utils"
local IO = require "kong.tools.io"
local lapp = require("lapp")
local args = lapp(string.format([[
Kong datastore management operations.
For development purposes only.
Seed the database with random data or drop it.
Usage: kong db <command> [options]
Commands:
<command> (string) where <command> is one of:
migrations, migrations:up, migrations:down, migrations:reset, seed, drop
seed, drop
Options:
-c,--config (default %s) path to configuration file
-r,--random (seed) flag to also insert random entities
-n,--number (default 1000) (seed) number of random entities to insert if --random
-r,--random flag to also insert random entities
-n,--number (default 1000) number of random entities to insert if --random
]], constants.CLI.GLOBAL_KONG_CONF))

-- $ kong db
Expand All @@ -29,83 +29,8 @@ end

local config_path = cutils.get_kong_config_path(args.config)
local _, dao_factory = IO.load_configuration_and_dao(config_path)
local migrations = Migrations(dao_factory, cutils.get_luarocks_install_dir())

if args.command == "migrations" then

local migrations, err = dao_factory:get_migrations()
if err then
cutils.logger:error_exit(err)
elseif migrations then
cutils.logger:log(string.format(
"Executed migrations for %s on keyspace: %s:\n%s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace),
table.concat(migrations, ", ")
))
else
cutils.logger:log(string.format(
"No migrations have been run yet for %s on keyspace: %s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace)
))
end

elseif args.command == "migrations:up" then

cutils.logger:log(string.format(
"Migrating %s keyspace: %s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace))
)

migrations:migrate(function(migration, err)
if err then
cutils.logger:error_exit(err)
elseif migration then
cutils.logger:success("Migrated up to: "..cutils.colors.yellow(migration.name))
else
cutils.logger:success("Schema already up to date")
end
end)

elseif args.command == "migrations:down" then

cutils.logger:log(string.format(
"Rolling back %s keyspace: %s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace)
))

migrations:rollback(function(migration, err)
if err then
cutils.logger:error_exit(err)
elseif migration then
cutils.logger:success("Rollbacked to: "..cutils.colors.yellow(migration.name))
else
cutils.logger:success("No migration to rollback")
end
end)

elseif args.command == "migrations:reset" then

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

migrations:reset(function(migration, err)
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")
end
end)

elseif args.command == "seed" then
if args.command == "seed" then

-- Drop if exists
local err = dao_factory:drop()
Expand Down
106 changes: 106 additions & 0 deletions src/cli/migrations.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env lua

local Migrations = require "kong.tools.migrations"
local constants = require "kong.constants"
local cutils = require "kong.cli.utils"
local IO = require "kong.tools.io"
local lapp = require("lapp")
local args = lapp(string.format([[
Kong datastore migrations.
Usage: kong migrations <command> [options]
Commands:
<command> (string) where <command> is one of:
list, up, down, reset
Options:
-c,--config (default %s) path to configuration file
]], constants.CLI.GLOBAL_KONG_CONF))

-- $ kong migrations
if args.command == "migrations" then
lapp.quit("Missing required <command>.")
end

local config_path = cutils.get_kong_config_path(args.config)
local _, dao_factory = IO.load_configuration_and_dao(config_path)
local migrations = Migrations(dao_factory, cutils.get_luarocks_install_dir())

if args.command == "list" then

local migrations, err = dao_factory:get_migrations()
if err then
cutils.logger:error_exit(err)
elseif migrations then
cutils.logger:log(string.format(
"Executed migrations for %s on keyspace %s:\n%s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace),
table.concat(migrations, ", ")
))
else
cutils.logger:log(string.format(
"No migrations have been run yet for %s on keyspace: %s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace)
))
end

elseif args.command == "up" then

cutils.logger:log(string.format(
"Migrating %s keyspace: %s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace))
)

migrations:migrate(function(migration, err)
if err then
cutils.logger:error_exit(err)
elseif migration then
cutils.logger:success("Migrated up to: "..cutils.colors.yellow(migration.name))
else
cutils.logger:success("Schema already up to date")
end
end)

elseif args.command == "down" then

cutils.logger:log(string.format(
"Rolling back %s keyspace: %s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace)
))

migrations:rollback(function(migration, err)
if err then
cutils.logger:error_exit(err)
elseif migration then
cutils.logger:success("Rollbacked to: "..cutils.colors.yellow(migration.name))
else
cutils.logger:success("No migration to rollback")
end
end)

elseif args.command == "reset" then

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

migrations:reset(function(migration, err)
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")
end
end)

else
lapp.quit("Invalid command: "..args.command)
end
10 changes: 5 additions & 5 deletions src/cli/utils/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ local logger = Logger()
--
-- Luarocks
--
local function get_infos()
local function get_kong_infos()
return { name = constants.NAME, version = constants.VERSION }
end

local function get_luarocks_dir()
local cfg = require "luarocks.cfg"
local search = require "luarocks.search"
local infos = get_infos()
local infos = get_kong_infos()

local tree_map = {}
local results = {}
Expand All @@ -83,13 +83,13 @@ end

local function get_luarocks_config_dir()
local repo = get_luarocks_dir()
local infos = get_infos()
local infos = get_kong_infos()
return lpath.conf_dir(infos.name:lower(), infos.version, repo)
end

local function get_luarocks_install_dir()
local repo = get_luarocks_dir()
local infos = get_infos()
local infos = get_kong_infos()
return lpath.install_dir(infos.name:lower(), infos.version, repo)
end

Expand Down Expand Up @@ -121,7 +121,7 @@ end
return {
colors = colors,
logger = logger,
get_infos = get_infos,
get_kong_infos = get_kong_infos,
get_kong_config_path = get_kong_config_path,
get_luarocks_install_dir = get_luarocks_install_dir
}
2 changes: 1 addition & 1 deletion src/cli/version.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env lua

local cutils = require "kong.cli.utils"
local infos = cutils.get_infos()
local infos = cutils.get_kong_infos()

cutils.logger:log(string.format("Kong version: %s", infos.version))

0 comments on commit f2bbba3

Please sign in to comment.