Skip to content

Conversation

HParker
Copy link
Contributor

@HParker HParker commented Aug 19, 2022

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.

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", path: "../code/rails"
  gem "sqlite3"
end

require "active_record"
require "minitest/autorun"
require "logger"

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :posts, force: true do |t|
    t.text :some_text, null: false, default: "hello-world"
    t.binary :some_binary, null: false, limit: 1024, default: "hello-world"
  end
end

class Post < ActiveRecord::Base
end

class BugTest < Minitest::Test
  def test_association_stuff

    post = Post.create!

    assert_equal "hello-world", post.some_binary # this passes after this change

    reloaded_post = Post.first
    assert_equal "hello-world", reloaded_post.some_binary # This passes before and after this change
  end
end

similar to #45832, but for 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
@HParker HParker added the SQLite label Aug 19, 2022
@byroot byroot merged commit 3dcd28a into rails:main Aug 19, 2022
@HParker HParker deleted the add-sqlite=binary-column-default-support branch August 19, 2022 21:38
@byroot
Copy link
Member

byroot commented Aug 21, 2022

But I'm confused on what the intended behavior for string type is.

Not sure what you mean here. Same as default value for other DBs. If you create a record without giving a value for that column, it should be set as the default value.

@swaathi
Copy link
Contributor

swaathi commented Aug 21, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants