commit f3b438ab533526bb6bb0d2b76ee940d363a74039
parent 386f1e4126f0c9abe8009cc3e585eebb75773033
Author: Christian Grothoff <christian@grothoff.org>
Date: Wed, 5 Aug 2026 18:07:37 +0200
fix 500 if frontend tried to add nx category for a product
Diffstat:
6 files changed, 73 insertions(+), 12 deletions(-)
diff --git a/src/backend/taler-merchant-httpd_patch-private-products-PRODUCT_ID.c b/src/backend/taler-merchant-httpd_patch-private-products-PRODUCT_ID.c
@@ -296,7 +296,8 @@ TMH_private_patch_products_ID (
json_array_foreach (categories, idx, val)
{
- if (! json_is_integer (val))
+ if ( (! json_is_integer (val)) ||
+ (0 >= json_integer_value (val)) )
{
GNUNET_break_op (0);
ret = TALER_MHD_reply_with_error (connection,
diff --git a/src/backend/taler-merchant-httpd_post-private-products.c b/src/backend/taler-merchant-httpd_post-private-products.c
@@ -280,7 +280,8 @@ TMH_private_post_products (const struct TMH_RequestHandler *rh,
json_array_foreach (categories, idx, val)
{
- if (! json_is_integer (val))
+ if ( (! json_is_integer (val)) ||
+ (0 >= json_integer_value (val)) )
{
GNUNET_break_op (0);
ret = TALER_MHD_reply_with_error (connection,
diff --git a/src/backenddb/insert_product.sql b/src/backenddb/insert_product.sql
@@ -203,6 +203,15 @@ FOR i IN 1..COALESCE(array_length(ina_categories,1),0)
LOOP
ini_cat=ina_categories[i];
+ PERFORM
+ FROM merchant_categories
+ WHERE category_serial=ini_cat;
+ IF NOT FOUND
+ THEN
+ out_no_cat=i;
+ RETURN;
+ END IF;
+
INSERT INTO merchant_product_categories
(product_serial
,category_serial)
@@ -211,11 +220,6 @@ LOOP
,ini_cat)
ON CONFLICT DO NOTHING;
- IF NOT FOUND
- THEN
- out_no_cat=i;
- RETURN;
- END IF;
END LOOP;
-- Success!
diff --git a/src/backenddb/sql-schema/merchant-0043.sql b/src/backenddb/sql-schema/merchant-0043.sql
@@ -0,0 +1,50 @@
+--
+-- This file is part of TALER
+-- Copyright (C) 2026 Taler Systems SA
+--
+-- TALER is free software; you can redistribute it and/or modify it under the
+-- terms of the GNU General Public License as published by the Free Software
+-- Foundation; either version 3, or (at your option) any later version.
+--
+-- TALER is distributed in the hope that it will be useful, but WITHOUT ANY
+-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+-- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+--
+-- You should have received a copy of the GNU General Public License along with
+-- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
+
+-- @file merchant-0043.sql
+-- @brief add missing primary key constraint on merchant_product_categories
+
+BEGIN;
+
+SELECT _v.register_patch('merchant-0043', NULL, NULL);
+
+SET search_path TO merchant;
+
+CREATE PROCEDURE merchant.merchant_0043_init(s TEXT)
+ LANGUAGE plpgsql
+ AS $OUTER$
+BEGIN
+ -- Add primary constraint, delete constraint-violating values first
+
+ EXECUTE format('DELETE FROM %I.merchant_product_categories f' ||
+ ' USING %I.merchant_product_categories d' ||
+ ' WHERE f.product_serial = d.product_serial' ||
+ ' AND f.category_serial = d.category_serial' ||
+ ' AND f.ctid > d.ctid;', s, s);
+ EXECUTE format('ALTER TABLE %I.merchant_product_categories' ||
+ ' ADD PRIMARY KEY(product_serial, category_serial);', s);
+END
+$OUTER$;
+
+INSERT INTO merchant.instance_fixups
+ (migration_name
+ ,version)
+ VALUES
+ ('merchant_0043_init'
+ ,43);
+-- Apply new fix-up to existing instances
+CALL merchant.fixup_instance_schema (43::INT8);
+
+COMMIT;
diff --git a/src/backenddb/sql-schema/meson.build b/src/backenddb/sql-schema/meson.build
@@ -120,6 +120,7 @@ generated_sql = [
['merchant-0040.sql'],
['merchant-0041.sql'],
['merchant-0042.sql'],
+ ['merchant-0043.sql'],
]
foreach g : generated_sql
diff --git a/src/backenddb/update_product.sql b/src/backenddb/update_product.sql
@@ -130,6 +130,15 @@ FOR i IN 1..COALESCE(array_length(ina_categories,1),0)
LOOP
ini_cat=ina_categories[i];
+ PERFORM
+ FROM merchant_categories
+ WHERE category_serial=ini_cat;
+ IF NOT FOUND
+ THEN
+ out_no_cat=i;
+ RETURN;
+ END IF;
+
INSERT INTO merchant_product_categories
(product_serial
,category_serial)
@@ -138,11 +147,6 @@ LOOP
,ini_cat)
ON CONFLICT DO NOTHING;
- IF NOT FOUND
- THEN
- out_no_cat=i;
- RETURN;
- END IF;
END LOOP;
UPDATE merchant_inventory SET