Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix SchemaStatements#initialize_type_map
Since the Rails 6 upgrade, we started seeing some random PostGIS type issues on our staging env, while we don't know yet why our production env doesn't seem to be affected, debugging the issue on staging shows that the ActiveRecord connection `type_map` isn't initialized properly which leave the custom `ActiveRecord::ConnectionAdapters::PostGIS::SpatialColumn` without a defined type, while usually, the type is lazy loaded correctly later on by `PostgreSQLAdapter#get_oid_type` it could happen that the `ModelSchema#load_schema!` method is called first which will define and cache the attribute type (`@attribute_types`) with the wrong default value type (`ActiveModel::Type::Value`) `type_cast` using the `lookup_cast_type_from_column` method that reads straight from the `type_map` cache without lazy loading the type. The wrong type is then always returned for that column which brakes all the casting logic. This patch fixes the `SchemaStatements#initialize_type_map` method by ensuring that the specific PostGIS types are registered before calling `super` which will then call `load_additional_types` and registers the custom types. That way the custom column will be properly initialized and be present in the `type_map` cache. Some code references: - The `initialize_type_map` activerecord-postgis-adapter method overwritten https://github.com/rgeo/activerecord-postgis-adapter/blob/cc0e3f53520feed0b43800d8ee86561ee03de413/lib/active_record/connection_adapters/postgis/schema_statements.rb#L87-L105 - the `load_additional_types` method in Rails: https://github.com/rails/rails/blob/8d57cb39a88787bb6cfb7e1c481556ef6d8ede7a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L593 - the `get_oid_type` method in Rails that lazy load the type: https://github.com/rails/rails/blob/8d57cb39a88787bb6cfb7e1c481556ef6d8ede7a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L514-L516 - the `lookup_cast_type_from_column` method in Rails that does not lazy load the type: https://github.com/rails/rails/blob/15748f6a052d4df68b6acf66456c181b42d9fe25/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb#L78 This patch should solve rgeo#307 and rgeo#308.
- Loading branch information