activerecord-postgis-adapter 5.1.0 → 6.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9a032372c1d3d40f4808f7bc319c6d6c901612492c6702a882bf245749980b05
4
- data.tar.gz: 730358e510e622e42d3ebf743a2e2a6366776a60303b205b7d8fd7fa4743f6bd
3
+ metadata.gz: 978d238ad317f3e43d498f237d63573aa71291fe2485f2b35b0013e829bc0727
4
+ data.tar.gz: 2520a77cc0070c3afaa4fdba2d771ba05e9fb569186e35a4e92dd24816f43ae3
5
5
  SHA512:
6
- metadata.gz: e2bd49f0d89b23776c003411c9c71e6d5882554095a78fc25e8adf6f94d4c06cded265db02881261a0b563f28a92ff2abe50df38f237e185ec628a69728673d7
7
- data.tar.gz: 4f2a8607f4561af4400e00f2a44bf65de457267c6aa74ed6432da8633efd2e1dd1ae14151b4b00f0f6f82895e2c97948e2ed8a2edce64ae4981d219dd878a5d5
6
+ metadata.gz: 5a9e6472022e69fbaf2d07dd76f98cdf97c440f6009a26ccd59d1b580ca4ff3590b889e0c1afe9addb2f518e88decec6e1107a5b0301c12ce19b9df5e22073ff
7
+ data.tar.gz: 102a2b33b86298a52ae3dac860bf75d0531ad32974b914f2e5ebc154d6d22c21c371770d6c649fa6abb7654984c5e3e3d4bdb6cab106ffb0304cd83f15f8e529
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Arel # :nodoc:
2
4
  module Visitors # :nodoc:
3
5
  # Different super-class under JRuby JDBC adapter.
@@ -1,46 +1,48 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module ConnectionAdapters
3
5
  module PostGIS
4
6
  module ColumnMethods
5
7
  def spatial(name, options = {})
6
8
  raise "You must set a type. For example: 't.spatial type: :st_point'" unless options[:type]
7
- column(name, options[:type], options)
9
+ column(name, options[:type], **options)
8
10
  end
9
11
 
10
12
  def geography(name, options = {})
11
- column(name, :geography, options)
13
+ column(name, :geography, **options)
12
14
  end
13
15
 
14
16
  def geometry(name, options = {})
15
- column(name, :geometry, options)
17
+ column(name, :geometry, **options)
16
18
  end
17
19
 
18
20
  def geometry_collection(name, options = {})
19
- column(name, :geometry_collection, options)
21
+ column(name, :geometry_collection, **options)
20
22
  end
21
23
 
22
24
  def line_string(name, options = {})
23
- column(name, :line_string, options)
25
+ column(name, :line_string, **options)
24
26
  end
25
27
 
26
28
  def multi_line_string(name, options = {})
27
- column(name, :multi_line_string, options)
29
+ column(name, :multi_line_string, **options)
28
30
  end
29
31
 
30
32
  def multi_point(name, options = {})
31
- column(name, :multi_point, options)
33
+ column(name, :multi_point, **options)
32
34
  end
33
35
 
34
36
  def multi_polygon(name, options = {})
35
- column(name, :multi_polygon, options)
37
+ column(name, :multi_polygon, **options)
36
38
  end
37
39
 
38
40
  def st_point(name, options = {})
39
- column(name, :st_point, options)
41
+ column(name, :st_point, **options)
40
42
  end
41
43
 
42
44
  def st_polygon(name, options = {})
43
- column(name, :st_polygon, options)
45
+ column(name, :st_polygon, **options)
44
46
  end
45
47
  end
46
48
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  if RUBY_ENGINE == "jruby"
2
4
  require "active_record/connection_adapters/jdbcpostgresql_adapter"
3
5
  else
@@ -29,13 +31,18 @@ module ActiveRecord # :nodoc:
29
31
  conn_params[:user] = conn_params.delete(:username) if conn_params[:username]
30
32
  conn_params[:dbname] = conn_params.delete(:database) if conn_params[:database]
31
33
 
32
- # Forward only valid config params to PGconn.connect.
34
+ # Forward only valid config params to PG.connect
33
35
  valid_conn_param_keys = PG::Connection.conndefaults_hash.keys + [:requiressl]
