Skip to content

Commit d876a43

Browse files
committed
avoid column.default type_cast-ing on AR 4.2 - been causing some nesty bugs
... e.g. on postres as an array 'default' instance has been leaking into models
1 parent 2e8f22d commit d876a43

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

lib/arjdbc/jdbc/column.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ def initialize(config, name, *args)
2626
end
2727
end
2828
default = default_value(default)
29-
default = args[0].type_cast_from_database(default) if ArJdbc::AR42
3029

3130
# super <= 4.1: (name, default, sql_type = nil, null = true)
3231
# super >= 4.2: (name, default, cast_type, sql_type = nil, null = true)

test/db/postgresql/column_test.rb

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,64 @@
11
require 'db/postgres'
22
require 'change_column_test_methods'
33

4-
class PostgreSQLChangeColumnTest < Test::Unit::TestCase
4+
class PostgreSQLColumnTest < Test::Unit::TestCase
55
include ChangeColumnTestMethods
6-
end
6+
end
7+
8+
class PostgreSQLColumnDefaultTest < Test::Unit::TestCase
9+
10+
class Project < ActiveRecord::Base; end
11+
12+
def setup
13+
@connection = ActiveRecord::Base.connection
14+
@connection.transaction do
15+
@connection.create_table 'projects', force: :cascade do |t|
16+
t.string "name", limit: 255, default: ' '
17+
t.text "some_ids", array: true, default: []
18+
end
19+
end
20+
end
21+
22+
def teardown
23+
@connection.execute 'DROP TABLE IF EXISTS projects'
24+
end
25+
26+
test 'default value from column is not shared' do
27+
assert_equal ' ', Project.columns_hash['name'].default
28+
29+
p = Project.create!
30+
assert_equal ' ', p.name
31+
p.name.replace '1-replaced'
32+
33+
p = Project.new; p.name << '2-append'; p.save!
34+
assert_equal ' ', Project.columns_hash['name'].default
35+
36+
p = Project.new; p.name << '3-append'
37+
assert_equal ' ', Project.columns_hash['name'].default
38+
end
39+
40+
test '_ids assingment incorrect array column default (shared bug)' do
41+
Project.create!(:name => 'p1', :some_ids => ['1'])
42+
p = Project.new(:name => 'p2'); p.some_ids << '2'; p.save!
43+
44+
if ar_version('4.2')
45+
assert_equal '{}', Project.columns_hash['some_ids'].default
46+
else
47+
assert_equal [], Project.columns_hash['some_ids'].default
48+
end
49+
50+
assert_equal ['2'], p.some_ids
51+
if ar_version('4.2')
52+
assert_equal ['2'], p.reload.some_ids
53+
else
54+
# MRI under AR 4.1 gets messed up the same :
55+
assert_equal [], p.reload.some_ids
56+
end
57+
58+
p = Project.new(:name => 'p3')
59+
assert_equal [], p.some_ids
60+
p.save!
61+
assert_equal [], p.some_ids
62+
end
63+
64+
end if Test::Unit::TestCase.ar_version('4.0')

0 commit comments

Comments
 (0)