Skip to content

Commit

Permalink
Make schema_dumper extension format options similar to Hash#inspect o…
Browse files Browse the repository at this point in the history
…n Ruby 3.4+

This fixes the schema_dumper specs on Ruby 3.4.0-preview2.  This
includes additional changes to avoid deprecation warnings and fix
other spec failures on Ruby 3.4.0-preview2.
  • Loading branch information
jeremyevans committed Oct 8, 2024
1 parent 4673918 commit 1176f61
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 78 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
=== master

* Make schema_dumper extension format options similar to Hash#inspect on Ruby 3.4+ (jeremyevans)

* Avoid deprecation warnings on Ruby 3.4.0-preview2 (jeremyevans)

* Handle FROM tables that are SQL::DelayedEvaluation instances when trying to get primary key values after INSERT on PostgreSQL (tomasmiguez) (#2230, #2232)

=== 5.85.0 (2025-10-01)
Expand Down
7 changes: 6 additions & 1 deletion lib/sequel/database/misc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class Database
:time=>Sequel::SQLTime, :boolean=>[TrueClass, FalseClass].freeze, :float=>Float, :decimal=>BigDecimal,
:blob=>Sequel::SQL::Blob}.freeze

# :nocov:
URI_PARSER = defined?(::URI::RFC2396_PARSER) ? ::URI::RFC2396_PARSER : ::URI::DEFAULT_PARSER
# :nocov:
private_constant :URI_PARSER

# Nested hook Proc; each new hook Proc just wraps the previous one.
@initialize_hook = proc{|db| }

Expand Down Expand Up @@ -85,7 +90,7 @@ def self.uri_to_options(uri)
def self.options_from_uri(uri)
uri_options = uri_to_options(uri)
uri.query.split('&').map{|s| s.split('=')}.each{|k,v| uri_options[k.to_sym] = v if k && !k.empty?} unless uri.query.to_s.strip.empty?
uri_options.to_a.each{|k,v| uri_options[k] = URI::DEFAULT_PARSER.unescape(v) if v.is_a?(String)}
uri_options.to_a.each{|k,v| uri_options[k] = URI_PARSER.unescape(v) if v.is_a?(String)}
uri_options
end
private_class_method :options_from_uri
Expand Down
18 changes: 15 additions & 3 deletions lib/sequel/extensions/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@

module Sequel
module SchemaDumper
# :nocov:
IGNORE_INDEX_ERRORS_KEY = RUBY_VERSION >= '3.4' ? 'ignore_index_errors: ' : ':ignore_index_errors=>'
# :nocov:
private_constant :IGNORE_INDEX_ERRORS_KEY

# Convert the column schema information to a hash of column options, one of which must
# be :type. The other options added should modify that type (e.g. :size). If a
# database type is not recognized, return it as a String type.
Expand Down Expand Up @@ -161,7 +166,7 @@ def dump_schema_migration(options=OPTS)
def dump_table_schema(table, options=OPTS)
gen = dump_table_generator(table, options)
commands = [gen.dump_columns, gen.dump_constraints, gen.dump_indexes].reject{|x| x == ''}.join("\n\n")
"create_table(#{table.inspect}#{', :ignore_index_errors=>true' if !options[:same_db] && options[:indexes] != false && !gen.indexes.empty?}) do\n#{commands.gsub(/^/, ' ')}\nend"
"create_table(#{table.inspect}#{", #{IGNORE_INDEX_ERRORS_KEY}true" if !options[:same_db] && options[:indexes] != false && !gen.indexes.empty?}) do\n#{commands.gsub(/^/, ' ')}\nend"
end

private
Expand Down Expand Up @@ -425,6 +430,13 @@ def use_column_schema_to_ruby_default_fallback?

module Schema
class CreateTableGenerator
# :nocov:
DEFAULT_KEY = RUBY_VERSION >= '3.4' ? 'default: ' : ':default=>'
IGNORE_ERRORS_KEY = RUBY_VERSION >= '3.4' ? 'ignore_errors: ' : ':ignore_errors=>'
# :nocov:
private_constant :DEFAULT_KEY
private_constant :IGNORE_ERRORS_KEY

