activerecord-postgis-adapter 7.1.1 → 8.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/active_record/connection_adapters/postgis/oid/date_time.rb +20 -0
- data/lib/active_record/connection_adapters/postgis/oid/spatial.rb +1 -1
- data/lib/active_record/connection_adapters/postgis/schema_statements.rb +12 -73
- data/lib/active_record/connection_adapters/postgis/spatial_column.rb +10 -11
- data/lib/active_record/connection_adapters/postgis/spatial_column_info.rb +1 -1
- data/lib/active_record/connection_adapters/postgis/spatial_table_definition.rb +9 -3
- data/lib/active_record/connection_adapters/postgis/version.rb +1 -1
- data/lib/active_record/connection_adapters/postgis_adapter.rb +58 -9
- metadata +28 -16
- data/lib/active_record/connection_adapters/postgis/databases.rake +0 -19
- data/lib/active_record/connection_adapters/postgis/postgis_database_tasks.rb +0 -142
- data/lib/active_record/connection_adapters/postgis/railtie.rb +0 -13
- data/lib/active_record/connection_adapters/postgis/setup.rb +0 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a24c165231e4b03b66bac3cc34446bc6683e960a7cfc5dd0e9cdc83aa9662b81
|
4
|
+
data.tar.gz: 1f8267c7469590eb14e12312e311e195c87504d3a1a64a9fe79392d50a9dab2f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 976f8ee3f628fcacffea4e7890f5c4f29d2308c3aa28bb070d7d547931767dce49438286f0be1dcb1424b9e3f38f5fb260f6c51750062755b0124416f5387064
|
7
|
+
data.tar.gz: 2012d45e2fb4603c11657a9a7d9a907d93e3202200eddc990bedd1a6a0e73680d9064838d248dabbc7c244d4c70ae461e49aa6c74adc96605abbf5b444eae0ba
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
module PostGIS
|
6
|
+
module OID
|
7
|
+
module DateTime
|
8
|
+
protected
|
9
|
+
|
10
|
+
# Uses PostGIS instead of PostgreSQLAdapter
|
11
|
+
def real_type_unless_aliased(real_type)
|
12
|
+
ActiveRecord::ConnectionAdapters::PostGISAdapter.datetime_type == real_type ? :datetime : real_type
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
PostgreSQL::OID::DateTime.prepend(DateTime)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -9,7 +9,7 @@ module ActiveRecord
|
|
9
9
|
# Accepts `geo_type`, `srid`, `has_z`, `has_m`, and `geographic` as parameters.
|
10
10
|
# Responsible for parsing sql_types returned from the database and WKT features.
|
11
11
|
class Spatial < Type::Value
|
12
|
-
def initialize(geo_type:
|
12
|
+
def initialize(geo_type: "geometry", srid: 0, has_z: false, has_m: false, geographic: false)
|
13
13
|
@geo_type = geo_type
|
14
14
|
@srid = srid
|
15
15
|
@has_z = has_z
|
@@ -5,18 +5,22 @@ module ActiveRecord
|
|
5
5
|
module PostGIS
|
6
6
|
module SchemaStatements
|
7
7
|
# override
|
8
|
-
# https://github.com/rails/rails/blob/
|
8
|
+
# https://github.com/rails/rails/blob/7-0-stable/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb#L662
|
9
9
|
# Create a SpatialColumn instead of a PostgreSQL::Column
|
10
10
|
def new_column_from_field(table_name, field)
|
11
|
-
column_name, type, default, notnull, oid, fmod, collation, comment = field
|
11
|
+
column_name, type, default, notnull, oid, fmod, collation, comment, attgenerated = field
|
12
12
|
type_metadata = fetch_type_metadata(column_name, type, oid.to_i, fmod.to_i)
|
13
13
|
default_value = extract_value_from_default(default)
|
14
|
-
default_function = extract_default_function(default_value, default)
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
15
|
+
if attgenerated.present?
|
16
|
+
default_function = default
|
17
|
+
else
|
18
|
+
default_function = extract_default_function(default_value, default)
|
19
|
+
end
|
20
|
+
|
21
|
+
if match = default_function&.match(/\Anextval\('"?(?<sequence_name>.+_(?<suffix>seq\d*))"?'::regclass\)\z/)
|
22
|
+
serial = sequence_name_from_parts(table_name, column_name, match[:suffix]) == match[:sequence_name]
|
23
|
+
end
|
20
24
|
|
21
25
|
# {:dimension=>2, :has_m=>false, :has_z=>false, :name=>"latlon", :srid=>0, :type=>"GEOMETRY"}
|
22
26
|
spatial = spatial_column_info(table_name).get(column_name, type_metadata.sql_type)
|
@@ -30,49 +34,11 @@ module ActiveRecord
|
|
30
34
|
collation: collation,
|
31
35
|
comment: comment.presence,
|
32
36
|
serial: serial,
|
37
|
+
generated: attgenerated,
|
33
38
|
spatial: spatial
|
34
39
|
)
|
35
40
|
end
|
36
41
|
|
37
|
-
# override
|
38
|
-
# https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb#L544
|
39
|
-
#
|
40
|
-
# returns Postgresql sql type string
|
41
|
-
# examples:
|
42
|
-
# "geometry(Point,4326)"
|
43
|
-
# "geography(Point,4326)"
|
44
|
-
#
|
45
|
-
# note: type alone is not enough to detect the sql type,
|
46
|
-
# so `limit` is used to pass the additional information. :(
|
47
|
-
#
|
48
|
-
# type_to_sql(:geography, limit: "Point,4326")
|
49
|
-
# => "geography(Point,4326)"
|
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})"
|
54
|
-
else
|
55
|
-
super
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
# override
|
60
|
-
def native_database_types
|
61
|
-
# Add spatial types
|
62
|
-
super.merge(
|
63
|
-
geography: { name: "geography" },
|
64
|
-
geometry: { name: "geometry" },
|
65
|
-
geometry_collection: { name: "geometry_collection" },
|
66
|
-
line_string: { name: "line_string" },
|
67
|
-
multi_line_string: { name: "multi_line_string" },
|
68
|
-
multi_point: { name: "multi_point" },
|
69
|
-
multi_polygon: { name: "multi_polygon" },
|
70
|
-
spatial: { name: "geometry" },
|
71
|
-
st_point: { name: "st_point" },
|
72
|
-
st_polygon: { name: "st_polygon" }
|
73
|
-
)
|
74
|
-
end
|
75
|
-
|
76
42
|
# override
|
77
43
|
def create_table_definition(*args, **kwargs)
|
78
44
|
PostGIS::TableDefinition.new(self, *args, **kwargs)
|
@@ -83,33 +49,6 @@ module ActiveRecord
|
|
83
49
|
@spatial_column_info ||= {}
|
84
50
|
@spatial_column_info[table_name.to_sym] ||= SpatialColumnInfo.new(self, table_name.to_s)
|
85
51
|
end
|
86
|
-
|
87
|
-
def initialize_type_map(map = type_map)
|
88
|
-
%w(
|
89
|
-
geography
|
90
|
-
geometry
|
91
|
-
geometry_collection
|
92
|
-
line_string
|
93
|
-
multi_line_string
|
94
|
-
multi_point
|
95
|
-
multi_polygon
|
96
|
-
st_point
|
97
|
-
st_polygon
|
98
|
-
).each do |geo_type|
|
99
|
-
map.register_type(geo_type) do |_, _, sql_type|
|
100
|
-
# sql_type is a string that comes from the database definition
|
101
|
-
# examples:
|
102
|
-
# "geometry(Point,4326)"
|
103
|
-
# "geography(Point,4326)"
|
104
|
-
# "geometry(Polygon,4326) NOT NULL"
|
105
|
-
# "geometry(Geography,4326)"
|
106
|
-
geo_type, srid, has_z, has_m, geographic = OID::Spatial.parse_sql_type(sql_type)
|
107
|
-
OID::Spatial.new(geo_type: geo_type, srid: srid, has_z: has_z, has_m: has_m, geographic: geographic)
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
super
|
112
|
-
end
|
113
52
|
end
|
114
53
|
end
|
115
54
|
end
|
@@ -9,7 +9,7 @@ module ActiveRecord # :nodoc:
|
|
9
9
|
# "Geography(Point,4326)"
|
10
10
|
def initialize(name, default, sql_type_metadata = nil, null = true,
|
11
11
|
default_function = nil, collation: nil, comment: nil,
|
12
|
-
serial: nil, spatial: nil)
|
12
|
+
serial: nil, generated: nil, spatial: nil)
|
13
13
|
@sql_type_metadata = sql_type_metadata
|
14
14
|
@geographic = !!(sql_type_metadata.sql_type =~ /geography\(/i)
|
15
15
|
if spatial
|
@@ -31,14 +31,12 @@ module ActiveRecord # :nodoc:
|
|
31
31
|
build_from_sql_type(sql_type_metadata.sql_type)
|
32
32
|
end
|
33
33
|
super(name, default, sql_type_metadata, null, default_function,
|
34
|
-
collation: collation, comment: comment, serial: serial)
|
35
|
-
if spatial?
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
@limit[:geographic] = true if @geographic
|
41
|
-
end
|
34
|
+
collation: collation, comment: comment, serial: serial, generated: generated)
|
35
|
+
if spatial? && @srid
|
36
|
+
@limit = { srid: @srid, type: to_type_name(geometric_type) }
|
37
|
+
@limit[:has_z] = true if @has_z
|
38
|
+
@limit[:has_m] = true if @has_m
|
39
|
+
@limit[:geographic] = true if @geographic
|
42
40
|
end
|
43
41
|
end
|
44
42
|
|
@@ -73,9 +71,10 @@ module ActiveRecord # :nodoc:
|
|
73
71
|
|
74
72
|
def to_type_name(geometric_type)
|
75
73
|
name = geometric_type.type_name.underscore
|
76
|
-
|
74
|
+
case name
|
75
|
+
when "point"
|
77
76
|
"st_point"
|
78
|
-
|
77
|
+
when "polygon"
|
79
78
|
"st_polygon"
|
80
79
|
else
|
81
80
|
name
|
@@ -21,7 +21,7 @@ module ActiveRecord # :nodoc:
|
|
21
21
|
dimension = row[1].to_i
|
22
22
|
has_m = !!(type =~ /m$/i)
|
23
23
|
type.sub!(/m$/, "")
|
24
|
-
has_z = dimension > 3 || dimension == 3 && !has_m
|
24
|
+
has_z = dimension > 3 || (dimension == 3 && !has_m)
|
25
25
|
result[name] = {
|
26
26
|
dimension: dimension,
|
27
27
|
has_m: has_m,
|
@@ -8,9 +8,15 @@ module ActiveRecord # :nodoc:
|
|
8
8
|
|
9
9
|
# super: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
|
10
10
|
def new_column_definition(name, type, **options)
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
col_type = if type.to_sym == :virtual
|
12
|
+
options[:type]
|
13
|
+
else
|
14
|
+
type
|
15
|
+
end
|
16
|
+
|
17
|
+
if (info = PostGISAdapter.spatial_column_options(col_type))
|
18
|
+
if (limit = options.delete(:limit)) && limit.is_a?(::Hash)
|
19
|
+
options.merge!(limit)
|
14
20
|
end
|
15
21
|
|
16
22
|
geo_type = ColumnDefinitionUtils.geo_type(options[:type] || type || info[:type])
|
@@ -17,18 +17,10 @@ require "active_record/connection_adapters/postgis/spatial_column_info"
|
|
17
17
|
require "active_record/connection_adapters/postgis/spatial_table_definition"
|
18
18
|
require "active_record/connection_adapters/postgis/spatial_column"
|
19
19
|
require "active_record/connection_adapters/postgis/arel_tosql"
|
20
|
-
require "active_record/connection_adapters/postgis/setup"
|
21
20
|
require "active_record/connection_adapters/postgis/oid/spatial"
|
21
|
+
require "active_record/connection_adapters/postgis/oid/date_time"
|
22
22
|
require "active_record/connection_adapters/postgis/type" # has to be after oid/*
|
23
23
|
require "active_record/connection_adapters/postgis/create_connection"
|
24
|
-
require "active_record/connection_adapters/postgis/postgis_database_tasks"
|
25
|
-
|
26
|
-
ActiveRecord::ConnectionAdapters::PostGIS.initial_setup
|
27
|
-
|
28
|
-
if defined?(Rails::Railtie)
|
29
|
-
require "active_record/connection_adapters/postgis/railtie"
|
30
|
-
end
|
31
|
-
|
32
24
|
# :startdoc:
|
33
25
|
|
34
26
|
module ActiveRecord
|
@@ -72,6 +64,53 @@ module ActiveRecord
|
|
72
64
|
DEFAULT_SRID
|
73
65
|
end
|
74
66
|
|
67
|
+
class << self
|
68
|
+
def initialize_type_map(map = type_map)
|
69
|
+
%w[
|
70
|
+
geography
|
71
|
+
geometry
|
72
|
+
geometry_collection
|
73
|
+
line_string
|
74
|
+
multi_line_string
|
75
|
+
multi_point
|
76
|
+
multi_polygon
|
77
|
+
st_point
|
78
|
+
st_polygon
|
79
|
+
].each do |geo_type|
|
80
|
+
map.register_type(geo_type) do |_, _, sql_type|
|
81
|
+
# sql_type is a string that comes from the database definition
|
82
|
+
# examples:
|
83
|
+
# "geometry(Point,4326)"
|
84
|
+
# "geography(Point,4326)"
|
85
|
+
# "geometry(Polygon,4326) NOT NULL"
|
86
|
+
# "geometry(Geography,4326)"
|
87
|
+
geo_type, srid, has_z, has_m, geographic = PostGIS::OID::Spatial.parse_sql_type(sql_type)
|
88
|
+
PostGIS::OID::Spatial.new(geo_type: geo_type, srid: srid, has_z: has_z, has_m: has_m, geographic: geographic)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
super
|
93
|
+
end
|
94
|
+
|
95
|
+
def native_database_types
|
96
|
+
@native_database_types ||= begin
|
97
|
+
default_types = PostgreSQLAdapter.native_database_types
|
98
|
+
default_types.merge({
|
99
|
+
geography: { name: "geography" },
|
100
|
+
geometry: { name: "geometry" },
|
101
|
+
geometry_collection: { name: "geometry_collection" },
|
102
|
+
line_string: { name: "line_string" },
|
103
|
+
multi_line_string: { name: "multi_line_string" },
|
104
|
+
multi_point: { name: "multi_point" },
|
105
|
+
multi_polygon: { name: "multi_polygon" },
|
106
|
+
spatial: { name: "geometry" },
|
107
|
+
st_point: { name: "st_point" },
|
108
|
+
st_polygon: { name: "st_polygon" }
|
109
|
+
})
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
75
114
|
def srs_database_columns
|
76
115
|
{
|
77
116
|
auth_name_column: "auth_name",
|
@@ -115,6 +154,16 @@ module ActiveRecord
|
|
115
154
|
end
|
116
155
|
end
|
117
156
|
end
|
157
|
+
SchemaDumper.ignore_tables |= %w[
|
158
|
+
geography_columns
|
159
|
+
geometry_columns
|
160
|
+
layer
|
161
|
+
raster_columns
|
162
|
+
raster_overviews
|
163
|
+
spatial_ref_sys
|
164
|
+
topology
|
165
|
+
]
|
166
|
+
Tasks::DatabaseTasks.register_task(/postgis/, "ActiveRecord::Tasks::PostgreSQLDatabaseTasks")
|
118
167
|
end
|
119
168
|
|
120
169
|
# if using JRUBY, create ArJdbc::PostGIS module
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-postgis-adapter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 8.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Azuma
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2023-10-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -17,14 +17,14 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - "~>"
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version:
|
20
|
+
version: 7.0.0
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - "~>"
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version:
|
27
|
+
version: 7.0.0
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: rgeo-activerecord
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -45,14 +45,14 @@ dependencies:
|
|
45
45
|
requirements:
|
46
46
|
- - "~>"
|
47
47
|
- !ruby/object:Gem::Version
|
48
|
-
version: '
|
48
|
+
version: '13.0'
|
49
49
|
type: :development
|
50
50
|
prerelease: false
|
51
51
|
version_requirements: !ruby/object:Gem::Requirement
|
52
52
|
requirements:
|
53
53
|
- - "~>"
|
54
54
|
- !ruby/object:Gem::Version
|
55
|
-
version: '
|
55
|
+
version: '13.0'
|
56
56
|
- !ruby/object:Gem::Dependency
|
57
57
|
name: minitest
|
58
58
|
requirement: !ruby/object:Gem::Requirement
|
@@ -82,19 +82,33 @@ dependencies:
|
|
82
82
|
- !ruby/object:Gem::Version
|
83
83
|
version: '1.1'
|
84
84
|
- !ruby/object:Gem::Dependency
|
85
|
-
name:
|
85
|
+
name: benchmark-ips
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - "~>"
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: 2.9.1
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - "~>"
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: 2.9.1
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: rubocop
|
86
100
|
requirement: !ruby/object:Gem::Requirement
|
87
101
|
requirements:
|
88
102
|
- - "~>"
|
89
103
|
- !ruby/object:Gem::Version
|
90
|
-
version: '
|
104
|
+
version: '1.50'
|
91
105
|
type: :development
|
92
106
|
prerelease: false
|
93
107
|
version_requirements: !ruby/object:Gem::Requirement
|
94
108
|
requirements:
|
95
109
|
- - "~>"
|
96
110
|
- !ruby/object:Gem::Version
|
97
|
-
version: '
|
111
|
+
version: '1.50'
|
98
112
|
description: ActiveRecord connection adapter for PostGIS. It is based on the stock
|
99
113
|
PostgreSQL adapter, and adds built-in support for the spatial extensions provided
|
100
114
|
by PostGIS. It uses the RGeo library to represent spatial data in Ruby.
|
@@ -111,12 +125,9 @@ files:
|
|
111
125
|
- lib/active_record/connection_adapters/postgis/column_methods.rb
|
112
126
|
- lib/active_record/connection_adapters/postgis/create_connection.rb
|
113
127
|
- lib/active_record/connection_adapters/postgis/database_statements.rb
|
114
|
-
- lib/active_record/connection_adapters/postgis/
|
128
|
+
- lib/active_record/connection_adapters/postgis/oid/date_time.rb
|
115
129
|
- lib/active_record/connection_adapters/postgis/oid/spatial.rb
|
116
|
-
- lib/active_record/connection_adapters/postgis/postgis_database_tasks.rb
|
117
|
-
- lib/active_record/connection_adapters/postgis/railtie.rb
|
118
130
|
- lib/active_record/connection_adapters/postgis/schema_statements.rb
|
119
|
-
- lib/active_record/connection_adapters/postgis/setup.rb
|
120
131
|
- lib/active_record/connection_adapters/postgis/spatial_column.rb
|
121
132
|
- lib/active_record/connection_adapters/postgis/spatial_column_info.rb
|
122
133
|
- lib/active_record/connection_adapters/postgis/spatial_table_definition.rb
|
@@ -127,7 +138,8 @@ files:
|
|
127
138
|
homepage: http://github.com/rgeo/activerecord-postgis-adapter
|
128
139
|
licenses:
|
129
140
|
- BSD-3-Clause
|
130
|
-
metadata:
|
141
|
+
metadata:
|
142
|
+
rubygems_mfa_required: 'true'
|
131
143
|
post_install_message:
|
132
144
|
rdoc_options: []
|
133
145
|
require_paths:
|
@@ -136,14 +148,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
136
148
|
requirements:
|
137
149
|
- - ">="
|
138
150
|
- !ruby/object:Gem::Version
|
139
|
-
version: 2.
|
151
|
+
version: 2.7.0
|
140
152
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
153
|
requirements:
|
142
154
|
- - ">="
|
143
155
|
- !ruby/object:Gem::Version
|
144
156
|
version: '0'
|
145
157
|
requirements: []
|
146
|
-
rubygems_version: 3.
|
158
|
+
rubygems_version: 3.2.33
|
147
159
|
signing_key:
|
148
160
|
specification_version: 4
|
149
161
|
summary: ActiveRecord adapter for PostGIS, based on RGeo.
|
@@ -1,19 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
namespace :db do
|
4
|
-
namespace :gis do
|
5
|
-
desc 'Setup PostGIS data in the database'
|
6
|
-
task setup: [:load_config] do
|
7
|
-
environments = [Rails.env]
|
8
|
-
environments << 'test' if Rails.env.development?
|
9
|
-
environments.each do |environment|
|
10
|
-
ActiveRecord::Base.configurations
|
11
|
-
.configs_for(env_name: environment)
|
12
|
-
.reject { |env| env.configuration_hash[:database].blank? }
|
13
|
-
.each do |env|
|
14
|
-
ActiveRecord::ConnectionAdapters::PostGIS::PostGISDatabaseTasks.new(env).setup_gis
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
@@ -1,142 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ActiveRecord # :nodoc:
|
4
|
-
module ConnectionAdapters # :nodoc:
|
5
|
-
module PostGIS # :nodoc:
|
6
|
-
class PostGISDatabaseTasks < ::ActiveRecord::Tasks::PostgreSQLDatabaseTasks # :nodoc:
|
7
|
-
def initialize(db_config)
|
8
|
-
super
|
9
|
-
ensure_installation_configs
|
10
|
-
end
|
11
|
-
|
12
|
-
def setup_gis
|
13
|
-
establish_su_connection
|
14
|
-
if extension_names
|
15
|
-
setup_gis_from_extension
|
16
|
-
end
|
17
|
-
establish_connection(db_config)
|
18
|
-
end
|
19
|
-
|
20
|
-
# Override to set the database owner and call setup_gis
|
21
|
-
def create(master_established = false)
|
22
|
-
establish_master_connection unless master_established
|
23
|
-
extra_configs = { encoding: encoding }
|
24
|
-
extra_configs[:owner] = username if has_su?
|
25
|
-
connection.create_database(db_config.database, configuration_hash.merge(extra_configs))
|
26
|
-
setup_gis
|
27
|
-
rescue ::ActiveRecord::StatementInvalid => error
|
28
|
-
if /database .* already exists/ === error.message
|
29
|
-
raise ::ActiveRecord::DatabaseAlreadyExists
|
30
|
-
else
|
31
|
-
raise
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
private
|
36
|
-
|
37
|
-
# Override to use su_username and su_password
|
38
|
-
def establish_master_connection
|
39
|
-
establish_connection(configuration_hash.merge(
|
40
|
-
database: "postgres",
|
41
|
-
password: su_password,
|
42
|
-
schema_search_path: "public",
|
43
|
-
username: su_username
|
44
|
-
))
|
45
|
-
end
|
46
|
-
|
47
|
-
def establish_su_connection
|
48
|
-
establish_connection(configuration_hash.merge(
|
49
|
-
password: su_password,
|
50
|
-
schema_search_path: "public",
|
51
|
-
username: su_username
|
52
|
-
))
|
53
|
-
end
|
54
|
-
|
55
|
-
def username
|
56
|
-
@username ||= configuration_hash[:username]
|
57
|
-
end
|
58
|
-
|
59
|
-
def quoted_username
|
60
|
-
@quoted_username ||= ::ActiveRecord::Base.connection.quote_column_name(username)
|
61
|
-
end
|
62
|
-
|
63
|
-
def password
|
64
|
-
@password ||= configuration_hash[:password]
|
65
|
-
end
|
66
|
-
|
67
|
-
def su_username
|
68
|
-
@su_username ||= configuration_hash.fetch(:su_username, username)
|
69
|
-
end
|
70
|
-
|
71
|
-
def su_password
|
72
|
-
@su_password ||= configuration_hash.fetch(:su_password, password)
|
73
|
-
end
|
74
|
-
|
75
|
-
def has_su?
|
76
|
-
return @has_su if defined?(@has_su)
|
77
|
-
|
78
|
-
@has_su = configuration_hash.include?(:su_username)
|
79
|
-
end
|
80
|
-
|
81
|
-
def search_path
|
82
|
-
@search_path ||= configuration_hash[:schema_search_path].to_s.strip.split(",").map(&:strip)
|
83
|
-
end
|
84
|
-
|
85
|
-
def extension_names
|
86
|
-
@extension_names ||= begin
|
87
|
-
extensions = configuration_hash[:postgis_extension]
|
88
|
-
case extensions
|
89
|
-
when ::String
|
90
|
-
extensions.split(",")
|
91
|
-
when ::Array
|
92
|
-
extensions
|
93
|
-
else
|
94
|
-
["postgis"]
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
def ensure_installation_configs
|
100
|
-
if configuration_hash[:setup] == "default" && !configuration_hash[:postgis_extension]
|
101
|
-
share_dir = `pg_config --sharedir`.strip rescue "/usr/share"
|
102
|
-
control_file = ::File.expand_path("extension/postgis.control", share_dir)
|
103
|
-
if ::File.readable?(control_file)
|
104
|
-
@configuration_hash = configuration_hash.merge(postgis_extension: "postgis").freeze
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
def setup_gis_from_extension
|
110
|
-
extension_names.each do |extname|
|
111
|
-
if extname == "postgis_topology"
|
112
|
-
unless search_path.include?("topology")
|
113
|
-
raise ArgumentError, "'topology' must be in schema_search_path for postgis_topology"
|
114
|
-
end
|
115
|
-
connection.execute("CREATE EXTENSION IF NOT EXISTS #{extname} SCHEMA topology")
|
116
|
-
else
|
117
|
-
if (postgis_schema = configuration_hash[:postgis_schema])
|
118
|
-
schema_clause = "WITH SCHEMA #{postgis_schema}"
|
119
|
-
unless schema_exists?(postgis_schema)
|
120
|
-
connection.execute("CREATE SCHEMA #{postgis_schema}")
|
121
|
-
connection.execute("GRANT ALL ON SCHEMA #{postgis_schema} TO PUBLIC")
|
122
|
-
end
|
123
|
-
else
|
124
|
-
schema_clause = ""
|
125
|
-
end
|
126
|
-
|
127
|
-
connection.execute("CREATE EXTENSION IF NOT EXISTS #{extname} #{schema_clause}")
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
def schema_exists?(schema_name)
|
133
|
-
connection.execute(
|
134
|
-
"SELECT schema_name FROM information_schema.schemata WHERE schema_name = '#{schema_name}'"
|
135
|
-
).any?
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
::ActiveRecord::Tasks::DatabaseTasks.register_task(/postgis/, PostGISDatabaseTasks)
|
140
|
-
end
|
141
|
-
end
|
142
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ActiveRecord # :nodoc:
|
4
|
-
module ConnectionAdapters # :nodoc:
|
5
|
-
module PostGIS # :nodoc:
|
6
|
-
class Railtie < ::Rails::Railtie # :nodoc:
|
7
|
-
rake_tasks do
|
8
|
-
load "active_record/connection_adapters/postgis/databases.rake"
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ActiveRecord # :nodoc:
|
4
|
-
module ConnectionAdapters # :nodoc:
|
5
|
-
module PostGIS # :nodoc:
|
6
|
-
def self.initial_setup
|
7
|
-
::ActiveRecord::SchemaDumper.ignore_tables |= %w(
|
8
|
-
geography_columns
|
9
|
-
geometry_columns
|
10
|
-
layer
|
11
|
-
raster_columns
|
12
|
-
raster_overviews
|
13
|
-
spatial_ref_sys
|
14
|
-
topology
|
15
|
-
)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|