Skip to content

Instantly share code, notes, and snippets.

@edwinv
Last active September 2, 2015 09:44
Show Gist options
  • Save edwinv/5a9aafcb4d37e253784b to your computer and use it in GitHub Desktop.
Save edwinv/5a9aafcb4d37e253784b to your computer and use it in GitHub Desktop.
PG::InvalidTextRepresentation exception
# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.2.4'
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'postgresql', host: 'postgresql.service.consul', database: 'bugreport', username: 'postgres', password: '')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
if execute("SELECT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'post_types')").to_a.first["exists"] == "f"
execute "CREATE TYPE post_types AS ENUM ('some_type')"
end
create_table :posts, force: true do |t|
t.column :post_type, :post_types, null: true
end
end
class Post < ActiveRecord::Base
end
class BugTest < Minitest::Test
def test_enum_null
# No error, `post_type` can be nil
comment = Post.create!
assert_equal "", Post.find(comment.id).post_type
# No error, valid value for `post_type`
comment.update_attribute(:post_type, "some_type")
assert_equal "some_type", Post.find(comment.id).post_type
# PG error, because of string typecast
comment.update_attribute(:post_type, nil)
assert_equal nil, Post.find(comment.id).post_type
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment