Skip to content

Commit 6b0af9e

Browse files
committed
Compatibility with Rails 8.1.beta1
- Handle `cast_type` being the second parameter for `SpatialColumn.initialize`. - Handle nil case during OID initiation. - Handle case where in `spatial_factory` the object could be frozen for whatever reason. - Update CI to test against 8.1 and supported PG and Rubies.
1 parent 5d380da commit 6b0af9e

File tree

6 files changed

+28
-13
lines changed

6 files changed

+28
-13
lines changed

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ jobs:
3939
--health-timeout 5s
4040
--health-retries 5
4141
env:
42-
AR_VERSION: 8.0.0.1
42+
AR_VERSION: 8.1.0.rc1
4343
strategy:
4444
fail-fast: false
4545
matrix:
4646
# https://ruby-lang.org/en/downloads/branches
4747
ruby: ["3.4", "3.3", "3.2"]
4848
# https://www.postgresql.org/support/versioning/
49-
pg: [12-master, 13-master, 14-master, 15-master, 16-master]
49+
pg: [13-master, 14-master, 15-master, 16-master, 17-master]
5050
steps:
5151
- name: Set Up Actions
5252
uses: actions/checkout@v4

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ gemspec
66
gem "pg", "~> 1.0", platform: :ruby
77
gem "byebug" if ENV["BYEBUG"]
88

9+
gem "rgeo-activerecord", git: "https://github.com/rgeo/rgeo-activerecord.git"
10+
911
def activerecord_version
1012
return ENV["AR_VERSION"] if ENV["AR_VERSION"]
1113

activerecord-postgis-adapter.gemspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Gem::Specification.new do |spec|
55
spec.summary = "ActiveRecord adapter for PostGIS, based on RGeo."
66
spec.description =
77
"ActiveRecord connection adapter for PostGIS. It is based on the stock " \
8-
"PostgreSQL adapter, and adds built-in support for the spatial extensions "\
8+
"PostgreSQL adapter, and adds built-in support for the spatial extensions " \
99
"provided by PostGIS. It uses the RGeo library to represent spatial data in Ruby."
1010

1111
spec.version = ActiveRecord::ConnectionAdapters::PostGIS::VERSION
@@ -20,8 +20,8 @@ Gem::Specification.new do |spec|
2020
# ruby-lang.org/en/downloads/branches
2121
spec.required_ruby_version = ">= 3.2.0"
2222

23-
spec.add_dependency "activerecord", "~> 8.0.0"
24-
spec.add_dependency "rgeo-activerecord", "~> 8.0.0"
23+
spec.add_dependency "activerecord", "~> 8.1.0.rc1"
24+
spec.add_dependency "rgeo-activerecord"
2525

2626
spec.add_development_dependency "rake", "~> 13.0"
2727
spec.add_development_dependency "minitest", "~> 5.4"

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module OID
1010
# Responsible for parsing sql_types returned from the database and WKT features.
1111
class Spatial < Type::Value
1212
def initialize(geo_type: "geometry", srid: 0, has_z: false, has_m: false, geographic: false)
13+
super()
1314
@geo_type = geo_type
1415
@srid = srid
1516
@has_z = has_z
@@ -25,6 +26,11 @@ def initialize(geo_type: "geometry", srid: 0, has_z: false, has_m: false, geogra
2526
# has_z: false
2627
# has_m: false
2728
def self.parse_sql_type(sql_type)
29+
# Could be nil during type registration
30+
if sql_type.nil?
31+
return [nil, 0, false, false, false]
32+
end
33+
2834
geo_type = nil
2935
srid = 0
3036
has_z = false
@@ -53,10 +59,16 @@ def self.parse_sql_type(sql_type)
5359
end
5460

5561
def spatial_factory
56-
@spatial_factory ||=
62+
if frozen?
5763
RGeo::ActiveRecord::SpatialFactoryStore.instance.factory(
5864
factory_attrs
5965
)
66+
else
67+
@spatial_factory ||=
68+
RGeo::ActiveRecord::SpatialFactoryStore.instance.factory(
69+
factory_attrs
70+
)
71+
end
6072
end
6173

6274
def spatial?

lib/active_record/connection_adapters/postgis/schema_statements.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ def new_column_from_field(table_name, field, _definitions)
1212
type_metadata = fetch_type_metadata(column_name, type, oid.to_i, fmod.to_i)
1313
default_value = extract_value_from_default(default)
1414

15-
if attgenerated.present?
16-
default_function = default
17-
else
18-
default_function = extract_default_function(default_value, default)
19-
end
15+
default_function = if attgenerated.present?
16+
default
17+
else
18+
extract_default_function(default_value, default)
19+
end
2020

2121
if (match = default_function&.match(/\Anextval\('"?(?<sequence_name>.+_(?<suffix>seq\d*))"?'::regclass\)\z/))
2222
serial = sequence_name_from_parts(table_name, column_name, match[:suffix]) == match[:sequence_name]
@@ -27,6 +27,7 @@ def new_column_from_field(table_name, field, _definitions)
2727

2828
SpatialColumn.new(
2929
column_name,
30+
get_oid_type(oid.to_i, fmod.to_i, column_name, type),
3031
default_value,
3132
type_metadata,
3233
!notnull,

lib/active_record/connection_adapters/postgis/spatial_column.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class SpatialColumn < ConnectionAdapters::PostgreSQLColumn # :nodoc:
77
# sql_type examples:
88
# "Geometry(Point,4326)"
99
# "Geography(Point,4326)"
10-
def initialize(name, default, sql_type_metadata = nil, null = true,
10+
def initialize(name, cast_type, default, sql_type_metadata = nil, null = true,
1111
default_function = nil, collation: nil, comment: nil,
1212
serial: nil, generated: nil, spatial: nil, identity: nil)
1313
@sql_type_metadata = sql_type_metadata
@@ -30,7 +30,7 @@ def initialize(name, default, sql_type_metadata = nil, null = true,
3030
# @geometric_type = geo_type_from_sql_type(sql_type)
3131
build_from_sql_type(sql_type_metadata.sql_type)
3232
end
33-
super(name, default, sql_type_metadata, null, default_function,
33+
super(name, cast_type, default, sql_type_metadata, null, default_function,
3434
collation: collation, comment: comment, serial: serial, generated: generated, identity: identity)
3535
if spatial? && @srid
3636
@limit = { srid: @srid, type: to_type_name(geometric_type) }

0 commit comments

Comments
 (0)