Skip to content

Commit 054a625

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 fc08d8a commit 054a625

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
@@ -42,8 +42,7 @@ def initialize(name, cast_type, default, sql_type_metadata = nil, null = true,
4242
# @geometric_type = geo_type_from_sql_type(sql_type)
4343
build_from_sql_type(sql_type_metadata.sql_type)
4444
end
45-
super(name, cast_type, default, sql_type_metadata, null, default_function,
46-
collation: collation, comment: comment, serial: serial, generated: generated, identity: identity)
45+
4746
if spatial? && @srid
4847
@limit = { srid: @srid, type: to_type_name(geometric_type) }
4948
@limit[:has_z] = true if @has_z
@@ -56,20 +55,18 @@ def initialize(name, cast_type, default, sql_type_metadata = nil, null = true,
5655
:geometric_type,
5756
:has_m,
5857
:has_z,
59-
:srid
58+
:srid,
59+
:hidden
6060

6161
alias geographic? geographic
6262
alias has_z? has_z
6363
alias has_m? has_m
64+
alias hidden? hidden
6465

6566
def limit
6667
spatial? ? @limit : super
6768
end
6869

69-
def hidden?
70-
@hidden
71-
end
72-
7370
def spatial?
7471
%i[geometry geography].include?(@sql_type_metadata.type)
7572
end
@@ -78,6 +75,58 @@ def serial?
7875
default_function == 'unique_rowid()'
7976
end
8077

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

83132
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)