From f991e1528127208ea794fbf49f7e3056c52994b2 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 2 Oct 2017 14:03:07 +1100 Subject: [PATCH] fix: use strict mode when using mysql Ensure that values that are too long for a mysql column raise an error, not truncate --- lib/db.rb | 1 + lib/pact_broker/app.rb | 1 + lib/pact_broker/db.rb | 8 ++++++++ lib/pact_broker/pacts/merger.rb | 3 +++ 4 files changed, 13 insertions(+) diff --git a/lib/db.rb b/lib/db.rb index 0cd096cb8..f276a8eb7 100644 --- a/lib/db.rb +++ b/lib/db.rb @@ -30,6 +30,7 @@ def self.connect db_credentials con.extension(:connection_validator) con.pool.connection_validation_timeout = -1 #Check the connection on every request con.timezone = :utc + con.run("SET sql_mode='STRICT_TRANS_TABLES';") if db_credentials[:adapter].to_s =~ /mysql/ con end diff --git a/lib/pact_broker/app.rb b/lib/pact_broker/app.rb index 3dc70843a..f64ac557e 100644 --- a/lib/pact_broker/app.rb +++ b/lib/pact_broker/app.rb @@ -63,6 +63,7 @@ def configure_database_connection PactBroker::DB.connection = configuration.database_connection PactBroker::DB.connection.timezone = :utc PactBroker::DB.validate_connection_config if configuration.validate_database_connection_config + PactBroker::DB.set_mysql_strict_mode_if_mysql Sequel.datetime_class = DateTime Sequel.database_timezone = :utc # Store all dates in UTC, assume any date without a TZ is UTC Sequel.application_timezone = :local # Convert dates to localtime when retrieving from database diff --git a/lib/pact_broker/db.rb b/lib/pact_broker/db.rb index 37581ed00..a1aa36c50 100644 --- a/lib/pact_broker/db.rb +++ b/lib/pact_broker/db.rb @@ -24,5 +24,13 @@ def self.run_migrations database_connection def self.validate_connection_config PactBroker::DB::ValidateEncoding.(connection) end + + def self.set_mysql_strict_mode_if_mysql + connection.run("SET sql_mode='STRICT_TRANS_TABLES';") if mysql? + end + + def self.mysql? + connection.adapter_scheme =~ /mysql/ + end end end diff --git a/lib/pact_broker/pacts/merger.rb b/lib/pact_broker/pacts/merger.rb index 1c255a8bc..31e0d45bf 100644 --- a/lib/pact_broker/pacts/merger.rb +++ b/lib/pact_broker/pacts/merger.rb @@ -19,6 +19,9 @@ def conflict? original_json, additional_json # Accepts two hashes representing pacts, outputs a merged hash # Does not make any guarantees about order of interactions + # TODO: should not modify original! + # TODO: is not checking response for equality! + # TODO: should have separate tests! def merge_pacts original_json, additional_json original, additional = [original_json, additional_json].map{|str| JSON.parse(str, PACT_PARSING_OPTIONS) }