Skip to content

Commit

Permalink
feat(migrations) split plugins migrations
Browse files Browse the repository at this point in the history
Proper architecture and CLI update for plugin-specific migrations. This
is the first step in separating the plugins out of the core repo as
planed for 0.5.0. It is related to #93.

This is implemented by using the same table as before
(`schema_migrations`) to keep track of the executed migrations, except
that each plugin and the core itself all have their own row.

For simplifications, migrations (plugins or core) now live in a single
file. The `database` folder disappeared for core and the migration lives
in the DAO. Plugins have migrations in a `migration` folder, named after
the type of the database (`cassandra.lua` currently).

For now, only the keyauth plugin has its own migrations.

The `kong migrations up|down` commands slightly changed. It takes a `-t`
parameter to specify which migrations to run (core or a plugin name) and
is running all migrations by default. `kong migrations list` lists all
executed migration for the core and all plugins.

- Needs tests (integration)
- Old unit tests were removed
- `database` was removed
- `kong migrations` slightly changed
  • Loading branch information
thibaultcha committed Aug 11, 2015
1 parent 06ef332 commit 9ce8000
Show file tree
Hide file tree
Showing 11 changed files with 347 additions and 639 deletions.
65 changes: 0 additions & 65 deletions database/migrations/cassandra/2015-01-12-175310_init_schema.lua

This file was deleted.

47 changes: 0 additions & 47 deletions database/migrations/cassandra/2015-04-24-154530_plugins.lua

This file was deleted.

27 changes: 0 additions & 27 deletions database/migrations/cassandra/2015-05-22-235608_0.3.0.lua

This file was deleted.

63 changes: 0 additions & 63 deletions database/migrations/cassandra/2015-06-09-170921_0.4.0.lua

This file was deleted.

4 changes: 3 additions & 1 deletion kong-0.4.1-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ build = {
["kong.reports.init_worker"] = "kong/reports/init_worker.lua",
["kong.reports.log"] = "kong/reports/log.lua",

["kong.dao.cassandra.schema.migrations"] = "kong/dao/cassandra/schema/migrations.lua",
["kong.dao.error"] = "kong/dao/error.lua",
["kong.dao.schemas_validation"] = "kong/dao/schemas_validation.lua",
["kong.dao.schemas.apis"] = "kong/dao/schemas/apis.lua",
Expand All @@ -99,6 +100,7 @@ build = {
["kong.plugins.basicauth.api"] = "kong/plugins/basicauth/api.lua",
["kong.plugins.basicauth.daos"] = "kong/plugins/basicauth/daos.lua",

["kong.plugins.keyauth.migrations.cassandra"] = "kong/plugins/keyauth/migrations/cassandra.lua",
["kong.plugins.keyauth.handler"] = "kong/plugins/keyauth/handler.lua",
["kong.plugins.keyauth.access"] = "kong/plugins/keyauth/access.lua",
["kong.plugins.keyauth.schema"] = "kong/plugins/keyauth/schema.lua",
Expand Down Expand Up @@ -181,5 +183,5 @@ build = {
conf = { "kong.yml" },
bin = { "bin/kong" }
},
copy_directories = { "database/migrations/", "ssl" }
copy_directories = { "ssl" }
}
105 changes: 73 additions & 32 deletions kong/cli/migrations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
local Migrations = require "kong.tools.migrations"
local constants = require "kong.constants"
local cutils = require "kong.cli.utils"
local utils = require "kong.tools.utils"
local input = require "kong.cli.utils.input"
local IO = require "kong.tools.io"
local lapp = require "lapp"
Expand All @@ -16,7 +17,9 @@ Commands:
list, up, down, reset
Options:
-c,--config (default %s) path to configuration file
-c,--config (default %s) path to configuration file.
-t,--type (default all) when 'up' or 'down', specify 'core' or 'plugin_name' to only run
specific migrations.
]], constants.CLI.GLOBAL_KONG_CONF))

-- $ kong migrations
Expand All @@ -25,8 +28,16 @@ if args.command == "migrations" then
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())
local configuration, dao_factory = IO.load_configuration_and_dao(config_path)
local migrations = Migrations(dao_factory)

local kind = args.type
if kind ~= "all" and kind ~= "core" then
-- Assuming we are trying to run migrations for a plugin
if not utils.table_contains(configuration.plugins_available, kind) then
cutils.logger:error_exit("No \""..kind.."\" plugin enabled in the configuration.")
end
end

if args.command == "list" then

Expand All @@ -35,11 +46,17 @@ if args.command == "list" then
cutils.logger:error_exit(err)
elseif migrations then
cutils.logger:info(string.format(
"Executed migrations for %s on keyspace %s:\n%s",
cutils.colors.yellow(dao_factory.type),
"Executed migrations for keyspace %s (%s):",
cutils.colors.yellow(dao_factory._properties.keyspace),
table.concat(migrations, ", ")
dao_factory.type
))

for _, row in ipairs(migrations) do
cutils.logger:info(string.format("%s: %s",
cutils.colors.yellow(row.id),
table.concat(row.migrations, ", ")
))
end
else
cutils.logger:info(string.format(
"No migrations have been run yet for %s on keyspace: %s",
Expand All @@ -50,48 +67,72 @@ if args.command == "list" then

elseif args.command == "up" then

cutils.logger:info(string.format(
"Migrating %s keyspace \"%s\"",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace))
)
local function migrate(identifier)
cutils.logger:info(string.format(
"Migrating %s on keyspace \"%s\" (%s)",
cutils.colors.yellow(identifier),
cutils.colors.yellow(dao_factory._properties.keyspace),
dao_factory.type
))

migrations:migrate(function(migration, err)
local err = migrations:migrate(identifier, function(identifier, migration)
if migration then
cutils.logger:info(string.format(
"%s migrated up to: %s",
identifier,
cutils.colors.yellow(migration.name)
))
end
end)
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)
end

if kind == "all" then
migrate("core")
for _, plugin_name in ipairs(configuration.plugins_available) do
local has_migrations = utils.load_module_if_exists("kong.plugins."..plugin_name..".migrations."..dao_factory.type)
if has_migrations then
migrate(plugin_name)
end
end
else
migrate(kind)
end

cutils.logger:success("Schema up to date")

elseif args.command == "down" then

if kind == "all" then
cutils.logger:error_exit("You must specify 'core' or a plugin name for this command.")
end

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

migrations:rollback(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("No migration to rollback")
end
end)
local rollbacked, err = migrations:rollback(kind)
if err then
cutils.logger:error_exit(err)
elseif rollbacked then
cutils.logger:success("\""..kind.."\" rollbacked: "..cutils.colors.yellow(rollbacked.name))
else
cutils.logger:success("No migration to rollback")
end

elseif args.command == "reset" then

local keyspace = dao_factory._properties.keyspace

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

if input.confirm("Are you sure? You will lose all of your data, this operation is irreversible.") then
Expand Down
Loading

0 comments on commit 9ce8000

Please sign in to comment.