34
36
  conn_params.slice!(*valid_conn_param_keys)
35
37
 
36
- # The postgres drivers don't allow the creation of an unconnected PGconn object,
37
- # so just pass a nil connection object for the time being.
38
- ConnectionAdapters::PostGISAdapter.new(nil, logger, conn_params, config)
38
+ conn = PG.connect(conn_params)
39
+ ConnectionAdapters::PostGISAdapter.new(conn, logger, conn_params, config)
40
+ rescue ::PG::Error => error
41
+ if error.message.include?(conn_params[:dbname])
42
+ raise ActiveRecord::NoDatabaseError
43
+ else
44
+ raise
45
+ end
39
46
  end
40
47
 
41
48
  end
@@ -1,16 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  namespace :db do
2
4
  namespace :gis do
3
5
  desc "Setup PostGIS data in the database"
4
6
  task setup: [:load_config] do
5
7
  environments = [Rails.env]
6
8
  environments << "test" if Rails.env.development?
7
- ActiveRecord::Base.configurations
8
- .values_at(*environments)
9
- .compact
10
- .reject { |config| config["database"].blank? }
11
- .each do |config|
12
- ActiveRecord::ConnectionAdapters::PostGIS::PostGISDatabaseTasks.new(config).setup_gis
13
- end
9
+ environments.each do |environment|
10
+ ActiveRecord::Base.configurations
11
+ .configs_for(env_name: environment)
12
+ .reject { |env| env.config["database"].blank? }
13
+ .each do |env|
14
+ ActiveRecord::ConnectionAdapters::PostGIS::PostGISDatabaseTasks.new(env.config).setup_gis
15
+ end
16
+ end
14
17
  end
15
18
  end
16
19
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module ConnectionAdapters
3
5
  module PostGIS
@@ -25,23 +27,20 @@ module ActiveRecord
25
27
  geo_type, srid, has_z, has_m = nil, 0, false, false
26
28
 
27
29
  if sql_type =~ /(geography|geometry)\((.*)\)$/i
30
+ # geometry(Point)
28
31
  # geometry(Point,4326)
29
32
  params = Regexp.last_match(2).split(",")
30
- if params.size > 1
31
- if params.first =~ /([a-z]+[^zm])(z?)(m?)/i
32
- has_z = Regexp.last_match(2).length > 0
33
- has_m = Regexp.last_match(3).length > 0
34
- geo_type = Regexp.last_match(1)
35
- end
36
- if params.last =~ /(\d+)/
37
- srid = Regexp.last_match(1).to_i
38
- end
39
- else
40
- # geometry(Point)
41
- geo_type = params[0]
33
+ if params.first =~ /([a-z]+[^zm])(z?)(m?)/i
34
+ has_z = Regexp.last_match(2).length > 0
35
+ has_m = Regexp.last_match(3).length > 0
36
+ geo_type = Regexp.last_match(1)
37
+ end
38
+ if params.last =~ /(\d+)/
39
+ srid = Regexp.last_match(1).to_i
42
40
  end
43
41
  else
44
42
  # geometry
43
+ # otherType(a,b)
45
44
  geo_type = sql_type
46
45
  end
47
46
  [geo_type, srid, has_z, has_m]
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord # :nodoc:
2
4
  module ConnectionAdapters # :nodoc:
3
5
  module PostGIS # :nodoc:
@@ -106,7 +108,9 @@ module ActiveRecord # :nodoc:
106
108
  def setup_gis_from_extension
107
109
  extension_names.each do |extname|
108
110
  if extname == "postgis_topology"
109
- raise ::ArgumentError, "'topology' must be in schema_search_path for postgis_topology" unless search_path.include?("topology")
111
+ unless search_path.include?("topology")
112
+ raise ArgumentError, "'topology' must be in schema_search_path for postgis_topology"
113
+ end
110
114
  connection.execute("CREATE EXTENSION IF NOT EXISTS #{extname} SCHEMA topology")
111
115
  else
112
116
  if (postgis_schema = configuration["postgis_schema"])
@@ -125,7 +129,9 @@ module ActiveRecord # :nodoc:
125
129
  end
126
130
 
