From 69f8a9e58cd676182755daaabf4299cf621f2758 Mon Sep 17 00:00:00 2001 From: Shiroko Date: Tue, 22 Oct 2024 16:41:24 +0800 Subject: [PATCH 1/3] tests(workspaces): Add schema reset to `get_db_utils` as a workaround. KAG-5040 --- spec/internal/db.lua | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/spec/internal/db.lua b/spec/internal/db.lua index 8fe43da18d71..a6e458391615 100644 --- a/spec/internal/db.lua +++ b/spec/internal/db.lua @@ -214,8 +214,14 @@ local function truncate_tables(db, tables) end -local function bootstrap_database(db) +local function bootstrap_database(db, reset) local schema_state = assert(db:schema_state()) + + if reset then + assert(db:schema_reset()) + schema_state = assert(db:schema_state()) + end + if schema_state.needs_bootstrap then assert(db:schema_bootstrap()) end @@ -275,7 +281,7 @@ end -- route = { id = route1.id }, -- config = {}, -- } -local function get_db_utils(strategy, tables, plugins, vaults, skip_migrations) +local function get_db_utils(strategy, tables, plugins, vaults, skip_migrations, reset_schema) strategy = strategy or conf.database conf.database = strategy -- overwrite kong.configuration.database @@ -313,7 +319,7 @@ local function get_db_utils(strategy, tables, plugins, vaults, skip_migrations) assert(db:init_connector()) if not skip_migrations then - bootstrap_database(db) + bootstrap_database(db, reset_schema) end db:truncate("plugins") From 1bc8986f0778fe3c9cd2b9c3354785fb837e9e0f Mon Sep 17 00:00:00 2001 From: Shiroko Date: Tue, 29 Oct 2024 09:34:13 +0800 Subject: [PATCH 2/3] tests(db): apply schema reset for every bootstrap --- spec/internal/db.lua | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/spec/internal/db.lua b/spec/internal/db.lua index a6e458391615..57761d1f3bec 100644 --- a/spec/internal/db.lua +++ b/spec/internal/db.lua @@ -214,16 +214,14 @@ local function truncate_tables(db, tables) end -local function bootstrap_database(db, reset) +local function bootstrap_database(db) + -- Drop all schema and data + assert(db:schema_reset()) local schema_state = assert(db:schema_state()) - if reset then - assert(db:schema_reset()) - schema_state = assert(db:schema_state()) - end - if schema_state.needs_bootstrap then assert(db:schema_bootstrap()) + schema_state = assert(db:schema_state()) end if schema_state.new_migrations then @@ -281,7 +279,7 @@ end -- route = { id = route1.id }, -- config = {}, -- } -local function get_db_utils(strategy, tables, plugins, vaults, skip_migrations, reset_schema) +local function get_db_utils(strategy, tables, plugins, vaults, skip_migrations) strategy = strategy or conf.database conf.database = strategy -- overwrite kong.configuration.database @@ -319,7 +317,7 @@ local function get_db_utils(strategy, tables, plugins, vaults, skip_migrations, assert(db:init_connector()) if not skip_migrations then - bootstrap_database(db, reset_schema) + bootstrap_database(db) end db:truncate("plugins") From b9e6e55265b590fd1eb3679565a7f844d3f1c2ae Mon Sep 17 00:00:00 2001 From: Shiroko Date: Wed, 30 Oct 2024 09:47:02 +0800 Subject: [PATCH 3/3] tests(db): move the db:schema_reset to get_db_utils to allow some special use cases. --- spec/02-integration/03-db/08-declarative_spec.lua | 5 ----- spec/internal/db.lua | 4 ++-- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/spec/02-integration/03-db/08-declarative_spec.lua b/spec/02-integration/03-db/08-declarative_spec.lua index 9c6b80af2e1c..ffe8d74b4e9f 100644 --- a/spec/02-integration/03-db/08-declarative_spec.lua +++ b/spec/02-integration/03-db/08-declarative_spec.lua @@ -11,11 +11,6 @@ for _, strategy in helpers.each_strategy() do local _ _, db = helpers.get_db_utils(strategy) - -- This is a special case, where some DB states could be corrupted by DB truncation in `lazy_teardown()`. - -- We manually bootstrap the DB here to ensure the creation of a table is done correctly - db:schema_reset() - helpers.bootstrap_database(db) - _G.kong.db = db assert(helpers.start_kong({ database = strategy, diff --git a/spec/internal/db.lua b/spec/internal/db.lua index 57761d1f3bec..5659cdf72ef2 100644 --- a/spec/internal/db.lua +++ b/spec/internal/db.lua @@ -215,8 +215,6 @@ end local function bootstrap_database(db) - -- Drop all schema and data - assert(db:schema_reset()) local schema_state = assert(db:schema_state()) if schema_state.needs_bootstrap then @@ -317,6 +315,8 @@ local function get_db_utils(strategy, tables, plugins, vaults, skip_migrations) assert(db:init_connector()) if not skip_migrations then + -- Drop all schema and data + assert(db:schema_reset()) bootstrap_database(db) end