activerecord-postgis-adapter 3.1.0 → 3.1.1
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/arel_tosql.rb +2 -4
- data/lib/active_record/connection_adapters/postgis/create_connection.rb +4 -6
- data/lib/active_record/connection_adapters/postgis/databases.rake +1 -1
- data/lib/active_record/connection_adapters/postgis/oid/spatial.rb +6 -6
- data/lib/active_record/connection_adapters/postgis/postgis_database_tasks.rb +27 -29
- data/lib/active_record/connection_adapters/postgis/railtie.rb +2 -2
- data/lib/active_record/connection_adapters/postgis/schema_statements.rb +7 -9
- data/lib/active_record/connection_adapters/postgis/setup.rb +1 -3
- data/lib/active_record/connection_adapters/postgis/spatial_column.rb +0 -1
- data/lib/active_record/connection_adapters/postgis/spatial_column_info.rb +1 -1
- data/lib/active_record/connection_adapters/postgis/spatial_table_definition.rb +3 -4
- data/lib/active_record/connection_adapters/postgis/version.rb +1 -1
- data/lib/active_record/connection_adapters/postgis_adapter.rb +20 -20
- data/lib/activerecord-postgis-adapter.rb +1 -1
- metadata +5 -22
- data/test/basic_test.rb +0 -160
- data/test/database.yml +0 -6
- data/test/ddl_test.rb +0 -323
- data/test/nested_class_test.rb +0 -21
- data/test/setup_test.rb +0 -9
- data/test/spatial_queries_test.rb +0 -79
- data/test/tasks_test.rb +0 -159
- data/test/test_helper.rb +0 -33
- data/test/type_test.rb +0 -26
data/test/nested_class_test.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class NestedClassTest < ActiveSupport::TestCase # :nodoc:
|
4
|
-
module Foo
|
5
|
-
def self.table_name_prefix
|
6
|
-
'foo_'
|
7
|
-
end
|
8
|
-
class Bar < ActiveRecord::Base
|
9
|
-
establish_connection YAML.load_file(ActiveSupport::TestCase::DATABASE_CONFIG_PATH)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_nested_model
|
14
|
-
Foo::Bar.connection.create_table(:foo_bars, force: true) do |t|
|
15
|
-
t.column 'latlon', :st_point, srid: 3785
|
16
|
-
end
|
17
|
-
assert_empty Foo::Bar.all
|
18
|
-
Foo::Bar.connection.drop_table(:foo_bars)
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
data/test/setup_test.rb
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class SpatialQueriesTest < ActiveSupport::TestCase # :nodoc:
|
4
|
-
def test_query_point
|
5
|
-
create_model
|
6
|
-
obj = SpatialModel.new
|
7
|
-
obj.latlon = factory.point(1.0, 2.0)
|
8
|
-
obj.save!
|
9
|
-
id = obj.id
|
10
|
-
obj2 = SpatialModel.where(latlon: factory.multi_point([factory.point(1.0, 2.0)])).first
|
11
|
-
refute_nil(obj2)
|
12
|
-
assert_equal(id, obj2.id)
|
13
|
-
obj3 = SpatialModel.where(latlon: factory.point(2.0, 2.0)).first
|
14
|
-
assert_nil(obj3)
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_query_point_wkt
|
18
|
-
create_model
|
19
|
-
obj = SpatialModel.new
|
20
|
-
obj.latlon = factory.point(1.0, 2.0)
|
21
|
-
obj.save!
|
22
|
-
id = obj.id
|
23
|
-
obj2 = SpatialModel.where(latlon: 'SRID=3785;POINT(1 2)').first
|
24
|
-
refute_nil(obj2)
|
25
|
-
assert_equal(id, obj2.id)
|
26
|
-
obj3 = SpatialModel.where(latlon: 'SRID=3785;POINT(2 2)').first
|
27
|
-
assert_nil(obj3)
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_query_st_distance
|
31
|
-
create_model
|
32
|
-
obj = SpatialModel.new
|
33
|
-
obj.latlon = factory.point(1.0, 2.0)
|
34
|
-
obj.save!
|
35
|
-
id = obj.id
|
36
|
-
obj2 = SpatialModel.where(SpatialModel.arel_table[:latlon].st_distance('SRID=3785;POINT(2 3)').lt(2)).first
|
37
|
-
refute_nil(obj2)
|
38
|
-
assert_equal(id, obj2.id)
|
39
|
-
obj3 = SpatialModel.where(SpatialModel.arel_table[:latlon].st_distance('SRID=3785;POINT(2 3)').gt(2)).first
|
40
|
-
assert_nil(obj3)
|
41
|
-
end
|
42
|
-
|
43
|
-
def test_query_st_distance_from_constant
|
44
|
-
create_model
|
45
|
-
obj = SpatialModel.new
|
46
|
-
obj.latlon = factory.point(1.0, 2.0)
|
47
|
-
obj.save!
|
48
|
-
id = obj.id
|
49
|
-
obj2 = SpatialModel.where(::Arel.spatial('SRID=3785;POINT(2 3)').st_distance(SpatialModel.arel_table[:latlon]).lt(2)).first
|
50
|
-
refute_nil(obj2)
|
51
|
-
assert_equal(id, obj2.id)
|
52
|
-
obj3 = SpatialModel.where(::Arel.spatial('SRID=3785;POINT(2 3)').st_distance(SpatialModel.arel_table[:latlon]).gt(2)).first
|
53
|
-
assert_nil(obj3)
|
54
|
-
end
|
55
|
-
|
56
|
-
def test_query_st_length
|
57
|
-
create_model
|
58
|
-
obj = SpatialModel.new
|
59
|
-
obj.path = factory.line(factory.point(1.0, 2.0), factory.point(3.0, 2.0))
|
60
|
-
obj.save!
|
61
|
-
id = obj.id
|
62
|
-
obj2 = SpatialModel.where(SpatialModel.arel_table[:path].st_length.eq(2)).first
|
63
|
-
refute_nil(obj2)
|
64
|
-
assert_equal(id, obj2.id)
|
65
|
-
obj3 = SpatialModel.where(SpatialModel.arel_table[:path].st_length.gt(3)).first
|
66
|
-
assert_nil(obj3)
|
67
|
-
end
|
68
|
-
|
69
|
-
private
|
70
|
-
|
71
|
-
def create_model
|
72
|
-
SpatialModel.connection.create_table(:spatial_models, force: true) do |t|
|
73
|
-
t.column 'latlon', :st_point, srid: 3785
|
74
|
-
t.column 'path', :line_string, srid: 3785
|
75
|
-
end
|
76
|
-
SpatialModel.reset_column_information
|
77
|
-
end
|
78
|
-
|
79
|
-
end
|
data/test/tasks_test.rb
DELETED
@@ -1,159 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'active_record/schema_dumper'
|
3
|
-
|
4
|
-
class TasksTest < ActiveSupport::TestCase # :nodoc:
|
5
|
-
NEW_CONNECTION = {
|
6
|
-
"adapter" => "postgis",
|
7
|
-
"host" => "127.0.0.1",
|
8
|
-
"database" => "postgis_tasks_test",
|
9
|
-
"username" => "postgres",
|
10
|
-
"setup" => "default",
|
11
|
-
"schema_search_path" => "public",
|
12
|
-
}
|
13
|
-
|
14
|
-
def test_create_database_from_extension_in_public_schema
|
15
|
-
drop_db_if_exists
|
16
|
-
ActiveRecord::Tasks::DatabaseTasks.create(NEW_CONNECTION)
|
17
|
-
refute_empty connection.select_values("SELECT * from public.spatial_ref_sys")
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_create_database_from_extension_in_separate_schema
|
21
|
-
drop_db_if_exists
|
22
|
-
configuration = NEW_CONNECTION.merge("postgis_schema" => "postgis")
|
23
|
-
ActiveRecord::Tasks::DatabaseTasks.create(configuration)
|
24
|
-
refute_empty connection.select_values("SELECT * from postgis.spatial_ref_sys")
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_empty_sql_dump
|
28
|
-
setup_database_tasks
|
29
|
-
ActiveRecord::Tasks::DatabaseTasks.structure_dump(NEW_CONNECTION, tmp_sql_filename)
|
30
|
-
sql = File.read(tmp_sql_filename)
|
31
|
-
assert(sql !~ /CREATE TABLE/)
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_basic_geography_sql_dump
|
35
|
-
setup_database_tasks
|
36
|
-
connection.create_table(:spatial_test, force: true) do |t|
|
37
|
-
t.st_point "latlon", geographic: true
|
38
|
-
t.geometry "geo_col", srid: 4326
|
39
|
-
end
|
40
|
-
ActiveRecord::Tasks::DatabaseTasks.structure_dump(NEW_CONNECTION, tmp_sql_filename)
|
41
|
-
data = File.read(tmp_sql_filename)
|
42
|
-
assert(data.index('latlon geography(Point,4326)'))
|
43
|
-
assert(data.index('geo_col geometry(Geometry,4326)'))
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_index_sql_dump
|
47
|
-
setup_database_tasks
|
48
|
-
connection.create_table(:spatial_test, force: true) do |t|
|
49
|
-
t.st_point "latlon", geographic: true
|
50
|
-
t.string "name"
|
51
|
-
end
|
52
|
-
connection.add_index :spatial_test, :latlon, using: :gist
|
53
|
-
connection.add_index :spatial_test, :name, using: :btree
|
54
|
-
ActiveRecord::Tasks::DatabaseTasks.structure_dump(NEW_CONNECTION, tmp_sql_filename)
|
55
|
-
data = File.read(tmp_sql_filename)
|
56
|
-
assert(data.index('latlon geography(Point,4326)'))
|
57
|
-
assert data.index('CREATE INDEX index_spatial_test_on_latlon ON spatial_test USING gist (latlon);')
|
58
|
-
assert data.index('CREATE INDEX index_spatial_test_on_name ON spatial_test USING btree (name);')
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_empty_schema_dump
|
62
|
-
setup_database_tasks
|
63
|
-
File.open(tmp_sql_filename, "w:utf-8") do |file|
|
64
|
-
ActiveRecord::SchemaDumper.dump(::ActiveRecord::Base.connection, file)
|
65
|
-
end
|
66
|
-
data = File.read(tmp_sql_filename)
|
67
|
-
assert(data.index('ActiveRecord::Schema'))
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_basic_geometry_schema_dump
|
71
|
-
setup_database_tasks
|
72
|
-
connection.create_table(:spatial_test, force: true) do |t|
|
73
|
-
t.geometry 'object1'
|
74
|
-
t.spatial "object2", srid: connection.default_srid, type: "geometry"
|
75
|
-
end
|
76
|
-
File.open(tmp_sql_filename, "w:utf-8") do |file|
|
77
|
-
ActiveRecord::SchemaDumper.dump(connection, file)
|
78
|
-
end
|
79
|
-
data = File.read(tmp_sql_filename)
|
80
|
-
assert data.index("t.geometry \"object1\", limit: {:srid=>#{connection.default_srid}, :type=>\"geometry\"")
|
81
|
-
assert data.index("t.geometry \"object2\", limit: {:srid=>#{connection.default_srid}, :type=>\"geometry\"")
|
82
|
-
end
|
83
|
-
|
84
|
-
def test_basic_geography_schema_dump
|
85
|
-
setup_database_tasks
|
86
|
-
connection.create_table(:spatial_test, force: true) do |t|
|
87
|
-
t.st_point "latlon1", geographic: true
|
88
|
-
t.spatial "latlon2", srid: 4326, type: "st_point", geographic: true
|
89
|
-
end
|
90
|
-
File.open(tmp_sql_filename, "w:utf-8") do |file|
|
91
|
-
ActiveRecord::SchemaDumper.dump(connection, file)
|
92
|
-
end
|
93
|
-
data = File.read(tmp_sql_filename)
|
94
|
-
assert data.index(%(t.geography "latlon1", limit: {:srid=>4326, :type=>"point", :geographic=>true}))
|
95
|
-
assert data.index(%(t.geography "latlon2", limit: {:srid=>4326, :type=>"point", :geographic=>true}))
|
96
|
-
end
|
97
|
-
|
98
|
-
def test_index_schema_dump
|
99
|
-
setup_database_tasks
|
100
|
-
connection.create_table(:spatial_test, force: true) do |t|
|
101
|
-
t.st_point "latlon", geographic: true
|
102
|
-
end
|
103
|
-
connection.add_index :spatial_test, :latlon, using: :gist
|
104
|
-
File.open(tmp_sql_filename, "w:utf-8") do |file|
|
105
|
-
ActiveRecord::SchemaDumper.dump(connection, file)
|
106
|
-
end
|
107
|
-
data = File.read(tmp_sql_filename)
|
108
|
-
assert data.index(%(t.geography "latlon", limit: {:srid=>4326, :type=>"point", :geographic=>true}))
|
109
|
-
assert data.index(%(add_index "spatial_test", ["latlon"], name: "index_spatial_test_on_latlon", using: :gist))
|
110
|
-
end
|
111
|
-
|
112
|
-
def test_add_index_with_no_options
|
113
|
-
setup_database_tasks
|
114
|
-
connection.create_table(:test, force: true) do |t|
|
115
|
-
t.string "name"
|
116
|
-
end
|
117
|
-
connection.add_index :test, :name
|
118
|
-
ActiveRecord::Tasks::DatabaseTasks.structure_dump(NEW_CONNECTION, tmp_sql_filename)
|
119
|
-
data = File.read(tmp_sql_filename)
|
120
|
-
assert data.index('CREATE INDEX index_test_on_name ON test USING btree (name);')
|
121
|
-
end
|
122
|
-
|
123
|
-
def test_add_index_via_references
|
124
|
-
setup_database_tasks
|
125
|
-
connection.create_table(:cats, force: true)
|
126
|
-
connection.create_table(:dogs, force: true) do |t|
|
127
|
-
t.references :cats, index: true
|
128
|
-
end
|
129
|
-
ActiveRecord::Tasks::DatabaseTasks.structure_dump(NEW_CONNECTION, tmp_sql_filename)
|
130
|
-
data = File.read(tmp_sql_filename)
|
131
|
-
assert data.index('CREATE INDEX index_dogs_on_cats_id ON dogs USING btree (cats_id);')
|
132
|
-
end
|
133
|
-
|
134
|
-
private
|
135
|
-
|
136
|
-
def connection
|
137
|
-
ActiveRecord::Base.connection
|
138
|
-
end
|
139
|
-
|
140
|
-
def tmp_sql_filename
|
141
|
-
File.expand_path('../tmp/tmp.sql', ::File.dirname(__FILE__))
|
142
|
-
end
|
143
|
-
|
144
|
-
def setup_database_tasks
|
145
|
-
FileUtils.rm_f(tmp_sql_filename)
|
146
|
-
FileUtils.mkdir_p(::File.dirname(tmp_sql_filename))
|
147
|
-
drop_db_if_exists
|
148
|
-
ActiveRecord::ConnectionAdapters::PostGIS::PostGISDatabaseTasks.new(NEW_CONNECTION).create
|
149
|
-
rescue ActiveRecord::Tasks::DatabaseAlreadyExists
|
150
|
-
# ignore
|
151
|
-
end
|
152
|
-
|
153
|
-
def drop_db_if_exists
|
154
|
-
ActiveRecord::ConnectionAdapters::PostGIS::PostGISDatabaseTasks.new(NEW_CONNECTION).drop
|
155
|
-
rescue ActiveRecord::Tasks::DatabaseAlreadyExists
|
156
|
-
# ignore
|
157
|
-
end
|
158
|
-
|
159
|
-
end
|
data/test/test_helper.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'minitest/pride'
|
3
|
-
require 'mocha/mini_test'
|
4
|
-
require 'active_record'
|
5
|
-
require 'activerecord-postgis-adapter'
|
6
|
-
|
7
|
-
begin
|
8
|
-
require 'byebug'
|
9
|
-
rescue LoadError
|
10
|
-
# ignore
|
11
|
-
end
|
12
|
-
|
13
|
-
class ActiveSupport::TestCase
|
14
|
-
self.test_order = :sorted
|
15
|
-
|
16
|
-
DATABASE_CONFIG_PATH = File.dirname(__FILE__) << '/database.yml'
|
17
|
-
|
18
|
-
class SpatialModel < ActiveRecord::Base
|
19
|
-
establish_connection YAML.load_file(DATABASE_CONFIG_PATH)
|
20
|
-
end
|
21
|
-
|
22
|
-
def factory
|
23
|
-
RGeo::Cartesian.preferred_factory(srid: 3785)
|
24
|
-
end
|
25
|
-
|
26
|
-
def geographic_factory
|
27
|
-
RGeo::Geographic.spherical_factory(srid: 4326)
|
28
|
-
end
|
29
|
-
|
30
|
-
def spatial_factory_store
|
31
|
-
RGeo::ActiveRecord::SpatialFactoryStore.instance
|
32
|
-
end
|
33
|
-
end
|
data/test/type_test.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
|
3
|
-
class TypeTest < ActiveSupport::TestCase
|
4
|
-
def test_parse_simple_type
|
5
|
-
assert_equal ["geometry", 0, false, false], spatial.parse_sql_type("geometry")
|
6
|
-
assert_equal ["geography", 0, false, false], spatial.parse_sql_type("geography")
|
7
|
-
end
|
8
|
-
|
9
|
-
def test_parse_geo_type
|
10
|
-
assert_equal ["Point", 0, false, false], spatial.parse_sql_type("geography(Point)")
|
11
|
-
assert_equal ["Polygon", 0, false, false], spatial.parse_sql_type("geography(Polygon)")
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_parse_type_with_srid
|
15
|
-
assert_equal ["Point", 4326, false, false], spatial.parse_sql_type("geography(Point,4326)")
|
16
|
-
assert_equal ["Polygon", 4327, true, false], spatial.parse_sql_type("geography(PolygonZ,4327)")
|
17
|
-
assert_equal ["Point", 4328, false, true], spatial.parse_sql_type("geography(PointM,4328)")
|
18
|
-
assert_equal ["Point", 4329, true, true], spatial.parse_sql_type("geography(PointZM,4329)")
|
19
|
-
end
|
20
|
-
|
21
|
-
private
|
22
|
-
|
23
|
-
def spatial
|
24
|
-
ActiveRecord::ConnectionAdapters::PostGIS::OID::Spatial
|
25
|
-
end
|
26
|
-
end
|