Skip to content

Commit

Permalink
Use Sequel builder methods for complex compact index queries
Browse files Browse the repository at this point in the history
  • Loading branch information
segiddins committed Jul 21, 2024
1 parent 02861e7 commit 0c3e854
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
37 changes: 14 additions & 23 deletions lib/gemstash/compact_index_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,17 @@ def build_result
private

def requirements_and_dependencies
group_by_columns = "number, platform, sha256, info_checksum, required_ruby_version, required_rubygems_version, versions.created_at"

dep_req_agg = "string_agg(dependencies.requirements, '@' ORDER BY dependencies.rubygem_name, dependencies.id) as dep_req_agg"

dep_name_agg = "string_agg(dependencies.rubygem_name, ',' ORDER BY dependencies.rubygem_name) AS dep_name_agg"

DB::Rubygem.db[<<~SQL.squish, @name].
SELECT #{group_by_columns}, #{dep_req_agg}, #{dep_name_agg}
FROM rubygems
LEFT JOIN versions ON versions.rubygem_id = rubygems.id
LEFT JOIN dependencies ON dependencies.version_id = versions.id
WHERE rubygems.name = ? AND versions.indexed = true
GROUP BY #{group_by_columns}
ORDER BY versions.created_at, number, platform, dep_name_agg
SQL
DB::Rubygem.association_left_join(versions: :dependencies).
where(name: @name).
where { versions[:indexed] }.
order { [versions[:created_at], versions[:number], versions[:platform], dep_name_agg] }.
select_group do
[versions[:number], versions[:platform], versions[:sha256], versions[:info_checksum], versions[:required_ruby_version], versions[:required_rubygems_version], versions[:created_at]]
end. # rubocop:disable Style/MultilineBlockChain
select_more do
[string_agg(dependencies[:requirements], "@").order(dependencies[:rubygem_name], dependencies[:id]).as(:dep_req_agg),
string_agg(dependencies[:rubygem_name], ",").order(dependencies[:rubygem_name]).as(:dep_name_agg)]
end. # rubocop:disable Style/MultilineBlockChain
map do |row|
reqs = row[:dep_req_agg]&.split("@")
dep_names = row[:dep_name_agg]&.split(",")
Expand Down Expand Up @@ -235,14 +231,9 @@ def fetch_resource
end

def build_result
names = DB::Rubygem.db[<<~SQL.squish].map {|row| row[:name] }
SELECT name
FROM rubygems
INNER JOIN versions ON versions.rubygem_id = rubygems.id
WHERE versions.indexed = true
GROUP BY name
ORDER BY name
SQL
names = DB::Rubygem.association_join(:versions).
where { versions[:indexed] }.
order(:name).group(:name).select_map(:name)
@result = CompactIndex.names(names).encode("UTF-8")
end

Expand Down
2 changes: 2 additions & 0 deletions lib/gemstash/db/rubygem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module Gemstash
module DB
# Sequel model for rubygems table.
class Rubygem < Sequel::Model
one_to_many :versions

def self.find_or_insert(spec)
record = self[name: spec.name]
return record.id if record
Expand Down
1 change: 1 addition & 0 deletions lib/gemstash/db/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module DB
# Sequel model for versions table.
class Version < Sequel::Model
many_to_one :rubygem
one_to_many :dependencies

def deindex
info = Gemstash::CompactIndexBuilder::Info.new(nil, rubygem.name).tap(&:build_result).result
Expand Down

0 comments on commit 0c3e854

Please sign in to comment.