Skip to content

Commit df34946

Browse files
committed
feat: make type_map ractor shareable
Fixes #389
1 parent ae9c973 commit df34946

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

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

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,21 @@ class Spatial < Type::Value
2727
# "geometry(Geography,4326)"
2828
def initialize(oid, sql_type)
2929
super()
30-
@sql_type = sql_type
31-
@geo_type, @srid, @has_z, @has_m = self.class.parse_sql_type(sql_type)
32-
@spatial_factory =
33-
RGeo::ActiveRecord::SpatialFactoryStore.instance.factory(
34-
factory_attrs
35-
)
30+
@sql_type = sql_type.freeze
31+
@factory_attrs = self.class
32+
.parse_sql_type(sql_type)
33+
.then { |geo_type, srid, has_z, has_m|
34+
{
35+
geo_type: geo_type.underscore.freeze,
36+
srid: srid.freeze,
37+
has_z: has_z.freeze,
38+
has_m: has_m.freeze,
39+
sql_type: type.to_s.freeze
40+
}
41+
}
42+
.freeze
3643
end
37-
protected attr_reader :sql_type, :spatial_factory
44+
protected attr_reader :sql_type, :factory_attrs
3845

3946
# sql_type: geometry, geometry(Point), geometry(Point,4326), ...
4047
#
@@ -66,7 +73,7 @@ def self.parse_sql_type(sql_type)
6673
end
6774

6875
def geographic?
69-
@sql_type =~ /geography/
76+
@sql_type.start_with?("geography")
7077
end
7178

7279
def spatial?
@@ -94,14 +101,14 @@ def serialize(value)
94101
# TODO: add tests (see #390)
95102
def ==(other)
96103
super &&
97-
@sql_type == other.sql_type
98-
@spatial_factory == other.spatial_factory
104+
@sql_type == other.sql_type &&
105+
@factory_attrs == other.factory_attrs
99106
end
100107
alias eql? ==
101108

102109
# TODO: add tests (see #390)
103110
def hash
104-
super ^ [@sql_type, @spatial_factory].hash
111+
super ^ [@sql_type, @factory_attrs].hash
105112
end
106113

107114
private
@@ -125,20 +132,16 @@ def binary_string?(string)
125132

126133
def wkt_parser(string)
127134
if binary_string?(string)
128-
RGeo::WKRep::WKBParser.new(@spatial_factory, support_ewkb: true, default_srid: @srid)
135+
RGeo::WKRep::WKBParser.new(spatial_factory, support_ewkb: true, default_srid: @srid)
129136
else
130-
RGeo::WKRep::WKTParser.new(@spatial_factory, support_ewkt: true, default_srid: @srid)
137+
RGeo::WKRep::WKTParser.new(spatial_factory, support_ewkt: true, default_srid: @srid)
131138
end
132139
end
133140

134-
def factory_attrs
135-
{
136-
geo_type: @geo_type.underscore,
137-
has_m: @has_m,
138-
has_z: @has_z,
139-
srid: @srid,
140-
sql_type: type.to_s
141-
}
141+
def spatial_factory
142+
RGeo::ActiveRecord::SpatialFactoryStore.instance.factory(
143+
factory_attrs
144+
)
142145
end
143146
end
144147
end
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
exclude :test_indexes, "Rails transactional tests are being used while making schema changes. See https://www.cockroachlabs.com/docs/stable/online-schema-changes.html#limited-support-for-schema-changes-within-transactions."
22
exclude :test_remove_index_when_name_and_wrong_column_name_specified, "Rails transactional tests are being used while making schema changes. See https://www.cockroachlabs.com/docs/stable/online-schema-changes.html#limited-support-for-schema-changes-within-transactions."
33
exclude :test_remove_index_when_name_and_wrong_column_name_specified_positional_argument, "Rails transactional tests are being used while making schema changes. See https://www.cockroachlabs.com/docs/stable/online-schema-changes.html#limited-support-for-schema-changes-within-transactions."
4-
exclude :test_type_map_is_ractor_shareable, "Not yet, see https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/389"

0 commit comments

Comments
 (0)