Skip to content

Commit

Permalink
Add binary column default value support to sqlite
Browse files Browse the repository at this point in the history
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
  • Loading branch information
HParker committed Aug 19, 2022
1 parent 43a6b4f commit e1bf63f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Expand Up @@ -430,6 +430,9 @@ def extract_value_from_default(default)
# 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.
Expand Down
33 changes: 33 additions & 0 deletions activerecord/test/cases/defaults_test.rb
Expand Up @@ -83,6 +83,39 @@ def test_default_strings_containing_single_quotes
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
Expand Down

0 comments on commit e1bf63f

Please sign in to comment.