From f2164f98fca98487f3d21a505a41f45e845266a0 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Thu, 7 Sep 2017 09:57:16 +0200 Subject: [PATCH] fix(migrations) stop depending on the order as recorded Migrations are recorded in the datastore and then the order in which they appear there is relevant for determining if they should run. Once something goes out-of-sync on every start/restart it will keep running migrations over and over again. --- kong/dao/factory.lua | 10 +++++--- .../03-dao/02-migrations_spec.lua | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/kong/dao/factory.lua b/kong/dao/factory.lua index 1cce7b3fdc3..852d8ce2d44 100644 --- a/kong/dao/factory.lua +++ b/kong/dao/factory.lua @@ -251,10 +251,14 @@ end local function migrate(self, identifier, migrations_modules, cur_migrations, on_migrate, on_success) local migrations = migrations_modules[identifier] - local recorded = cur_migrations[identifier] or {} + local recorded = {} + for _, name in ipairs(cur_migrations[identifier] or {}) do + recorded[name] = true + end + local to_run = {} - for i, mig in ipairs(migrations) do - if mig.name ~= recorded[i] then + for _, mig in ipairs(migrations) do + if not recorded[mig.name] then to_run[#to_run + 1] = mig end end diff --git a/spec/02-integration/03-dao/02-migrations_spec.lua b/spec/02-integration/03-dao/02-migrations_spec.lua index 2e5325a5e9c..1a878b7e9ae 100644 --- a/spec/02-integration/03-dao/02-migrations_spec.lua +++ b/spec/02-integration/03-dao/02-migrations_spec.lua @@ -98,6 +98,29 @@ helpers.for_each_dao(function(kong_config) assert.falsy(err) assert.True(ok) + assert.spy(on_migration).was_not_called() + assert.spy(on_success).was_not_called() + end) + it("should not depend on order of the migration entries", function() + local old_fetcher = factory.current_migrations + finally(function() + factory.current_migrations = old_fetcher + end) + + factory.current_migrations = function(...) + local mig = old_fetcher(...) + local core = mig.core + core[#core], core[#core-1] = core[#core-1], core[#core] + return mig + end + + local on_migration = spy.new(function() end) + local on_success = spy.new(function() end) + + local ok, err = factory:run_migrations(on_migration, on_success) + assert.falsy(err) + assert.True(ok) + assert.spy(on_migration).was_not_called() assert.spy(on_success).was_not_called() end)