127
131
  def schema_exists?(schema_name)
128
- connection.execute("SELECT schema_name FROM information_schema.schemata WHERE schema_name = '#{schema_name}'").any?
132
+ connection.execute(
133
+ "SELECT schema_name FROM information_schema.schemata WHERE schema_name = '#{schema_name}'"
134
+ ).any?
129
135
  end
130
136
  end
131
137
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord # :nodoc:
2
4
  module ConnectionAdapters # :nodoc:
3
5
  module PostGIS # :nodoc:
@@ -1,45 +1,41 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module ConnectionAdapters
3
5
  module PostGIS
4
6
  module SchemaStatements
5
7
  # override
6
- # pass table_name to #new_column
7
- def columns(table_name)
8
- # Limit, precision, and scale are all handled by the superclass.
9
- column_definitions(table_name).map do |column_name, type, default, notnull, oid, fmod, collation|
10
- oid = oid.to_i
11
- fmod = fmod.to_i
12
- type_metadata = fetch_type_metadata(column_name, type, oid, fmod)
13
- cast_type = get_oid_type(oid.to_i, fmod.to_i, column_name, type)
14
- default_value = extract_value_from_default(default)
15
-
16
- default_function = extract_default_function(default_value, default)
17
- new_column(table_name, column_name, default_value, cast_type, type_metadata, !notnull, default_function, collation)
18
- end
19
- end
8
+ # https://github.com/rails/rails/blob/6-0-stable/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb#L624
9
+ # Create a SpatialColumn instead of a PostgreSQL::Column
10
+ def new_column_from_field(table_name, field)
11
+ column_name, type, default, notnull, oid, fmod, collation, comment = field
12
+ type_metadata = fetch_type_metadata(column_name, type, oid.to_i, fmod.to_i)
13
+ default_value = extract_value_from_default(default)
14
+ default_function = extract_default_function(default_value, default)
20
15
 
21
- # override
22
- def new_column(table_name, column_name, default, cast_type, sql_type_metadata = nil, null = true, default_function = nil, collation = nil)
23
- # JDBC gets true/false in Rails 4, where other platforms get 't'/'f' strings.
24
- if null.is_a?(String)
25
- null = (null == "t")
26
- end
16
+ serial =
17
+ if (match = default_function&.match(/\Anextval\('"?(?<sequence_name>.+_(?<suffix>seq\d*))"?'::regclass\)\z/))
18
+ sequence_name_from_parts(table_name, column_name, match[:suffix]) == match[:sequence_name]
19
+ end
27
20
 
28
- column_info = spatial_column_info(table_name).get(column_name, sql_type_metadata.sql_type)
21
+ # {:dimension=>2, :has_m=>false, :has_z=>false, :name=>"latlon", :srid=>0, :type=>"GEOMETRY"}
22
+ spatial = spatial_column_info(table_name).get(column_name, type_metadata.sql_type)
29
23
 
30
- SpatialColumn.new(column_name,
31
- default,
32
- sql_type_metadata,
33
- null,
34
- table_name,
35
- default_function,
36
- collation,
37
- cast_type,
38
- column_info)
24
+ SpatialColumn.new(
25
+ column_name,
26
+ default_value,
27
+ type_metadata,
28
+ !notnull,
29
+ default_function,
30
+ collation: collation,
31
+ comment: comment.presence,
32
+ serial: serial,
33
+ spatial: spatial
34
+ )
39
35
  end
40
36
 
41
37
  # override
42
- # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb#L583
38
+ # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb#L544
43
39
  #
44
40
  # returns Postgresql sql type string
45
41
  # examples:
@@ -49,12 +45,12 @@ module ActiveRecord
49
45
  # note: type alone is not enough to detect the sql type,
50
46
  # so `limit` is used to pass the additional information. :(
51
47
  #
52
- # type_to_sql(:geography, "Point,4326")
48
+ # type_to_sql(:geography, limit: "Point,4326")
53
49
  # => "geography(Point,4326)"
54
- def type_to_sql(type, options = {})
55
- case type
56
- when :geometry, :geography
57
- "#{type}(#{options[:limit]})"
50
+ def type_to_sql(type, limit: nil, precision: nil, scale: nil, array: nil, **)
51
+ case type.to_s
52
+ when "geometry", "geography"
53
+ "#{type}(#{limit})"
58
54
  else
