Skip to content

Commit f991e15

Browse files
committed
fix: use strict mode when using mysql
Ensure that values that are too long for a mysql column raise an error, not truncate
1 parent f01053f commit f991e15

File tree

4 files changed

+13
-0
lines changed

4 files changed

+13
-0
lines changed

lib/db.rb

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def self.connect db_credentials
3030
con.extension(:connection_validator)
3131
con.pool.connection_validation_timeout = -1 #Check the connection on every request
3232
con.timezone = :utc
33+
con.run("SET sql_mode='STRICT_TRANS_TABLES';") if db_credentials[:adapter].to_s =~ /mysql/
3334
con
3435
end
3536

lib/pact_broker/app.rb

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def configure_database_connection
6363
PactBroker::DB.connection = configuration.database_connection
6464
PactBroker::DB.connection.timezone = :utc
6565
PactBroker::DB.validate_connection_config if configuration.validate_database_connection_config
66+
PactBroker::DB.set_mysql_strict_mode_if_mysql
6667
Sequel.datetime_class = DateTime
6768
Sequel.database_timezone = :utc # Store all dates in UTC, assume any date without a TZ is UTC
6869
Sequel.application_timezone = :local # Convert dates to localtime when retrieving from database

lib/pact_broker/db.rb

+8
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,13 @@ def self.run_migrations database_connection
2424
def self.validate_connection_config
2525
PactBroker::DB::ValidateEncoding.(connection)
2626
end
27+
28+
def self.set_mysql_strict_mode_if_mysql
29+
connection.run("SET sql_mode='STRICT_TRANS_TABLES';") if mysql?
30+
end
31+
32+
def self.mysql?
33+
connection.adapter_scheme =~ /mysql/
34+
end
2735
end
2836
end

lib/pact_broker/pacts/merger.rb

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ def conflict? original_json, additional_json
1919

2020
# Accepts two hashes representing pacts, outputs a merged hash
2121
# Does not make any guarantees about order of interactions
22+
# TODO: should not modify original!
23+
# TODO: is not checking response for equality!
24+
# TODO: should have separate tests!
2225
def merge_pacts original_json, additional_json
2326
original, additional = [original_json, additional_json].map{|str| JSON.parse(str, PACT_PARSING_OPTIONS) }
2427

0 commit comments

Comments
 (0)