Skip to content

Commit

Permalink
fix: use strict mode when using mysql
Browse files Browse the repository at this point in the history
Ensure that values that are too long for a mysql column raise an error, not truncate
  • Loading branch information
bethesque committed Oct 2, 2017
1 parent f01053f commit f991e15
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions lib/pact_broker/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/pact_broker/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions lib/pact_broker/pacts/merger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down

0 comments on commit f991e15

Please sign in to comment.