59
55
  super
60
56
  end
@@ -78,8 +74,8 @@ module ActiveRecord
78
74
  end
79
75
 
80
76
  # override
81
- def create_table_definition(*args)
82
- PostGIS::TableDefinition.new(*args)
77
+ def create_table_definition(*args, **kwargs)
78
+ PostGIS::TableDefinition.new(self, *args, **kwargs)
83
79
  end
84
80
 
85
81
  # memoize hash of column infos for tables
@@ -89,8 +85,6 @@ module ActiveRecord
89
85
  end
90
86
 
91
87
  def initialize_type_map(map = type_map)
92
- super
93
-
94
88
  %w(
95
89
  geography
96
90
  geometry
@@ -106,6 +100,8 @@ module ActiveRecord
106
100
  OID::Spatial.new(oid, sql_type)
107
101
  end
108
102
  end
103
+
104
+ super
109
105
  end
110
106
  end
111
107
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord # :nodoc:
2
4
  module ConnectionAdapters # :nodoc:
3
5
  module PostGIS # :nodoc:
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord # :nodoc:
2
4
  module ConnectionAdapters # :nodoc:
3
5
  module PostGIS # :nodoc:
@@ -5,18 +7,17 @@ module ActiveRecord # :nodoc:
5
7
  # sql_type examples:
6
8
  # "Geometry(Point,4326)"
7
9
  # "Geography(Point,4326)"
8
- # cast_type example classes:
9
- # OID::Spatial
10
- # OID::Integer
11
- def initialize(name, default, sql_type_metadata = nil, null = true, table_name = nil, default_function = nil, collation = nil, cast_type = nil, opts = nil)
12
- @cast_type = cast_type
10
+ def initialize(name, default, sql_type_metadata = nil, null = true,
11
+ default_function = nil, collation: nil, comment: nil,
12
+ serial: nil, spatial: nil)
13
+ @sql_type_metadata = sql_type_metadata
13
14
  @geographic = !!(sql_type_metadata.sql_type =~ /geography\(/i)
14
- if opts
15
+ if spatial
15
16
  # This case comes from an entry in the geometry_columns table
16
- set_geometric_type_from_name(opts[:type])
17
- @srid = opts[:srid].to_i
18
- @has_z = !!opts[:has_z]
19
- @has_m = !!opts[:has_m]
17
+ set_geometric_type_from_name(spatial[:type])
18
+ @srid = spatial[:srid].to_i
19
+ @has_z = !!spatial[:has_z]
20
+ @has_m = !!spatial[:has_m]
20
21
  elsif @geographic
21
22
  # Geographic type information is embedded in the SQL type
22
23
  @srid = 4326
@@ -29,7 +30,8 @@ module ActiveRecord # :nodoc:
29
30
  # @geometric_type = geo_type_from_sql_type(sql_type)
30
31
  build_from_sql_type(sql_type_metadata.sql_type)
31
32
  end
32
- super(name, default, sql_type_metadata, null, table_name, default_function, collation)
33
+ super(name, default, sql_type_metadata, null, default_function,
34
+ collation: collation, comment: comment, serial: serial)
33
35
  if spatial?
34
36
  if @srid
35
37
  @limit = { srid: @srid, type: to_type_name(geometric_type) }
@@ -51,15 +53,11 @@ module ActiveRecord # :nodoc:
51
53
  alias :has_m? :has_m
52
54
 
53
55
  def limit
54
- if spatial?
55
- @limit
56
- else
57
- super
58
- end
56
+ spatial? ? @limit : super
59
57
  end
60
58
 
61
59
  def spatial?
62
- @cast_type.respond_to?(:spatial?) && @cast_type.spatial?
60
+ %i[geometry geography].include?(@sql_type_metadata.type)
63
61
  end
64
62
 
65
63
  private
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord # :nodoc:
2
4
  module ConnectionAdapters # :nodoc:
3
5
  module PostGIS
@@ -9,7 +11,9 @@ module ActiveRecord # :nodoc:
9
11
  end
10
12
 
11
13
  def all
12
- info = @adapter.query("SELECT f_geometry_column,coord_dimension,srid,type FROM geometry_columns WHERE f_table_name='#{@table_name}'")
14
+ info = @adapter.query(
15
+ "SELECT f_geometry_column,coord_dimension,srid,type FROM geometry_columns WHERE f_table_name='#{@table_name}'"
16
+ )
13
17
  result = {}
14
18
  info.each do |row|
15
19
  name = row[0]
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord # :nodoc:
2
4
  module ConnectionAdapters # :nodoc:
3
5
  module PostGIS # :nodoc:
@@ -5,7 +7,7 @@ module ActiveRecord # :nodoc:
5
7
  include ColumnMethods
6
8
 
7
9
  # super: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
8
- def new_column_definition(name, type, options)
10
+ def new_column_definition(name, type, **options)
9
11
  if (info = PostGISAdapter.spatial_column_options(type.to_sym))
10
12
  if (limit = options.delete(:limit))
11
13
  options.merge!(limit) if limit.is_a?(::Hash)
@@ -16,9 +18,9 @@ module ActiveRecord # :nodoc:
16
18
 
17
19
  options[:limit] = ColumnDefinitionUtils.limit_from_options(geo_type, options)
18
20
  options[:spatial_type] = geo_type
19
- column = super(name, base_type, options)
21
+ column = super(name, base_type, **options)
20
22
  else
21
- column = super(name, type, options)
23
+ column = super(name, type, **options)
22
24
  end
23
25
 
24
26
  column
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module ConnectionAdapters
3
5
  module PostGIS
4
- VERSION = "5.1.0"
6
+ VERSION = "6.0.1"
5
7
  end
6
8
  end
7
9
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # The activerecord-postgis-adapter gem installs the *postgis*
2
4
  # connection adapter into ActiveRecord.
3
5
 
@@ -36,7 +38,7 @@ end
36
38
  module ActiveRecord
37
39
  module ConnectionAdapters
38
40
  class PostGISAdapter < PostgreSQLAdapter
39
- include PostGIS::SchemaStatements
41
+ ADAPTER_NAME = 'PostGIS'.freeze
40
42
 
41
43
  SPATIAL_COLUMN_OPTIONS =
42
44
  {
@@ -55,22 +57,10 @@ module ActiveRecord
55
57
  # http://postgis.17.x6.nabble.com/Default-SRID-td5001115.html
56
58
  DEFAULT_SRID = 0
57
59
 
58
- # def initialize(*args)
59
- def initialize(connection, logger, connection_parameters, config)
60
- super
61
-
62
- @visitor = Arel::Visitors::PostGIS.new(self)
63
- # copy from https://github.com/rails/rails/blob/6ece7df8d80c6d93db43878fa4c0278a0204072c/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L199
64
- if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true })
65
- @prepared_statements = true
66
- @visitor.extend(DetermineIfPreparableVisitor)
67
- else
68
- @prepared_statements = false
69
- end
70
- end
60
+ include PostGIS::SchemaStatements
71
61
 
72
- def adapter_name
73
- "PostGIS".freeze
62
+ def arel_visitor # :nodoc:
63
+ Arel::Visitors::PostGIS.new(self)
74
64
  end
75
65
 
76
66
  def self.spatial_column_options(key)
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_record/connection_adapters/postgis_adapter"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-postgis-adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.1.0
4
+ version: 6.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Azuma, Tee Parham
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-02 00:00:00.000000000 Z
11
+ date: 2020-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '5.1'
19
+ version: '6.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '5.1'
26
+ version: '6.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rgeo-activerecord
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -130,15 +130,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
130
130
  requirements:
131
131
  - - ">="
132
132
  - !ruby/object:Gem::Version
133
- version: 2.2.2
133
+ version: 2.5.0
134
134
  required_rubygems_version: !ruby/object:Gem::Requirement
135
135
  requirements:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
139
  requirements: []
140
- rubyforge_project:
141
- rubygems_version: 2.7.0
140
+ rubygems_version: 3.1.2
142
141
  signing_key:
143
142
  specification_version: 4
144
143
  summary: ActiveRecord adapter for PostGIS, based on RGeo.