# Dump this generator's columns to a string that could be evaled inside
# another instance to represent the same columns
def dump_columns
Expand Down Expand Up @@ -508,7 +520,7 @@ def dump_indexes(options=OPTS)
c = c.dup
cols = c.delete(:columns)
if table = options[:add_index] || options[:drop_index]
"#{options[:drop_index] ? 'drop' : 'add'}_index #{table.inspect}, #{cols.inspect}#{', :ignore_errors=>true' if options[:ignore_errors]}#{opts_inspect(c)}"
"#{options[:drop_index] ? 'drop' : 'add'}_index #{table.inspect}, #{cols.inspect}#{", #{IGNORE_ERRORS_KEY}true" if options[:ignore_errors]}#{opts_inspect(c)}"
else
"index #{cols.inspect}#{opts_inspect(c)}"
end
Expand All @@ -526,7 +538,7 @@ def opts_inspect(opts)
if opts[:default]
opts = opts.dup
de = Sequel.eval_inspect(opts.delete(:default))
", :default=>#{de}#{", #{opts.inspect[1...-1]}" if opts.length > 0}"
", #{DEFAULT_KEY}#{de}#{", #{opts.inspect[1...-1]}" if opts.length > 0}"
else
", #{opts.inspect[1...-1]}" if opts.length > 0
end
Expand Down
6 changes: 4 additions & 2 deletions spec/bin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,17 @@ def bin(opts={})
foreign_key :a, :a
index :a
end
size = RUBY_VERSION >= '3.4' ? 'size: 255' : ':size=>255'
ignore_index_errors = RUBY_VERSION >= '3.4' ? 'ignore_index_errors: true' : ':ignore_index_errors=>true'
bin(:args=>'-d').must_equal <<END
Sequel.migration do
change do
create_table(:a) do
primary_key :a
String :name, :size=>255
String :name, #{size}
end
create_table(:b, :ignore_index_errors=>true) do
create_table(:b, #{ignore_index_errors}) do
foreign_key :a, :a
index [:a]
Expand Down
2 changes: 1 addition & 1 deletion spec/core/database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2263,7 +2263,7 @@ def @db.database_error_regexps

it "should raise an InvalidValue when given an invalid value" do
proc{@db.typecast_value(:integer, "13a")}.must_raise(Sequel::InvalidValue)
proc{@db.typecast_value(:float, "4.e2")}.must_raise(Sequel::InvalidValue)
proc{@db.typecast_value(:float, "4.e2a")}.must_raise(Sequel::InvalidValue)
proc{@db.typecast_value(:decimal, :invalid_value)}.must_raise(Sequel::InvalidValue)
proc{@db.typecast_value(:date, Object.new)}.must_raise(Sequel::InvalidValue)
proc{@db.typecast_value(:date, 'a')}.must_raise(Sequel::InvalidValue)
Expand Down
15 changes: 13 additions & 2 deletions spec/extensions/arbitrary_servers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,18 @@
@db[:t].all
q1.push nil
t.join
@db.sqls.must_equal ['SELECT * FROM t', 'SELECT * FROM t -- {:host=>"a"}', 'SELECT * FROM t', 'SELECT * FROM t -- {:host=>"c"}', 'SELECT * FROM t -- {:host=>"d"}',
'SELECT * FROM t -- {:host=>"b"}', 'SELECT * FROM t -- {:host=>"a"}', 'SELECT * FROM t', 'SELECT * FROM t -- {:host=>"c"}', 'SELECT * FROM t']
host = RUBY_VERSION >= '3.4' ? 'host: ' : ':host=>'
@db.sqls.must_equal [
'SELECT * FROM t',
"SELECT * FROM t -- {#{host}\"a\"}",
'SELECT * FROM t',
"SELECT * FROM t -- {#{host}\"c\"}",
"SELECT * FROM t -- {#{host}\"d\"}",
"SELECT * FROM t -- {#{host}\"b\"}",
"SELECT * FROM t -- {#{host}\"a\"}",
'SELECT * FROM t',
"SELECT * FROM t -- {#{host}\"c\"}",
'SELECT * FROM t',
]
end
end
Loading

0 comments on commit 1176f61

Please sign in to comment.