Skip to content

Commit

Permalink
fix(migrations): split redis migrations into up and teardown
Browse files Browse the repository at this point in the history
"UP" phase should only contain non-destructive operations.
"TEARDOWN" (or "FINISH") phase should be used to change/delete data.

KAG-4419
  • Loading branch information
nowNick authored and locao committed May 6, 2024
1 parent 8470056 commit 49aa233
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: "**ACME**: Fixed migration of redis configuration."
type: bugfix
scope: Plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: "**Response-RateLimiting**: Fixed migration of redis configuration."
type: bugfix
scope: Plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: "**Rate-Limiting**: Fixed migration of redis configuration."
type: bugfix
scope: Plugin
51 changes: 28 additions & 23 deletions kong/plugins/acme/migrations/003_350_to_360.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,16 @@ return {
BEGIN
UPDATE plugins
SET config =
config
#- '{storage_config,redis}'
|| jsonb_build_object(
'storage_config',
(config -> 'storage_config') - 'redis'
jsonb_set(
config,
'{storage_config,redis}',
config #> '{storage_config, redis}'
|| jsonb_build_object(
'redis',
jsonb_build_object(
'host', config #> '{storage_config, redis, host}',
'port', config #> '{storage_config, redis, port}',
'password', config #> '{storage_config, redis, auth}',
'username', config #> '{storage_config, redis, username}',
'ssl', config #> '{storage_config, redis, ssl}',
'ssl_verify', config #> '{storage_config, redis, ssl_verify}',
'server_name', config #> '{storage_config, redis, ssl_server_name}',
'timeout', config #> '{storage_config, redis, timeout}',
'database', config #> '{storage_config, redis, database}'
) || jsonb_build_object(
'extra_options',
jsonb_build_object(
'scan_count', config #> '{storage_config, redis, scan_count}',
'namespace', config #> '{storage_config, redis, namespace}'
)
'password', config #> '{storage_config, redis, auth}',
'server_name', config #> '{storage_config, redis, ssl_server_name}',
'extra_options', jsonb_build_object(
'scan_count', config #> '{storage_config, redis, scan_count}',
'namespace', config #> '{storage_config, redis, namespace}'
)
)
)
Expand All @@ -37,5 +23,24 @@ return {
-- Do nothing, accept existing state
END$$;
]],
teardown = function(connector, _)
local sql = [[
DO $$
BEGIN
UPDATE plugins
SET config =
config
#- '{storage_config,redis,auth}'
#- '{storage_config,redis,ssl_server_name}'
#- '{storage_config,redis,scan_count}'
#- '{storage_config,redis,namespace}'
WHERE name = 'acme';
EXCEPTION WHEN UNDEFINED_COLUMN OR UNDEFINED_TABLE THEN
-- Do nothing, accept existing state
END$$;
]]
assert(connector:query(sql))
return true
end,
},
}
34 changes: 25 additions & 9 deletions kong/plugins/rate-limiting/migrations/006_350_to_360.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ return {
UPDATE plugins
SET config =
config::jsonb
- 'redis_host'
- 'redis_port'
- 'redis_password'
- 'redis_username'
- 'redis_ssl'
- 'redis_ssl_verify'
- 'redis_server_name'
- 'redis_timeout'
- 'redis_database'
|| jsonb_build_object(
'redis',
jsonb_build_object(
Expand All @@ -34,5 +25,30 @@ return {
-- Do nothing, accept existing state
END$$;
]],
teardown = function(connector, _)
local sql = [[
DO $$
BEGIN
UPDATE plugins
SET config =
config::jsonb
- 'redis_host'
- 'redis_port'
- 'redis_password'
- 'redis_username'
- 'redis_ssl'
- 'redis_ssl_verify'
- 'redis_server_name'
- 'redis_timeout'
- 'redis_database'
WHERE name = 'rate-limiting';
EXCEPTION WHEN UNDEFINED_COLUMN OR UNDEFINED_TABLE THEN
-- Do nothing, accept existing state
END$$;
]]
assert(connector:query(sql))

return true
end,
},
}
34 changes: 25 additions & 9 deletions kong/plugins/response-ratelimiting/migrations/001_350_to_360.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ return {
UPDATE plugins
SET config =
config::jsonb
- 'redis_host'
- 'redis_port'
- 'redis_password'
- 'redis_username'
- 'redis_ssl'
- 'redis_ssl_verify'
- 'redis_server_name'
- 'redis_timeout'
- 'redis_database'
|| jsonb_build_object(
'redis',
jsonb_build_object(
Expand All @@ -34,5 +25,30 @@ return {
-- Do nothing, accept existing state
END$$;
]],
teardown = function(connector, _)
local sql = [[
DO $$
BEGIN
UPDATE plugins
SET config =
config::jsonb
- 'redis_host'
- 'redis_port'
- 'redis_password'
- 'redis_username'
- 'redis_ssl'
- 'redis_ssl_verify'
- 'redis_server_name'
- 'redis_timeout'
- 'redis_database'
WHERE name = 'response-ratelimiting';
EXCEPTION WHEN UNDEFINED_COLUMN OR UNDEFINED_TABLE THEN
-- Do nothing, accept existing state
END$$;
]]
assert(connector:query(sql))

return true
end,
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ if uh.database_type() == 'postgres' then
admin_client:close()
end)

uh.new_after_up("has updated acme redis configuration", function ()
uh.new_after_finish("has updated acme redis configuration", function ()
local admin_client = assert(uh.admin_client())
local res = assert(admin_client:send {
method = "GET",
Expand Down

1 comment on commit 49aa233

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:49aa2332b219826410c46cf207c9bb31ce25785d
Artifacts available https://github.com/Kong/kong/actions/runs/8973724428

Please sign in to comment.