Skip to content

Commit

Permalink
Pass sort keys to Arel as a list rather than an Array
Browse files Browse the repository at this point in the history
  • Loading branch information
dnrce committed May 21, 2017
1 parent f2e26e6 commit 9b28173
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
12 changes: 6 additions & 6 deletions lib/yaml_db/serialization_helper.rb
Expand Up @@ -183,12 +183,12 @@ def self.table_column_names(table)
def self.each_table_page(table, records_per_page=1000)
total_count = table_record_count(table)
pages = (total_count.to_f / records_per_page).ceil - 1
key = sort_key(table)
keys = sort_keys(table)
boolean_columns = Utils.boolean_columns(table)
quoted_table_name = Utils.quote_table(table)

(0..pages).to_a.each do |page|
query = Arel::Table.new(table).order(key).skip(records_per_page*page).take(records_per_page).project(Arel.sql('*'))
query = Arel::Table.new(table).order(*keys).skip(records_per_page*page).take(records_per_page).project(Arel.sql('*'))
records = ActiveRecord::Base.connection.select_all(query.to_sql)
records = Utils.convert_booleans(records, boolean_columns)
yield records
Expand All @@ -199,15 +199,15 @@ def self.table_record_count(table)
ActiveRecord::Base.connection.select_one("SELECT COUNT(*) FROM #{Utils.quote_table(table)}").values.first.to_i
end

# Just return the first column as sort key unless the table looks like a
# standard HABTM join table, in which case add the second "id column"
def self.sort_key(table)
# Return the first column as sort key unless the table looks like a
# standard has_and_belongs_to_many join table, in which case add the second "ID column"
def self.sort_keys(table)
first_column, second_column = table_column_names(table)

if [first_column, second_column].all? { |name| name =~ /_id$/ }
[Utils.quote_column(first_column), Utils.quote_column(second_column)]
else
Utils.quote_column(first_column)
[Utils.quote_column(first_column)]
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions spec/yaml_db/serialization_helper_dump_spec.rb
Expand Up @@ -26,7 +26,7 @@ module SerializationHelper

describe ".each_table_page" do
before do
allow(Dump).to receive(:sort_key)
allow(Dump).to receive(:sort_keys)
end

it "returns all records from the database and returns them when there is only 1 page" do
Expand Down Expand Up @@ -72,13 +72,13 @@ module SerializationHelper
end
end

describe ".sort_key" do
describe ".sort_keys" do
before do
allow(Utils).to receive(:quote_column) { |column| column }
end

it "returns the first column as sort key" do
expect(Dump.sort_key('mytable')).to eq('a')
expect(Dump.sort_keys('mytable')).to eq(['a'])
end

it "returns the combined ids as sort key if the table looks like a HABTM" do
Expand All @@ -87,12 +87,12 @@ module SerializationHelper
double('b_id', :name => 'b_id', :type => :string)
])

expect(Dump.sort_key('mytable')).to eq(['a_id', 'b_id'])
expect(Dump.sort_keys('mytable')).to eq(['a_id', 'b_id'])
end

it "quotes the column name" do
allow(Utils).to receive(:quote_column).with('a').and_return('`a`')
expect(Dump.sort_key('mytable')).to eq('`a`')
expect(Dump.sort_keys('mytable')).to eq(['`a`'])
end
end
end
Expand Down

0 comments on commit 9b28173

Please sign in to comment.