From 4e4af64582a0ac659762a337b6cc80cd2bc7de9b Mon Sep 17 00:00:00 2001 From: johha Date: Mon, 27 Oct 2025 12:16:24 +0100 Subject: [PATCH] Remove not needed `validates_unique` for route mappings Table `route_mappings` has a unique constraint for columns `app_guid`, `route_guid`, `process_type` and `app_port`. Therefore we can rely on the DB to catch uniqueness errors and remove the sequel `validates_unique` in the model. This reduces the number of DB queries as the following query is now skipped: ```SQL SELECT 1 AS \"one\" FROM \"route_mappings\" WHERE ((\"app_guid\" = ) AND (\"route_guid\" = ) AND (\"process_type\" = 'web') AND (\"app_port\" = )) LIMIT 1" ``` --- app/models/runtime/route_mapping_model.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/models/runtime/route_mapping_model.rb b/app/models/runtime/route_mapping_model.rb index 4a33139ab7c..384454b44e9 100644 --- a/app/models/runtime/route_mapping_model.rb +++ b/app/models/runtime/route_mapping_model.rb @@ -34,7 +34,6 @@ def protocol_with_defaults def validate validates_presence [:app_port] - validates_unique %i[app_guid route_guid process_type app_port] validate_weight end @@ -60,6 +59,15 @@ def has_app_port_specified? app_port != ProcessModel::NO_APP_PORT_SPECIFIED end + def around_save + yield + rescue Sequel::UniqueConstraintViolation => e + raise e unless e.message.include?('route_mappings_app_guid_route_guid_process_type_app_port_key') + + errors.add(%i[app_guid route_guid process_type app_port], :unique) + raise validation_failed_error + end + private def logger