Skip to content

Commit

Permalink
feat(pacts for verification): optimise queries for determining which …
Browse files Browse the repository at this point in the history
…provider version tags are pending
  • Loading branch information
bethesque committed Jan 30, 2020
1 parent 9b3162a commit b4e1461
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
6 changes: 6 additions & 0 deletions lib/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@ module DB
# pool, as noted in the documentation for the extension.
#
def self.connect db_credentials
# Keep this conifiguration in sync with lib/pact_broker/app.rb#configure_database_connection
Sequel.datetime_class = DateTime
# logger = Logger.new($stdout)
con = Sequel.connect(db_credentials.merge(:logger => logger, :pool_class => Sequel::ThreadedConnectionPool, :encoding => 'utf8'))
con.extension(:connection_validator)
con.extension(:pagination)
con.extend_datasets do
def any?
!empty?
end
end
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/
Expand Down
7 changes: 7 additions & 0 deletions lib/pact_broker/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,18 @@ def load_configuration_from_database
end

def configure_database_connection
# Keep this configuration in sync with lib/db.rb
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
PactBroker::DB.connection.extension(:pagination)
PactBroker::DB.extend_datasets do
def any?
!empty?
end
end

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
22 changes: 16 additions & 6 deletions lib/pact_broker/pacts/pact_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,31 @@ def latest_consumer_version_number

def select_provider_tags_with_successful_verifications(tags)
tags.select do | tag |
verifications_join = {
Sequel[:verifications][:pact_version_id] => Sequel[:pact_versions][:id],
Sequel[:verifications][:success] => true
}
tags_join = {
Sequel[:tags][:version_id] => Sequel[:versions][:id],
Sequel[:tags][:name] => tag
}
PactVersion.where(Sequel[:pact_versions][:id] => id)
.join(:verifications, Sequel[:verifications][:pact_version_id] => Sequel[:pact_versions][:id])
.join(:verifications, verifications_join)
.join(:versions, Sequel[:versions][:id] => Sequel[:verifications][:provider_version_id])
.join(:tags, Sequel[:tags][:version_id] => Sequel[:versions][:id])
.where(Sequel[:tags][:name] => tag)
.where(Sequel[:verifications][:success] => true)
.join(:tags, tags_join)
.any?
end
end

def verified_successfully_by_any_provider_version?
verifications_join = {
Sequel[:verifications][:pact_version_id] => Sequel[:pact_versions][:id],
Sequel[:verifications][:pact_version_id] => id,
Sequel[:verifications][:success] => true
}
PactVersion.where(Sequel[:pact_versions][:id] => id)
.join(:verifications, Sequel[:verifications][:pact_version_id] => Sequel[:pact_versions][:id])
.join(:verifications, verifications_join)
.join(:versions, Sequel[:versions][:id] => Sequel[:verifications][:provider_version_id])
.where(Sequel[:verifications][:success] => true)
.any?
end

Expand Down

0 comments on commit b4e1461

Please sign in to comment.