From e4e8fe30a567305aec5d78ed5f7f5d235c9b73be Mon Sep 17 00:00:00 2001 From: Ariel Rzezak Date: Wed, 11 Mar 2026 09:07:27 -0300 Subject: [PATCH 1/6] Fix CLI to use passed argv instead of global ARGV --- lib/taql/cli.rb | 3 ++- test/test_cli.rb | 8 ++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/taql/cli.rb b/lib/taql/cli.rb index 1831fdd..eeb193d 100644 --- a/lib/taql/cli.rb +++ b/lib/taql/cli.rb @@ -5,6 +5,7 @@ class Cli attr_reader :options, :query def initialize(argv) + @argv = argv @options = {markdown: false} @query = parse! end @@ -24,7 +25,7 @@ def environment_path def parse! OptionParser.new do |parser| parser.on("-m", "--markdown", TrueClass, "Output table in Markdown") - end.parse!(into: options) + end.parse!(@argv, into: options) end def silence diff --git a/test/test_cli.rb b/test/test_cli.rb index f7858cb..a2d688e 100644 --- a/test/test_cli.rb +++ b/test/test_cli.rb @@ -2,18 +2,14 @@ class TestCli < Minitest::Test def test_that_it_parses_queries - ARGV.replace(["SELECT * FROM users"]) - - cli = Taql::Cli.new(ARGV) + cli = Taql::Cli.new(["SELECT * FROM users"]) assert_equal "SELECT * FROM users", *cli.query assert_equal false, cli.options[:markdown] end def test_that_it_parses_options - ARGV.replace(["--markdown", "SELECT * FROM users"]) - - cli = Taql::Cli.new(ARGV) + cli = Taql::Cli.new(["--markdown", "SELECT * FROM users"]) assert_equal "SELECT * FROM users", *cli.query assert_equal true, cli.options[:markdown] From 4968c10e9c9c239ec9ab97192d82a94d4a12cbaa Mon Sep 17 00:00:00 2001 From: Ariel Rzezak Date: Wed, 11 Mar 2026 09:08:38 -0300 Subject: [PATCH 2/6] Memoize headers, columns, and column_widths in Table Also fix headers to use flat_map for correct deduplication when entries have heterogeneous keys. --- lib/taql/table.rb | 6 +++--- test/test_table.rb | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/taql/table.rb b/lib/taql/table.rb index 84b42be..d2d9783 100644 --- a/lib/taql/table.rb +++ b/lib/taql/table.rb @@ -17,13 +17,13 @@ def body end def columns - headers.map do |header| + @columns ||= headers.map do |header| [header, *entries.map { |entry| entry[header] }] end end def headers - entries.map(&:keys).uniq.flatten + @headers ||= entries.flat_map(&:keys).uniq end def print @@ -43,7 +43,7 @@ def border end def column_widths - columns.map { |column| column.map(&:length).max } + @column_widths ||= columns.map { |column| column.map(&:length).max } end def edge diff --git a/test/test_table.rb b/test/test_table.rb index 472bd8a..748c58a 100644 --- a/test/test_table.rb +++ b/test/test_table.rb @@ -68,4 +68,5 @@ def test_that_it_renders_nothing assert_output("\n") { puts empty_table.print } end + end From 2ad5d0fce20cb9f85514ace1814465bbf65361e1 Mon Sep 17 00:00:00 2001 From: Ariel Rzezak Date: Wed, 11 Mar 2026 09:09:14 -0300 Subject: [PATCH 3/6] Add test for heterogeneous entry headers --- test/test_table.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/test_table.rb b/test/test_table.rb index 748c58a..127eaf1 100644 --- a/test/test_table.rb +++ b/test/test_table.rb @@ -69,4 +69,12 @@ def test_that_it_renders_nothing assert_output("\n") { puts empty_table.print } end + def test_headers_with_heterogeneous_entries + table = Taql::Table.new([ + {"name" => "Alice", "age" => 30}, + {"name" => "Bob", "email" => "bob@example.com"} + ]) + + assert_equal ["name", "age", "email"], table.headers + end end From 338efc837a959775028f861a7bb2eed7f8f33ee4 Mon Sep 17 00:00:00 2001 From: Ariel Rzezak Date: Wed, 11 Mar 2026 09:09:56 -0300 Subject: [PATCH 4/6] Raise error when CLI is invoked without a query --- lib/taql/cli.rb | 1 + test/test_cli.rb | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/lib/taql/cli.rb b/lib/taql/cli.rb index eeb193d..be82ff9 100644 --- a/lib/taql/cli.rb +++ b/lib/taql/cli.rb @@ -8,6 +8,7 @@ def initialize(argv) @argv = argv @options = {markdown: false} @query = parse! + raise ArgumentError, "Usage: taql [--markdown] QUERY" if @query.empty? end def run diff --git a/test/test_cli.rb b/test/test_cli.rb index a2d688e..2cdbce9 100644 --- a/test/test_cli.rb +++ b/test/test_cli.rb @@ -14,4 +14,8 @@ def test_that_it_parses_options assert_equal "SELECT * FROM users", *cli.query assert_equal true, cli.options[:markdown] end + + def test_that_it_raises_without_query + assert_raises(ArgumentError) { Taql::Cli.new([]) } + end end From 7257020afe9da28525011871ce1c6e98c58b4c07 Mon Sep 17 00:00:00 2001 From: Ariel Rzezak Date: Wed, 11 Mar 2026 09:12:40 -0300 Subject: [PATCH 5/6] Use lease_connection when available for Rails 7.2+ compat --- lib/taql/railtie.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/taql/railtie.rb b/lib/taql/railtie.rb index 92b345b..5591cff 100644 --- a/lib/taql/railtie.rb +++ b/lib/taql/railtie.rb @@ -6,8 +6,13 @@ class Railtie < Rails::Railtie initializer "taql.initialize" do ActiveSupport.on_load(:active_record) do - Taql.instance_variable_set(:@default_connection, -> { ActiveRecord::Base.connection_pool.connection }) + Taql.instance_variable_set(:@default_connection, method(:connection)) end end + + def self.connection + pool = ActiveRecord::Base.connection_pool + pool.respond_to?(:lease_connection) ? pool.lease_connection : pool.connection + end end end From 2840c25c05193447893f3ec54d12e21203236397 Mon Sep 17 00:00:00 2001 From: Ariel Rzezak Date: Wed, 11 Mar 2026 09:30:51 -0300 Subject: [PATCH 6/6] Add minitest/mock require for Ruby 4.0 compat --- Gemfile.lock | 2 +- lib/taql/railtie.rb | 5 ++++- test/test_helper.rb | 1 + 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 659c063..df0cc3d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -25,7 +25,7 @@ GEM pp (0.6.2) prettyprint prettyprint (0.2.0) - prism (1.4.0) + prism (1.9.0) psych (5.2.3) date stringio diff --git a/lib/taql/railtie.rb b/lib/taql/railtie.rb index 5591cff..e4a8d5d 100644 --- a/lib/taql/railtie.rb +++ b/lib/taql/railtie.rb @@ -11,8 +11,11 @@ class Railtie < Rails::Railtie end def self.connection - pool = ActiveRecord::Base.connection_pool pool.respond_to?(:lease_connection) ? pool.lease_connection : pool.connection end + + def self.pool + ActiveRecord::Base.connection_pool + end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 82615ea..d3e11d7 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,3 +2,4 @@ require "taql" require "minitest/autorun" +require "minitest/mock"