Add binary column default value support to sqlite

This adds support for reading binary column default values before the column data is read from the database.
This makes binary columns behave more like other column types with default values
This commit is contained in:
HParker 2022-08-19 11:27:21 -07:00
parent 43a6b4fc0f
commit e1bf63fca9
2 changed files with 36 additions and 0 deletions

View File

@ -430,6 +430,9 @@ module ActiveRecord
# Numeric types
when /\A-?\d+(\.\d*)?\z/
$&
# Binary columns
when /x'(.*)'/
[ $1 ].pack("H*")
else
# Anything else is blank or some function
# and we can't know the value of that, so return nil.

View File

@ -83,6 +83,39 @@ class DefaultStringsTest < ActiveRecord::TestCase
end
end
if current_adapter?(:SQLite3Adapter, :PostgreSQLAdapter)
class DefaultBinaryTest < ActiveRecord::TestCase
class DefaultBinary < ActiveRecord::Base; end
setup do
@connection = ActiveRecord::Base.connection
@connection.create_table :default_binaries do |t|
t.binary :varbinary_col, null: false, limit: 64, default: "varbinary_default"
t.binary :varbinary_col_hex_looking, null: false, limit: 64, default: "0xDEADBEEF"
end
DefaultBinary.reset_column_information
end
def test_default_varbinary_string
assert_equal "varbinary_default", DefaultBinary.new.varbinary_col
end
if current_adapter?(:Mysql2Adapter) && !ActiveRecord::Base.connection.mariadb?
def test_default_binary_string
assert_equal "binary_default", DefaultBinary.new.binary_col
end
end
def test_default_varbinary_string_that_looks_like_hex
assert_equal "0xDEADBEEF", DefaultBinary.new.varbinary_col_hex_looking
end
teardown do
@connection.drop_table :default_binaries
end
end
end
if supports_text_column_with_default?
class DefaultTextTest < ActiveRecord::TestCase
class DefaultText < ActiveRecord::Base; end