Skip to content

Commit b6d5357

Browse files
authored
Remove not needed validates_unique for route mappings (#4620)
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\" = <app_guid>) AND (\"route_guid\" = <route_guid>) AND (\"process_type\" = 'web') AND (\"app_port\" = <port>)) LIMIT 1" ```
1 parent d7ae589 commit b6d5357

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

app/models/runtime/route_mapping_model.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def protocol_with_defaults
3434

3535
def validate
3636
validates_presence [:app_port]
37-
validates_unique %i[app_guid route_guid process_type app_port]
3837

3938
validate_weight
4039
end
@@ -60,6 +59,15 @@ def has_app_port_specified?
6059
app_port != ProcessModel::NO_APP_PORT_SPECIFIED
6160
end
6261

62+
def around_save
63+
yield
64+
rescue Sequel::UniqueConstraintViolation => e
65+
raise e unless e.message.include?('route_mappings_app_guid_route_guid_process_type_app_port_key')
66+
67+
errors.add(%i[app_guid route_guid process_type app_port], :unique)
68+
raise validation_failed_error
69+
end
70+
6371
private
6472

6573
def logger

0 commit comments

Comments
 (0)