Skip to content

Commit

Permalink
feat(migrations) adds --force flag
Browse files Browse the repository at this point in the history
Adds a `--force` flag which allows for re-running the `up`
migrations in between release candidate releases. This is
a stopgap measure due to the absence of separate migration
files in the previous release candidates of Kong 1.0, which
should allow databases built with previous release candidates
to be upgraded to the latest one.
  • Loading branch information
hishamhm committed Dec 10, 2018
1 parent 7bbae2e commit ab441c3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
9 changes: 8 additions & 1 deletion kong/cmd/migrations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ Options:
-q,--quiet Suppress all output.
-f,--force Run migrations even if database reports
as already executed.
--db-timeout (default 60) Timeout, in seconds, for all database
operations (including schema consensus for
Cassandra).
Expand Down Expand Up @@ -131,11 +134,15 @@ local function execute(args)
elseif args.command == "up" then
migrations_utils.up(schema_state, db, {
ttl = args.lock_timeout,
force = args.force,
abort = true, -- exit the mutex if another node acquired it
})

elseif args.command == "finish" then
migrations_utils.finish(schema_state, db, args.lock_timeout)
migrations_utils.finish(schema_state, db, {
ttl = args.lock_timeout,
force = args.force,
})

else
error("unreachable")
Expand Down
29 changes: 21 additions & 8 deletions kong/cmd/utils/migrations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ local function up(schema_state, db, opts)

else
-- fresh install: must bootstrap (which will run migrations up)
error("can't run migrations: database needs bootstrapping; " ..
error("cannot run migrations: database needs bootstrapping; " ..
"run 'kong migrations bootstrap'")
end
end
Expand All @@ -128,8 +128,19 @@ local function up(schema_state, db, opts)
error("database has pending migrations; run 'kong migrations finish'")
end

if opts.force then
log.debug("forcing re-execution of these migrations:\n%s",
schema_state.executed_migrations)

assert(db:run_migrations(schema_state.executed_migrations, {
run_up = true,
}))
end

if not schema_state.new_migrations then
log("database is already up-to-date")
if not opts.force then
log("database is already up-to-date")
end
return
end

Expand All @@ -151,16 +162,18 @@ local function up(schema_state, db, opts)
end


local function finish(schema_state, db, ttl)
local function finish(schema_state, db, opts)
if schema_state.needs_bootstrap then
log("can't run migrations: database not bootstrapped")
log("cannot run migrations: database not bootstrapped")
return
end

local opts = {
ttl = ttl,
no_wait = true, -- exit the mutex if another node acquired it
}
if opts.force then
log("cannot use --force with 'finish'")
return
end

opts.no_wait = true -- exit the mutex if another node acquired it

local ok, err = db:cluster_mutex(MIGRATIONS_MUTEX_KEY, opts, function()
local schema_state = assert(db:schema_state())
Expand Down

0 comments on commit ab441c3

Please sign in to comment.