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)