Skip to content

Commit 98b3190

Browse files
committed
fix: proper override for Column and Spatial
This is WIP and there is likely more to do. Hence the reference #390. Fixes the flaky test `PostGISTest#test_spatial_factory_attrs_parsing`
1 parent ae87e49 commit 98b3190

File tree

3 files changed

+70
-8
lines changed

3 files changed

+70
-8
lines changed

lib/active_record/connection_adapters/cockroachdb/column.rb

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ def initialize(name, cast_type, default, sql_type_metadata = nil, null = true,
4444
# @geometric_type = geo_type_from_sql_type(sql_type)
4545
build_from_sql_type(sql_type_metadata.sql_type)
4646
end
47-
super(name, cast_type, default, sql_type_metadata, null, default_function,
48-
collation: collation, comment: comment, serial: serial, generated: generated, identity: identity)
47+
4948
if spatial? && @srid
5049
@limit = { srid: @srid, type: to_type_name(geometric_type) }
5150
@limit[:has_z] = true if @has_z
@@ -58,20 +57,18 @@ def initialize(name, cast_type, default, sql_type_metadata = nil, null = true,
5857
:geometric_type,
5958
:has_m,
6059
:has_z,
61-
:srid
60+
:srid,
61+
:hidden
6262

6363
alias geographic? geographic
6464
alias has_z? has_z
6565
alias has_m? has_m
66+
alias hidden? hidden
6667

6768
def limit
6869
spatial? ? @limit : super
6970
end
7071

71-
def hidden?
72-
@hidden
73-
end
74-
7572
def spatial?
7673
%i[geometry geography].include?(@sql_type_metadata.type)
7774
end
@@ -80,6 +77,58 @@ def serial?
8077
default_function == 'unique_rowid()'
8178
end
8279

80+
# TODO: add tests (see #390)
81+
def init_with(coder)
82+
@geographic = coder["geographic"]
83+
@geometric_type = coder["geometric_type"]
84+
@has_m = coder["has_m"]
85+
@has_z = coder["has_z"]
86+
@srid = coder["srid"]
87+
@hidden = coder["hidden"]
88+
@limit = coder["limit"]
89+
super
90+
end
91+
92+
# TODO: add tests (see #390)
93+
def encode_with(coder)
94+
coder["geographic"] = @geographic
95+
coder["geometric_type"] = @geometric_type
96+
coder["has_m"] = @has_m
97+
coder["has_z"] = @has_z
98+
coder["srid"] = @srid
99+
coder["hidden"] = @hidden
100+
coder["limit"] = @limit
101+
super
102+
end
103+
104+
# TODO: add tests (see #390)
105+
def ==(other)
106+
other.is_a?(Column) &&
107+
super &&
108+
other.geographic == geographic &&
109+
other.geometric_type == geometric_type &&
110+
other.has_m == has_m &&
111+
other.has_z == has_z &&
112+
other.srid == srid &&
113+
other.hidden == hidden &&
114+
other.limit == limit
115+
116+
end
117+
alias :eql? :==
118+
119+
# TODO: add tests (see #390)
120+
def hash
121+
Column.hash ^
122+
super.hash ^
123+
geographic.hash ^
124+
geometric_type.hash ^
125+
has_m.hash ^
126+
has_z.hash ^
127+
srid.hash ^
128+
hidden.hash ^
129+
limit.hash
130+
end
131+
83132
private
84133

85134
def build_from_sql_type(sql_type)

lib/active_record/connection_adapters/cockroachdb/oid/spatial.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def initialize(oid, sql_type)
3333
factory_attrs
3434
)
3535
end
36+
protected attr_reader :sql_type, :spatial_factory
3637

3738
# sql_type: geometry, geometry(Point), geometry(Point,4326), ...
3839
#
@@ -89,6 +90,19 @@ def serialize(value)
8990
.generate(geo_value)
9091
end
9192

93+
# TODO: add tests (see #390)
94+
def ==(other)
95+
super &&
96+
@sql_type == other.sql_type
97+
@spatial_factory == other.spatial_factory
98+
end
99+
alias eql? ==
100+
101+
# TODO: add tests (see #390)
102+
def hash
103+
super ^ [@sql_type, @spatial_factory].hash
104+
end
105+
92106
private
93107

94108
def cast_value(value)

test/cases/adapters/postgresql/postgis_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ def test_custom_factory
104104
object.save!
105105
object.reload
106106
assert_equal boundary.to_s, object.boundary.to_s
107-
spatial_factory_store.clear
108107
end
109108

110109
def test_spatial_factory_attrs_parsing

0 commit comments

Comments
 (0)