rs_spatial_adapter 1.2.0
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.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +197 -0
- data/VERSION +1 -0
- data/lib/spatial_adapter.rb +41 -0
- data/lib/spatial_adapter/common/raw_geom_info.rb +23 -0
- data/lib/spatial_adapter/common/schema_definitions.rb +11 -0
- data/lib/spatial_adapter/common/schema_dumper.rb +136 -0
- data/lib/spatial_adapter/common/spatial_column.rb +70 -0
- data/lib/spatial_adapter/common/table_definition.rb +14 -0
- data/lib/spatial_adapter/mysql.rb +98 -0
- data/lib/spatial_adapter/mysql2.rb +97 -0
- data/lib/spatial_adapter/postgresql.rb +388 -0
- data/lib/spatial_adapter/railtie.rb +13 -0
- data/rails/init.rb +11 -0
- data/spec/README.txt +22 -0
- data/spec/db/mysql2_raw.rb +70 -0
- data/spec/db/mysql_raw.rb +70 -0
- data/spec/db/postgis_raw.rb +190 -0
- data/spec/models/common.rb +65 -0
- data/spec/mysql/connection_adapter_spec.rb +106 -0
- data/spec/mysql/migration_spec.rb +64 -0
- data/spec/mysql/models_spec.rb +65 -0
- data/spec/mysql/schema_dumper_spec.rb +56 -0
- data/spec/mysql2/connection_adapter_spec.rb +106 -0
- data/spec/mysql2/migration_spec.rb +64 -0
- data/spec/mysql2/models_spec.rb +65 -0
- data/spec/mysql2/schema_dumper_spec.rb +56 -0
- data/spec/postgresql/connection_adapter_spec.rb +227 -0
- data/spec/postgresql/migration_spec.rb +365 -0
- data/spec/postgresql/models_spec.rb +221 -0
- data/spec/postgresql/schema_dumper_spec.rb +79 -0
- data/spec/shared_examples.rb +43 -0
- data/spec/spec_helper.rb +90 -0
- metadata +131 -0
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'shared_examples'
|
3
|
+
require 'spatial_adapter/mysql2'
|
4
|
+
require 'db/mysql2_raw'
|
5
|
+
require 'models/common'
|
6
|
+
|
7
|
+
describe "Spatially-enabled Models" do
|
8
|
+
before :each do
|
9
|
+
mysql2_connection
|
10
|
+
@connection = ActiveRecord::Base.connection
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "inserting records" do
|
14
|
+
it 'should save Point objects' do
|
15
|
+
model = PointModel.new(:extra => 'test', :geom => GeometryFactory.point)
|
16
|
+
@connection.should_receive(:insert_sql).with(Regexp.new(GeometryFactory.point.as_hex_wkb), anything(), anything(), anything(), anything())
|
17
|
+
model.save.should == true
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should save LineString objects' do
|
21
|
+
model = LineStringModel.new(:extra => 'test', :geom => GeometryFactory.line_string)
|
22
|
+
@connection.should_receive(:insert_sql).with(Regexp.new(GeometryFactory.line_string.as_hex_wkb), anything(), anything(), anything(), anything())
|
23
|
+
model.save.should == true
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should save Polygon objects' do
|
27
|
+
model = PolygonModel.new(:extra => 'test', :geom => GeometryFactory.polygon)
|
28
|
+
@connection.should_receive(:insert_sql).with(Regexp.new(GeometryFactory.polygon.as_hex_wkb), anything(), anything(), anything(), anything())
|
29
|
+
model.save.should == true
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should save MultiPoint objects' do
|
33
|
+
model = MultiPointModel.new(:extra => 'test', :geom => GeometryFactory.multi_point)
|
34
|
+
@connection.should_receive(:insert_sql).with(Regexp.new(GeometryFactory.multi_point.as_hex_wkb), anything(), anything(), anything(), anything())
|
35
|
+
model.save.should == true
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should save MultiLineString objects' do
|
39
|
+
model = MultiLineStringModel.new(:extra => 'test', :geom => GeometryFactory.multi_line_string)
|
40
|
+
@connection.should_receive(:insert_sql).with(Regexp.new(GeometryFactory.multi_line_string.as_hex_wkb), anything(), anything(), anything(), anything())
|
41
|
+
model.save.should == true
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should save MultiPolygon objects' do
|
45
|
+
model = MultiPolygonModel.new(:extra => 'test', :geom => GeometryFactory.multi_polygon)
|
46
|
+
@connection.should_receive(:insert_sql).with(Regexp.new(GeometryFactory.multi_polygon.as_hex_wkb), anything(), anything(), anything(), anything())
|
47
|
+
model.save.should == true
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should save GeometryCollection objects' do
|
51
|
+
model = GeometryCollectionModel.new(:extra => 'test', :geom => GeometryFactory.geometry_collection)
|
52
|
+
@connection.should_receive(:insert_sql).with(Regexp.new(GeometryFactory.geometry_collection.as_hex_wkb), anything(), anything(), anything(), anything())
|
53
|
+
model.save.should == true
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should save Geometry objects' do
|
57
|
+
model = GeometryModel.new(:extra => 'test', :geom => GeometryFactory.point)
|
58
|
+
@connection.should_receive(:insert_sql).with(Regexp.new(GeometryFactory.point.as_hex_wkb), anything(), anything(), anything(), anything())
|
59
|
+
model.save.should == true
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
include CommonModelActions
|
64
|
+
end
|
65
|
+
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'spatial_adapter/mysql2'
|
3
|
+
|
4
|
+
describe "Spatially-enabled Schema Dumps" do
|
5
|
+
before :all do
|
6
|
+
mysql2_connection
|
7
|
+
@connection = ActiveRecord::Base.connection
|
8
|
+
|
9
|
+
# Create a new table
|
10
|
+
ActiveRecord::Schema.define do
|
11
|
+
create_table :migrated_geometry_models, :options=> "ENGINE=MyISAM", :force => true do |t|
|
12
|
+
t.integer :extra
|
13
|
+
t.point :geom, :null => false
|
14
|
+
end
|
15
|
+
add_index :migrated_geometry_models, :geom, :spatial => true, :name => 'test_spatial_index'
|
16
|
+
end
|
17
|
+
|
18
|
+
File.open('schema.rb', "w") do |file|
|
19
|
+
ActiveRecord::SchemaDumper.dump(@connection, file)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Drop the original table
|
23
|
+
@connection.drop_table "migrated_geometry_models"
|
24
|
+
|
25
|
+
# Load the dumped schema
|
26
|
+
load('schema.rb')
|
27
|
+
end
|
28
|
+
|
29
|
+
after :all do
|
30
|
+
# delete the schema file
|
31
|
+
File.delete('schema.rb')
|
32
|
+
|
33
|
+
# Drop the new table
|
34
|
+
@connection.drop_table "migrated_geometry_models"
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should preserve spatial attributes of tables" do
|
38
|
+
columns = @connection.columns("migrated_geometry_models")
|
39
|
+
|
40
|
+
columns.should have(3).items
|
41
|
+
geom_column = columns.select{|c| c.name == 'geom'}.first
|
42
|
+
geom_column.should be_a(SpatialAdapter::SpatialColumn)
|
43
|
+
geom_column.geometry_type.should == :point
|
44
|
+
geom_column.type.should == :string
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should preserve spatial indexes" do
|
48
|
+
indexes = @connection.indexes("migrated_geometry_models")
|
49
|
+
|
50
|
+
indexes.should have(1).item
|
51
|
+
|
52
|
+
indexes.first.name.should == 'test_spatial_index'
|
53
|
+
indexes.first.columns.should == ["geom"]
|
54
|
+
indexes.first.spatial.should == true
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,227 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'spatial_adapter/postgresql'
|
3
|
+
require 'db/postgis_raw'
|
4
|
+
require 'models/common'
|
5
|
+
|
6
|
+
describe "Modified PostgreSQLAdapter" do
|
7
|
+
before :each do
|
8
|
+
postgis_connection
|
9
|
+
@connection = ActiveRecord::Base.connection
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#postgis_version' do
|
13
|
+
it 'should report a version number if PostGIS is installed' do
|
14
|
+
@connection.should_receive(:select_value).with('SELECT postgis_full_version()').and_return('POSTGIS="1.5.0" GEOS="3.2.0-CAPI-1.6.0" PROJ="Rel. 4.7.1, 23 September 2009" LIBXML="2.7.6" USE_STATS')
|
15
|
+
@connection.postgis_version.should_not be_nil
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should report nil if PostGIS is not installed' do
|
19
|
+
@connection.should_receive(:select_value).with('SELECT postgis_full_version()').and_raise(ActiveRecord::StatementInvalid)
|
20
|
+
@connection.postgis_version.should be_nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#postgis_major_version' do
|
25
|
+
it 'should be the first component of the version number' do
|
26
|
+
@connection.stub!(:postgis_version).and_return('1.5.0')
|
27
|
+
@connection.postgis_major_version.should == 1
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should be nil if PostGIS is not installed' do
|
31
|
+
@connection.stub!(:postgis_version).and_return(nil)
|
32
|
+
@connection.postgis_major_version.should be_nil
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#postgis_minor_version' do
|
37
|
+
it 'should be the second component of the version number' do
|
38
|
+
@connection.stub!(:postgis_version).and_return('1.5.0')
|
39
|
+
@connection.postgis_minor_version.should == 5
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should be nil if PostGIS is not installed' do
|
43
|
+
@connection.stub!(:postgis_version).and_return(nil)
|
44
|
+
@connection.postgis_minor_version.should be_nil
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '#spatial?' do
|
49
|
+
it 'should be true if PostGIS is installed' do
|
50
|
+
@connection.should_receive(:select_value).with('SELECT postgis_full_version()').and_return('POSTGIS="1.5.0" GEOS="3.2.0-CAPI-1.6.0" PROJ="Rel. 4.7.1, 23 September 2009" LIBXML="2.7.6" USE_STATS')
|
51
|
+
@connection.should be_spatial
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should be false if PostGIS is not installed' do
|
55
|
+
@connection.should_receive(:select_value).with('SELECT postgis_full_version()').and_raise(ActiveRecord::StatementInvalid)
|
56
|
+
@connection.should_not be_spatial
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe '#supports_geographic?' do
|
61
|
+
it "should be true for PostGIS version 1.5.0" do
|
62
|
+
@connection.stub!(:postgis_version).and_return('1.5.0')
|
63
|
+
@connection.supports_geographic?.should == true
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should be true for PostGIS newer than 1.5.0" do
|
67
|
+
@connection.stub!(:postgis_version).and_return('1.5.1')
|
68
|
+
@connection.supports_geographic?.should == true
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should be true for PostGIS older than 1.5.0" do
|
72
|
+
@connection.stub!(:postgis_version).and_return('1.4.0')
|
73
|
+
@connection.supports_geographic?.should == false
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "#columns" do
|
78
|
+
describe "type" do
|
79
|
+
it "should be a regular SpatialPostgreSQLColumn if column is a geometry data type" do
|
80
|
+
column = PointModel.columns.select{|c| c.name == 'geom'}.first
|
81
|
+
column.should be_a(ActiveRecord::ConnectionAdapters::SpatialPostgreSQLColumn)
|
82
|
+
column.geometry_type.should == :point
|
83
|
+
column.should_not be_geographic
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should be a geographic SpatialPostgreSQLColumn if column is a geography data type" do
|
87
|
+
column = GeographyPointModel.columns.select{|c| c.name == 'geom'}.first
|
88
|
+
column.should be_a(ActiveRecord::ConnectionAdapters::SpatialPostgreSQLColumn)
|
89
|
+
column.geometry_type.should == :point
|
90
|
+
column.should be_geographic
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should be PostgreSQLColumn if column is not a spatial data type" do
|
94
|
+
PointModel.columns.select{|c| c.name == 'extra'}.first.should be_a(ActiveRecord::ConnectionAdapters::PostgreSQLColumn)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe "@geometry_type" do
|
99
|
+
it "should be :point for geometry columns restricted to POINT types" do
|
100
|
+
PointModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :point
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should be :line_string for geometry columns restricted to LINESTRING types" do
|
104
|
+
LineStringModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :line_string
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should be :polygon for geometry columns restricted to POLYGON types" do
|
108
|
+
PolygonModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :polygon
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should be :multi_point for geometry columns restricted to MULTIPOINT types" do
|
112
|
+
MultiPointModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :multi_point
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should be :multi_line_string for geometry columns restricted to MULTILINESTRING types" do
|
116
|
+
MultiLineStringModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :multi_line_string
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should be :multi_polygon for geometry columns restricted to MULTIPOLYGON types" do
|
120
|
+
MultiPolygonModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :multi_polygon
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should be :geometry_collection for geometry columns restricted to GEOMETRYCOLLECTION types" do
|
124
|
+
GeometryCollectionModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :geometry_collection
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should be :geometry for geometry columns not restricted to a type" do
|
128
|
+
GeometryModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :geometry
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should be :point for geography columns restricted to POINT types" do
|
132
|
+
GeographyPointModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :point
|
133
|
+
end
|
134
|
+
|
135
|
+
it "should be :line_string for geography columns restricted to LINESTRING types" do
|
136
|
+
GeographyLineStringModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :line_string
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should be :polygon for geography columns restricted to POLYGON types" do
|
140
|
+
GeographyPolygonModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :polygon
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should be :multi_point for geography columns restricted to MULTIPOINT types" do
|
144
|
+
GeographyMultiPointModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :multi_point
|
145
|
+
end
|
146
|
+
|
147
|
+
it "should be :multi_line_string for geography columns restricted to MULTILINESTRING types" do
|
148
|
+
GeographyMultiLineStringModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :multi_line_string
|
149
|
+
end
|
150
|
+
|
151
|
+
it "should be :multi_polygon for geography columns restricted to MULTIPOLYGON types" do
|
152
|
+
GeographyMultiPolygonModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :multi_polygon
|
153
|
+
end
|
154
|
+
|
155
|
+
it "should be :geometry_collection for geography columns restricted to GEOMETRYCOLLECTION types" do
|
156
|
+
GeographyGeometryCollectionModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :geometry_collection
|
157
|
+
end
|
158
|
+
|
159
|
+
it "should be :geometry for geography columns not restricted to a type" do
|
160
|
+
GeographyModel.columns.select{|c| c.name == 'geom'}.first.geometry_type.should == :geometry
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
describe "#indexes" do
|
166
|
+
before :each do
|
167
|
+
@indexes = @connection.indexes('point_models')
|
168
|
+
end
|
169
|
+
|
170
|
+
it "should return an IndexDefinition for each index on the table" do
|
171
|
+
@indexes.should have(2).items
|
172
|
+
@indexes.each do |i|
|
173
|
+
i.should be_a(ActiveRecord::ConnectionAdapters::IndexDefinition)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
it "should indicate the correct columns in the index" do
|
178
|
+
@indexes.select{|i| i.name == 'index_point_models_on_geom'}.first.columns.should == ['geom']
|
179
|
+
@indexes.select{|i| i.name == 'index_point_models_on_extra'}.first.columns.should == ['extra', 'more_extra']
|
180
|
+
end
|
181
|
+
|
182
|
+
it "should be marked as spatial if a GiST index on a geometry column" do
|
183
|
+
@indexes.select{|i| i.name == 'index_point_models_on_geom'}.first.spatial.should == true
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should be marked as spatial if a GiST index on a geography column" do
|
187
|
+
@indexes = @connection.indexes('geography_point_models')
|
188
|
+
@indexes.select{|i| i.name == 'index_geography_point_models_on_geom'}.first.spatial.should == true
|
189
|
+
end
|
190
|
+
|
191
|
+
it "should not be marked as spatial if not a GiST index" do
|
192
|
+
@indexes.select{|i| i.name == 'index_point_models_on_extra'}.first.spatial.should == false
|
193
|
+
end
|
194
|
+
|
195
|
+
it "should not be marked as spatial if a GiST index on a non-geometry column" do
|
196
|
+
@connection.execute(<<-SQL)
|
197
|
+
create table non_spatial_models
|
198
|
+
(
|
199
|
+
id serial primary key,
|
200
|
+
location point,
|
201
|
+
extra varchar(255)
|
202
|
+
);
|
203
|
+
create index index_non_spatial_models_on_location on non_spatial_models using gist (box(location, location));
|
204
|
+
SQL
|
205
|
+
@indexes = @connection.indexes('non_spatial_models')
|
206
|
+
@indexes.select{|i| i.name == 'index_non_spatial_models_on_location'}.first.spatial.should == false
|
207
|
+
@connection.execute 'drop table non_spatial_models'
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
describe "#add_index" do
|
212
|
+
after :each do
|
213
|
+
@connection.should_receive(:execute).with(any_args())
|
214
|
+
@connection.remove_index('geometry_models', 'geom')
|
215
|
+
end
|
216
|
+
|
217
|
+
it "should create a spatial index given :spatial => true" do
|
218
|
+
@connection.should_receive(:execute).with(/using gist/i)
|
219
|
+
@connection.add_index('geometry_models', 'geom', :spatial => true)
|
220
|
+
end
|
221
|
+
|
222
|
+
it "should not create a spatial index unless specified" do
|
223
|
+
@connection.should_not_receive(:execute).with(/using gist/i)
|
224
|
+
@connection.add_index('geometry_models', 'extra')
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
@@ -0,0 +1,365 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'spatial_adapter/postgresql'
|
3
|
+
|
4
|
+
class MigratedGeometryModel < ActiveRecord::Base
|
5
|
+
end
|
6
|
+
|
7
|
+
describe "Spatially-enabled Migrations" do
|
8
|
+
before :each do
|
9
|
+
postgis_connection
|
10
|
+
@connection = ActiveRecord::Base.connection
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "creating tables" do
|
14
|
+
after :each do
|
15
|
+
@connection.drop_table "migrated_geometry_models"
|
16
|
+
end
|
17
|
+
|
18
|
+
SpatialAdapter.geometry_data_types.keys.each do |type|
|
19
|
+
it "should create #{type.to_s} columns" do
|
20
|
+
ActiveRecord::Schema.define do
|
21
|
+
create_table :migrated_geometry_models, :force => true do |t|
|
22
|
+
t.integer :extra
|
23
|
+
t.send(type, :geom)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
|
28
|
+
geom_column.should be_a(SpatialAdapter::SpatialColumn)
|
29
|
+
geom_column.type.should == :string
|
30
|
+
geom_column.geometry_type.should == type
|
31
|
+
geom_column.should_not be_geographic
|
32
|
+
geom_column.with_z.should == false
|
33
|
+
geom_column.with_m.should == false
|
34
|
+
geom_column.srid.should == -1
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should create #{type.to_s} geographic columns" do
|
38
|
+
ActiveRecord::Schema.define do
|
39
|
+
create_table :migrated_geometry_models, :force => true do |t|
|
40
|
+
t.integer :extra
|
41
|
+
t.column :geom, type, :geographic => true
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
|
46
|
+
|
47
|
+
geom_column.should be_a(SpatialAdapter::SpatialColumn)
|
48
|
+
geom_column.type.should == :string
|
49
|
+
geom_column.geometry_type.should == type
|
50
|
+
geom_column.should be_geographic
|
51
|
+
geom_column.with_z.should == false
|
52
|
+
geom_column.with_m.should == false
|
53
|
+
#geom_column.srid.should == 4326 # SRID is currently irrelevant for geography columns
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
it "should create 3d (xyz) geometry columns" do
|
59
|
+
ActiveRecord::Schema.define do
|
60
|
+
create_table :migrated_geometry_models, :force => true do |t|
|
61
|
+
t.integer :extra
|
62
|
+
t.point :geom, :with_z => true
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
|
67
|
+
geom_column.should be_a(SpatialAdapter::SpatialColumn)
|
68
|
+
geom_column.with_z.should == true
|
69
|
+
geom_column.with_m.should == false
|
70
|
+
geom_column.srid.should == -1
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
it "should create 3d (xym) geometry columns" do
|
75
|
+
ActiveRecord::Schema.define do
|
76
|
+
create_table :migrated_geometry_models, :force => true do |t|
|
77
|
+
t.integer :extra
|
78
|
+
t.point :geom, :with_m => true
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
|
83
|
+
geom_column.should be_a(SpatialAdapter::SpatialColumn)
|
84
|
+
geom_column.geometry_type.should == :point
|
85
|
+
geom_column.type.should == :string
|
86
|
+
geom_column.with_z.should == false
|
87
|
+
geom_column.with_m.should == true
|
88
|
+
geom_column.srid.should == -1
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
it "should create 4d (xyzm) geometry columns" do
|
93
|
+
ActiveRecord::Schema.define do
|
94
|
+
create_table :migrated_geometry_models, :force => true do |t|
|
95
|
+
t.integer :extra
|
96
|
+
t.point :geom, :with_z => true, :with_m => true
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
|
101
|
+
geom_column.should be_a(SpatialAdapter::SpatialColumn)
|
102
|
+
geom_column.geometry_type.should == :point
|
103
|
+
geom_column.type.should == :string
|
104
|
+
geom_column.with_z.should == true
|
105
|
+
geom_column.with_m.should == true
|
106
|
+
geom_column.srid.should == -1
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should create 3d (xyz) geographic columns" do
|
110
|
+
ActiveRecord::Schema.define do
|
111
|
+
create_table :migrated_geometry_models, :force => true do |t|
|
112
|
+
t.integer :extra
|
113
|
+
t.point :geom, :with_z => true, :geographic => true
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
|
118
|
+
geom_column.should be_a(SpatialAdapter::SpatialColumn)
|
119
|
+
geom_column.should be_geographic
|
120
|
+
geom_column.with_z.should == true
|
121
|
+
geom_column.with_m.should == false
|
122
|
+
end
|
123
|
+
|
124
|
+
|
125
|
+
it "should create 3d (xym) geographic columns" do
|
126
|
+
ActiveRecord::Schema.define do
|
127
|
+
create_table :migrated_geometry_models, :force => true do |t|
|
128
|
+
t.integer :extra
|
129
|
+
t.point :geom, :with_m => true, :geographic => true
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
|
134
|
+
geom_column.should be_a(SpatialAdapter::SpatialColumn)
|
135
|
+
geom_column.geometry_type.should == :point
|
136
|
+
geom_column.type.should == :string
|
137
|
+
geom_column.should be_geographic
|
138
|
+
geom_column.with_z.should == false
|
139
|
+
geom_column.with_m.should == true
|
140
|
+
end
|
141
|
+
|
142
|
+
|
143
|
+
it "should create 4d (xyzm) geographic columns" do
|
144
|
+
ActiveRecord::Schema.define do
|
145
|
+
create_table :migrated_geometry_models, :force => true do |t|
|
146
|
+
t.integer :extra
|
147
|
+
t.point :geom, :with_z => true, :with_m => true, :geographic => true
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
|
152
|
+
geom_column.should be_a(SpatialAdapter::SpatialColumn)
|
153
|
+
geom_column.geometry_type.should == :point
|
154
|
+
geom_column.should be_geographic
|
155
|
+
geom_column.type.should == :string
|
156
|
+
geom_column.with_z.should == true
|
157
|
+
geom_column.with_m.should == true
|
158
|
+
end
|
159
|
+
|
160
|
+
|
161
|
+
it "should create geometry columns with specified SRID" do
|
162
|
+
ActiveRecord::Schema.define do
|
163
|
+
create_table :migrated_geometry_models, :force => true do |t|
|
164
|
+
t.integer :extra
|
165
|
+
t.geometry :geom, :srid => 4326
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
|
170
|
+
geom_column.should be_a(SpatialAdapter::SpatialColumn)
|
171
|
+
geom_column.type.should == :string
|
172
|
+
geom_column.geometry_type.should == :geometry
|
173
|
+
geom_column.with_z.should == false
|
174
|
+
geom_column.with_m.should == false
|
175
|
+
geom_column.srid.should == 4326
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
describe "adding columns" do
|
180
|
+
before :each do
|
181
|
+
ActiveRecord::Schema.define do
|
182
|
+
create_table :migrated_geometry_models, :force => true do |t|
|
183
|
+
t.integer :extra
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
after :each do
|
189
|
+
@connection.drop_table "migrated_geometry_models"
|
190
|
+
end
|
191
|
+
|
192
|
+
SpatialAdapter.geometry_data_types.keys.each do |type|
|
193
|
+
it "should add #{type.to_s} columns" do
|
194
|
+
ActiveRecord::Schema.define do
|
195
|
+
add_column :migrated_geometry_models, :geom, type
|
196
|
+
end
|
197
|
+
|
198
|
+
geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
|
199
|
+
geom_column.should be_a(SpatialAdapter::SpatialColumn)
|
200
|
+
geom_column.type.should == :string
|
201
|
+
geom_column.geometry_type.should == type
|
202
|
+
geom_column.with_z.should == false
|
203
|
+
geom_column.with_m.should == false
|
204
|
+
geom_column.srid.should == -1
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
it "should add 3d (xyz) geometry columns" do
|
209
|
+
ActiveRecord::Schema.define do
|
210
|
+
add_column :migrated_geometry_models, :geom, :point, :with_z => true
|
211
|
+
end
|
212
|
+
|
213
|
+
geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
|
214
|
+
geom_column.should be_a(SpatialAdapter::SpatialColumn)
|
215
|
+
geom_column.type.should == :string
|
216
|
+
geom_column.geometry_type.should == :point
|
217
|
+
geom_column.with_z.should == true
|
218
|
+
geom_column.with_m.should == false
|
219
|
+
geom_column.srid.should == -1
|
220
|
+
end
|
221
|
+
|
222
|
+
|
223
|
+
it "should add 3d (xym) geometry columns" do
|
224
|
+
ActiveRecord::Schema.define do
|
225
|
+
add_column :migrated_geometry_models, :geom, :point, :with_m => true
|
226
|
+
end
|
227
|
+
|
228
|
+
geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
|
229
|
+
geom_column.should be_a(SpatialAdapter::SpatialColumn)
|
230
|
+
geom_column.type.should == :string
|
231
|
+
geom_column.geometry_type.should == :point
|
232
|
+
geom_column.with_z.should == false
|
233
|
+
geom_column.with_m.should == true
|
234
|
+
geom_column.srid.should == -1
|
235
|
+
end
|
236
|
+
|
237
|
+
|
238
|
+
it "should add 4d (xyzm) geometry columns" do
|
239
|
+
ActiveRecord::Schema.define do
|
240
|
+
add_column :migrated_geometry_models, :geom, :point, :with_z => true, :with_m => true
|
241
|
+
end
|
242
|
+
|
243
|
+
geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
|
244
|
+
geom_column.should be_a(SpatialAdapter::SpatialColumn)
|
245
|
+
geom_column.type.should == :string
|
246
|
+
geom_column.geometry_type.should == :point
|
247
|
+
geom_column.with_z.should == true
|
248
|
+
geom_column.with_m.should == true
|
249
|
+
geom_column.srid.should == -1
|
250
|
+
end
|
251
|
+
|
252
|
+
it "should add 3d (xyz) geography columns" do
|
253
|
+
ActiveRecord::Schema.define do
|
254
|
+
add_column :migrated_geometry_models, :geom, :point, :with_z => true, :geographic => true
|
255
|
+
end
|
256
|
+
|
257
|
+
geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
|
258
|
+
geom_column.should be_a(SpatialAdapter::SpatialColumn)
|
259
|
+
geom_column.type.should == :string
|
260
|
+
geom_column.should be_geographic
|
261
|
+
geom_column.geometry_type.should == :point
|
262
|
+
geom_column.with_z.should == true
|
263
|
+
geom_column.with_m.should == false
|
264
|
+
end
|
265
|
+
|
266
|
+
|
267
|
+
it "should add 3d (xym) geography columns" do
|
268
|
+
ActiveRecord::Schema.define do
|
269
|
+
add_column :migrated_geometry_models, :geom, :point, :with_m => true, :geographic => true
|
270
|
+
end
|
271
|
+
|
272
|
+
geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
|
273
|
+
geom_column.should be_a(SpatialAdapter::SpatialColumn)
|
274
|
+
geom_column.type.should == :string
|
275
|
+
geom_column.should be_geographic
|
276
|
+
geom_column.geometry_type.should == :point
|
277
|
+
geom_column.with_z.should == false
|
278
|
+
geom_column.with_m.should == true
|
279
|
+
end
|
280
|
+
|
281
|
+
|
282
|
+
it "should add 4d (xyzm) geography columns" do
|
283
|
+
ActiveRecord::Schema.define do
|
284
|
+
add_column :migrated_geometry_models, :geom, :point, :with_z => true, :with_m => true, :geographic => true
|
285
|
+
end
|
286
|
+
|
287
|
+
geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
|
288
|
+
geom_column.should be_a(SpatialAdapter::SpatialColumn)
|
289
|
+
geom_column.type.should == :string
|
290
|
+
geom_column.should be_geographic
|
291
|
+
geom_column.geometry_type.should == :point
|
292
|
+
geom_column.with_z.should == true
|
293
|
+
geom_column.with_m.should == true
|
294
|
+
end
|
295
|
+
|
296
|
+
it "should add GEOMETRY columns with specified SRID" do
|
297
|
+
ActiveRecord::Schema.define do
|
298
|
+
add_column :migrated_geometry_models, :geom, :geometry, :srid => 4326
|
299
|
+
end
|
300
|
+
|
301
|
+
geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
|
302
|
+
geom_column.should be_a(SpatialAdapter::SpatialColumn)
|
303
|
+
geom_column.geometry_type.should == :geometry
|
304
|
+
geom_column.type.should == :string
|
305
|
+
geom_column.with_z.should == false
|
306
|
+
geom_column.with_m.should == false
|
307
|
+
geom_column.srid.should == 4326
|
308
|
+
end
|
309
|
+
end
|
310
|
+
|
311
|
+
describe "removing columns" do
|
312
|
+
after :each do
|
313
|
+
@connection.drop_table "migrated_geometry_models"
|
314
|
+
end
|
315
|
+
|
316
|
+
SpatialAdapter.geometry_data_types.keys.each do |type|
|
317
|
+
it "should remove #{type.to_s} columns using DropGeometryColumn" do
|
318
|
+
ActiveRecord::Schema.define do
|
319
|
+
create_table :migrated_geometry_models, :force => true do |t|
|
320
|
+
t.integer :extra
|
321
|
+
t.send(type, :geom)
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
325
|
+
@connection.should_receive(:execute).with(/DropGeometryColumn(.*migrated_geometry_models.*geom)/)
|
326
|
+
ActiveRecord::Schema.define do
|
327
|
+
remove_column :migrated_geometry_models, :geom
|
328
|
+
end
|
329
|
+
@connection.should_receive(:execute).with(anything())
|
330
|
+
end
|
331
|
+
end
|
332
|
+
|
333
|
+
SpatialAdapter.geometry_data_types.keys.each do |type|
|
334
|
+
it "should remove #{type.to_s} geography columns using ALTER TABLE DROP COLUMN" do
|
335
|
+
ActiveRecord::Schema.define do
|
336
|
+
create_table :migrated_geometry_models, :force => true do |t|
|
337
|
+
t.integer :extra
|
338
|
+
t.send(type, :geom, :geographic => true)
|
339
|
+
end
|
340
|
+
end
|
341
|
+
|
342
|
+
@connection.should_receive(:execute).with(/alter table.*migrated_geometry_models.*drop.*geom/i)
|
343
|
+
ActiveRecord::Schema.define do
|
344
|
+
remove_column :migrated_geometry_models, :geom
|
345
|
+
end
|
346
|
+
@connection.should_receive(:execute).with(anything())
|
347
|
+
end
|
348
|
+
end
|
349
|
+
|
350
|
+
it "should still remove non-spatial columns using ALTER TABLE DROP COLUMN" do
|
351
|
+
ActiveRecord::Schema.define do
|
352
|
+
create_table :migrated_geometry_models, :force => true do |t|
|
353
|
+
t.integer :extra
|
354
|
+
t.point :geom
|
355
|
+
end
|
356
|
+
end
|
357
|
+
|
358
|
+
@connection.should_receive(:execute).with(/alter table.*migrated_geometry_models.*drop.*extra/i)
|
359
|
+
ActiveRecord::Schema.define do
|
360
|
+
remove_column :migrated_geometry_models, :extra
|
361
|
+
end
|
362
|
+
@connection.should_receive(:execute).with(anything())
|
363
|
+
end
|
364
|
+
end
|
365
|
+
end
|