From 6b0af9e4e0bf4a5a64fe8d4930a4773d83c6e64f Mon Sep 17 00:00:00 2001 From: Tony Drake Date: Fri, 5 Sep 2025 09:04:06 -0400 Subject: [PATCH 1/4] 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. --- .github/workflows/tests.yml | 4 ++-- Gemfile | 2 ++ activerecord-postgis-adapter.gemspec | 6 +++--- .../connection_adapters/postgis/oid/spatial.rb | 14 +++++++++++++- .../postgis/schema_statements.rb | 11 ++++++----- .../connection_adapters/postgis/spatial_column.rb | 4 ++-- 6 files changed, 28 insertions(+), 13 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 054b14f1..8ec4b499 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -39,14 +39,14 @@ jobs: --health-timeout 5s --health-retries 5 env: - AR_VERSION: 8.0.0.1 + AR_VERSION: 8.1.0.rc1 strategy: fail-fast: false matrix: # https://ruby-lang.org/en/downloads/branches ruby: ["3.4", "3.3", "3.2"] # https://www.postgresql.org/support/versioning/ - pg: [12-master, 13-master, 14-master, 15-master, 16-master] + pg: [13-master, 14-master, 15-master, 16-master, 17-master] steps: - name: Set Up Actions uses: actions/checkout@v4 diff --git a/Gemfile b/Gemfile index 5976f205..337f6737 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,8 @@ gemspec gem "pg", "~> 1.0", platform: :ruby gem "byebug" if ENV["BYEBUG"] +gem "rgeo-activerecord", git: "https://github.com/rgeo/rgeo-activerecord.git" + def activerecord_version return ENV["AR_VERSION"] if ENV["AR_VERSION"] diff --git a/activerecord-postgis-adapter.gemspec b/activerecord-postgis-adapter.gemspec index 53cf6952..9920c5c3 100644 --- a/activerecord-postgis-adapter.gemspec +++ b/activerecord-postgis-adapter.gemspec @@ -5,7 +5,7 @@ Gem::Specification.new do |spec| spec.summary = "ActiveRecord adapter for PostGIS, based on RGeo." spec.description = "ActiveRecord connection adapter for PostGIS. It is based on the stock " \ - "PostgreSQL adapter, and adds built-in support for the spatial extensions "\ + "PostgreSQL adapter, and adds built-in support for the spatial extensions " \ "provided by PostGIS. It uses the RGeo library to represent spatial data in Ruby." spec.version = ActiveRecord::ConnectionAdapters::PostGIS::VERSION @@ -20,8 +20,8 @@ Gem::Specification.new do |spec| # ruby-lang.org/en/downloads/branches spec.required_ruby_version = ">= 3.2.0" - spec.add_dependency "activerecord", "~> 8.0.0" - spec.add_dependency "rgeo-activerecord", "~> 8.0.0" + spec.add_dependency "activerecord", "~> 8.1.0.rc1" + spec.add_dependency "rgeo-activerecord" spec.add_development_dependency "rake", "~> 13.0" spec.add_development_dependency "minitest", "~> 5.4" diff --git a/lib/active_record/connection_adapters/postgis/oid/spatial.rb b/lib/active_record/connection_adapters/postgis/oid/spatial.rb index 46ed619f..bd272a50 100644 --- a/lib/active_record/connection_adapters/postgis/oid/spatial.rb +++ b/lib/active_record/connection_adapters/postgis/oid/spatial.rb @@ -10,6 +10,7 @@ module OID # Responsible for parsing sql_types returned from the database and WKT features. class Spatial < Type::Value def initialize(geo_type: "geometry", srid: 0, has_z: false, has_m: false, geographic: false) + super() @geo_type = geo_type @srid = srid @has_z = has_z @@ -25,6 +26,11 @@ def initialize(geo_type: "geometry", srid: 0, has_z: false, has_m: false, geogra # has_z: false # has_m: false def self.parse_sql_type(sql_type) + # Could be nil during type registration + if sql_type.nil? + return [nil, 0, false, false, false] + end + geo_type = nil srid = 0 has_z = false @@ -53,10 +59,16 @@ def self.parse_sql_type(sql_type) end def spatial_factory - @spatial_factory ||= + if frozen? RGeo::ActiveRecord::SpatialFactoryStore.instance.factory( factory_attrs ) + else + @spatial_factory ||= + RGeo::ActiveRecord::SpatialFactoryStore.instance.factory( + factory_attrs + ) + end end def spatial? diff --git a/lib/active_record/connection_adapters/postgis/schema_statements.rb b/lib/active_record/connection_adapters/postgis/schema_statements.rb index 62f3f754..f1e02c58 100644 --- a/lib/active_record/connection_adapters/postgis/schema_statements.rb +++ b/lib/active_record/connection_adapters/postgis/schema_statements.rb @@ -12,11 +12,11 @@ def new_column_from_field(table_name, field, _definitions) type_metadata = fetch_type_metadata(column_name, type, oid.to_i, fmod.to_i) default_value = extract_value_from_default(default) - if attgenerated.present? - default_function = default - else - default_function = extract_default_function(default_value, default) - end + default_function = if attgenerated.present? + default + else + extract_default_function(default_value, default) + end if (match = default_function&.match(/\Anextval\('"?(?.+_(?seq\d*))"?'::regclass\)\z/)) 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) SpatialColumn.new( column_name, + get_oid_type(oid.to_i, fmod.to_i, column_name, type), default_value, type_metadata, !notnull, diff --git a/lib/active_record/connection_adapters/postgis/spatial_column.rb b/lib/active_record/connection_adapters/postgis/spatial_column.rb index 9457e390..ea50d69b 100644 --- a/lib/active_record/connection_adapters/postgis/spatial_column.rb +++ b/lib/active_record/connection_adapters/postgis/spatial_column.rb @@ -7,7 +7,7 @@ class SpatialColumn < ConnectionAdapters::PostgreSQLColumn # :nodoc: # sql_type examples: # "Geometry(Point,4326)" # "Geography(Point,4326)" - def initialize(name, default, sql_type_metadata = nil, null = true, + def initialize(name, cast_type, default, sql_type_metadata = nil, null = true, default_function = nil, collation: nil, comment: nil, serial: nil, generated: nil, spatial: nil, identity: nil) @sql_type_metadata = sql_type_metadata @@ -30,7 +30,7 @@ def initialize(name, default, sql_type_metadata = nil, null = true, # @geometric_type = geo_type_from_sql_type(sql_type) build_from_sql_type(sql_type_metadata.sql_type) end - super(name, default, sql_type_metadata, null, default_function, + super(name, cast_type, default, sql_type_metadata, null, default_function, collation: collation, comment: comment, serial: serial, generated: generated, identity: identity) if spatial? && @srid @limit = { srid: @srid, type: to_type_name(geometric_type) } From 071088994e7c3b671e9e043390603152c1939b9f Mon Sep 17 00:00:00 2001 From: Tony Drake Date: Sat, 18 Oct 2025 17:16:06 -0400 Subject: [PATCH 2/4] Fix backtrace in deprication tests Borrowed from https://github.com/cockroachdb/activerecord-cockroachdb-adapter/commit/e656a24a28b9089e61a6dd14df9d92a13d8e3f20#diff-ce5c7fbac7d9135b5da3cda2f2a9a00e930ce097624d919ef81100dca118e31d --- .../AssociationDeprecationTest/NotifyModeTest.rb | 2 ++ .../RaiseBacktraceModeTest.rb | 2 ++ .../AssociationDeprecationTest/RaiseModeTest.rb | 2 ++ .../WarnBacktraceModeTest.rb | 2 ++ .../AssociationDeprecationTest/WarnModeTest.rb | 2 ++ .../fix_backtrace_cleaner.rb | 10 ++++++++++ 6 files changed, 20 insertions(+) create mode 100644 test/excludes/AssociationDeprecationTest/NotifyModeTest.rb create mode 100644 test/excludes/AssociationDeprecationTest/RaiseBacktraceModeTest.rb create mode 100644 test/excludes/AssociationDeprecationTest/RaiseModeTest.rb create mode 100644 test/excludes/AssociationDeprecationTest/WarnBacktraceModeTest.rb create mode 100644 test/excludes/AssociationDeprecationTest/WarnModeTest.rb create mode 100644 test/excludes/AssociationDeprecationTest/fix_backtrace_cleaner.rb diff --git a/test/excludes/AssociationDeprecationTest/NotifyModeTest.rb b/test/excludes/AssociationDeprecationTest/NotifyModeTest.rb new file mode 100644 index 00000000..ed1f6040 --- /dev/null +++ b/test/excludes/AssociationDeprecationTest/NotifyModeTest.rb @@ -0,0 +1,2 @@ +require_relative "fix_backtrace_cleaner" +include(FixBacktraceCleaner) diff --git a/test/excludes/AssociationDeprecationTest/RaiseBacktraceModeTest.rb b/test/excludes/AssociationDeprecationTest/RaiseBacktraceModeTest.rb new file mode 100644 index 00000000..ed1f6040 --- /dev/null +++ b/test/excludes/AssociationDeprecationTest/RaiseBacktraceModeTest.rb @@ -0,0 +1,2 @@ +require_relative "fix_backtrace_cleaner" +include(FixBacktraceCleaner) diff --git a/test/excludes/AssociationDeprecationTest/RaiseModeTest.rb b/test/excludes/AssociationDeprecationTest/RaiseModeTest.rb new file mode 100644 index 00000000..ed1f6040 --- /dev/null +++ b/test/excludes/AssociationDeprecationTest/RaiseModeTest.rb @@ -0,0 +1,2 @@ +require_relative "fix_backtrace_cleaner" +include(FixBacktraceCleaner) diff --git a/test/excludes/AssociationDeprecationTest/WarnBacktraceModeTest.rb b/test/excludes/AssociationDeprecationTest/WarnBacktraceModeTest.rb new file mode 100644 index 00000000..ed1f6040 --- /dev/null +++ b/test/excludes/AssociationDeprecationTest/WarnBacktraceModeTest.rb @@ -0,0 +1,2 @@ +require_relative "fix_backtrace_cleaner" +include(FixBacktraceCleaner) diff --git a/test/excludes/AssociationDeprecationTest/WarnModeTest.rb b/test/excludes/AssociationDeprecationTest/WarnModeTest.rb new file mode 100644 index 00000000..ed1f6040 --- /dev/null +++ b/test/excludes/AssociationDeprecationTest/WarnModeTest.rb @@ -0,0 +1,2 @@ +require_relative "fix_backtrace_cleaner" +include(FixBacktraceCleaner) diff --git a/test/excludes/AssociationDeprecationTest/fix_backtrace_cleaner.rb b/test/excludes/AssociationDeprecationTest/fix_backtrace_cleaner.rb new file mode 100644 index 00000000..9ed08029 --- /dev/null +++ b/test/excludes/AssociationDeprecationTest/fix_backtrace_cleaner.rb @@ -0,0 +1,10 @@ +module FixBacktraceCleaner + def setup + super + bc = ActiveSupport::BacktraceCleaner.new + bc.remove_silencers! + bc.remove_filters! + bc.add_silencer { !_1.include?(::AssociationDeprecationTest::TestCase::THIS_FILE) } + ActiveRecord::LogSubscriber.backtrace_cleaner = bc + end +end From b0041ea51c51c36216c076146d5cb876a9375cf1 Mon Sep 17 00:00:00 2001 From: Tony Drake Date: Sat, 18 Oct 2025 18:25:25 -0400 Subject: [PATCH 3/4] Remove pointless frozen check logic --- .../connection_adapters/postgis/oid/spatial.rb | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/lib/active_record/connection_adapters/postgis/oid/spatial.rb b/lib/active_record/connection_adapters/postgis/oid/spatial.rb index bd272a50..135dd291 100644 --- a/lib/active_record/connection_adapters/postgis/oid/spatial.rb +++ b/lib/active_record/connection_adapters/postgis/oid/spatial.rb @@ -59,16 +59,9 @@ def self.parse_sql_type(sql_type) end def spatial_factory - if frozen? - RGeo::ActiveRecord::SpatialFactoryStore.instance.factory( - factory_attrs - ) - else - @spatial_factory ||= - RGeo::ActiveRecord::SpatialFactoryStore.instance.factory( - factory_attrs - ) - end + RGeo::ActiveRecord::SpatialFactoryStore.instance.factory( + factory_attrs + ) end def spatial? From 4a4faf56967250d87cd3ca8606b9c84030a2e428 Mon Sep 17 00:00:00 2001 From: Tony Drake Date: Wed, 22 Oct 2025 14:19:16 -0400 Subject: [PATCH 4/4] Update gemspec for Rails 8.1.0 final --- .github/workflows/tests.yml | 2 +- CONTRIBUTING.md | 4 ++-- activerecord-postgis-adapter.gemspec | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 8ec4b499..a3d7431a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -39,7 +39,7 @@ jobs: --health-timeout 5s --health-retries 5 env: - AR_VERSION: 8.1.0.rc1 + AR_VERSION: 8.1.0 strategy: fail-fast: false matrix: diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5b6ff06d..a150be69 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -40,8 +40,8 @@ Make sure the tests pass: Run tests with a specific ActiveRecord version: ```sh -AR_VERSION=7.0.1 bundle install -AR_VERSION=7.0.1 bundle exec rake test +AR_VERSION=8.1.0 bundle install +AR_VERSION=8.1.0 bundle exec rake test ``` To run a specific test, use the `POSTGIS_TEST_FILES` environment variable: diff --git a/activerecord-postgis-adapter.gemspec b/activerecord-postgis-adapter.gemspec index 9920c5c3..7f83e830 100644 --- a/activerecord-postgis-adapter.gemspec +++ b/activerecord-postgis-adapter.gemspec @@ -20,7 +20,7 @@ Gem::Specification.new do |spec| # ruby-lang.org/en/downloads/branches spec.required_ruby_version = ">= 3.2.0" - spec.add_dependency "activerecord", "~> 8.1.0.rc1" + spec.add_dependency "activerecord", "~> 8.1.0" spec.add_dependency "rgeo-activerecord" spec.add_development_dependency "rake", "~> 13.0"