From ddb8853fedbb2fd13d83d0e4091a45339f98056a Mon Sep 17 00:00:00 2001 From: chronolaw Date: Mon, 28 Oct 2024 11:48:50 +0800 Subject: [PATCH] change pk to json --- kong/clustering/services/sync/hooks.lua | 2 +- kong/clustering/services/sync/rpc.lua | 2 +- kong/clustering/services/sync/strategies/postgres.lua | 6 +++--- kong/db/migrations/core/024_370_to_380.lua | 2 +- .../05-migration/db/migrations/core/024_370_to_380_spec.lua | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/kong/clustering/services/sync/hooks.lua b/kong/clustering/services/sync/hooks.lua index 685dfe8776d3..e28ac40919d3 100644 --- a/kong/clustering/services/sync/hooks.lua +++ b/kong/clustering/services/sync/hooks.lua @@ -79,7 +79,7 @@ function _M:entity_delta_writer(row, name, options, ws_id, is_delete) local deltas = { { type = name, - pk = row.id, + pk = { id = row.id }, ws_id = ws_id, row = is_delete and ngx_null or row, }, diff --git a/kong/clustering/services/sync/rpc.lua b/kong/clustering/services/sync/rpc.lua index ddfa74f4279f..625b394d3505 100644 --- a/kong/clustering/services/sync/rpc.lua +++ b/kong/clustering/services/sync/rpc.lua @@ -274,7 +274,7 @@ local function do_sync() else -- delete the entity - local old_entity, err = kong.db[delta_type]:select({ id = delta.pk, }) -- TODO: composite key + local old_entity, err = kong.db[delta_type]:select(delta.pk) -- composite key if err then return nil, err end diff --git a/kong/clustering/services/sync/strategies/postgres.lua b/kong/clustering/services/sync/strategies/postgres.lua index bbe8654eae8a..616a4e38cc78 100644 --- a/kong/clustering/services/sync/strategies/postgres.lua +++ b/kong/clustering/services/sync/strategies/postgres.lua @@ -73,15 +73,15 @@ local NEW_VERSION_QUERY = [[ -- deltas: { --- { type = "service", "pk" = "d78eb00f-8702-4d6a-bfd9-e005f904ae3e", "ws_id" = "73478cf6-964f-412d-b1c4-8ac88d9e85e9", row = "JSON", } --- { type = "route", "pk" = "0a5bac5c-b795-4981-95d2-919ba3390b7e", "ws_id" = "73478cf6-964f-412d-b1c4-8ac88d9e85e9", row = "JSON", } +-- { type = "service", "pk" = { id = "d78eb00f..." }, "ws_id" = "73478cf6...", row = "JSON", } +-- { type = "route", "pk" = { id = "0a5bac5c..." }, "ws_id" = "73478cf6...", row = "JSON", } -- } function _M:insert_delta(deltas) local buf = buffer.new() for _, d in ipairs(deltas) do buf:putf("(new_version, %s, %s, %s, %s)", self.connector:escape_literal(d.type), - self.connector:escape_literal(d.pk), + self.connector:escape_literal(cjson_encode(d.pk)), self.connector:escape_literal(d.ws_id or kong.default_workspace), self.connector:escape_literal(cjson_encode(d.row))) end diff --git a/kong/db/migrations/core/024_370_to_380.lua b/kong/db/migrations/core/024_370_to_380.lua index eb145db78339..afb94a09f7a3 100644 --- a/kong/db/migrations/core/024_370_to_380.lua +++ b/kong/db/migrations/core/024_370_to_380.lua @@ -9,7 +9,7 @@ return { CREATE TABLE IF NOT EXISTS clustering_sync_delta ( "version" INT NOT NULL, "type" TEXT NOT NULL, - "pk" UUID NOT NULL, + "pk" JSON NOT NULL, "ws_id" UUID NOT NULL, "row" JSON, FOREIGN KEY (version) REFERENCES clustering_sync_version(version) ON DELETE CASCADE diff --git a/spec/05-migration/db/migrations/core/024_370_to_380_spec.lua b/spec/05-migration/db/migrations/core/024_370_to_380_spec.lua index f17c9d48879b..f4a6145d16be 100644 --- a/spec/05-migration/db/migrations/core/024_370_to_380_spec.lua +++ b/spec/05-migration/db/migrations/core/024_370_to_380_spec.lua @@ -10,7 +10,7 @@ describe("database migration", function() assert.database_has_relation("clustering_sync_delta") assert.table_has_column("clustering_sync_delta", "version", "integer") assert.table_has_column("clustering_sync_delta", "type", "text") - assert.table_has_column("clustering_sync_delta", "pk", "uuid") + assert.table_has_column("clustering_sync_delta", "pk", "json") assert.table_has_column("clustering_sync_delta", "ws_id", "uuid") assert.table_has_column("clustering_sync_delta", "row", "json") end)