activerecord-postgis-adapter 2.0.2 → 2.1.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.
- checksums.yaml +4 -4
- data/lib/active_record/connection_adapters/postgis_adapter.rb +9 -8
- data/lib/active_record/connection_adapters/postgis_adapter/common_adapter_methods.rb +6 -6
- data/lib/active_record/connection_adapters/postgis_adapter/main_adapter.rb +18 -33
- data/lib/active_record/connection_adapters/postgis_adapter/spatial_column.rb +12 -8
- data/lib/active_record/connection_adapters/postgis_adapter/spatial_column_info.rb +42 -0
- data/lib/active_record/connection_adapters/postgis_adapter/version.rb +1 -1
- data/test/basic_test.rb +175 -183
- data/test/ddl_test.rb +248 -217
- data/test/nested_class_test.rb +22 -30
- data/test/setup_test.rb +4 -12
- data/test/spatial_queries_test.rb +83 -91
- data/test/tasks_test.rb +145 -153
- metadata +18 -3
data/test/nested_class_test.rb
CHANGED
@@ -1,40 +1,32 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
module Tests # :nodoc:
|
7
|
-
class NestedClassTest < ActiveSupport::TestCase # :nodoc:
|
8
|
-
DATABASE_CONFIG_PATH = ::File.dirname(__FILE__) + '/database.yml'
|
9
|
-
OVERRIDE_DATABASE_CONFIG_PATH = ::File.dirname(__FILE__) + '/database_local.yml'
|
3
|
+
class NestedClassTest < ActiveSupport::TestCase # :nodoc:
|
4
|
+
DATABASE_CONFIG_PATH = ::File.dirname(__FILE__) + '/database.yml'
|
5
|
+
OVERRIDE_DATABASE_CONFIG_PATH = ::File.dirname(__FILE__) + '/database_local.yml'
|
10
6
|
|
11
|
-
|
7
|
+
include RGeo::ActiveRecord::AdapterTestHelper
|
12
8
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
define_test_methods do
|
22
|
-
|
23
|
-
def test_nested_model
|
24
|
-
Foo::Bar.class_eval do
|
25
|
-
establish_connection(DATABASE_CONFIG)
|
26
|
-
end
|
27
|
-
Foo::Bar.connection.create_table(:foo_bars) do |t|
|
28
|
-
t.column 'latlon', :point, :srid => 3785
|
29
|
-
end
|
30
|
-
Foo::Bar.all
|
31
|
-
Foo::Bar.connection.drop_table(:foo_bars)
|
32
|
-
end
|
9
|
+
module Foo
|
10
|
+
def self.table_name_prefix
|
11
|
+
'foo_'
|
12
|
+
end
|
13
|
+
class Bar < ::ActiveRecord::Base
|
14
|
+
end
|
15
|
+
end
|
33
16
|
|
34
|
-
|
17
|
+
define_test_methods do
|
35
18
|
|
36
|
-
|
19
|
+
def test_nested_model
|
20
|
+
Foo::Bar.class_eval do
|
21
|
+
establish_connection(DATABASE_CONFIG)
|
22
|
+
end
|
23
|
+
Foo::Bar.connection.create_table(:foo_bars) do |t|
|
24
|
+
t.column 'latlon', :point, :srid => 3785
|
37
25
|
end
|
26
|
+
Foo::Bar.all
|
27
|
+
Foo::Bar.connection.drop_table(:foo_bars)
|
38
28
|
end
|
29
|
+
|
39
30
|
end
|
31
|
+
|
40
32
|
end
|
data/test/setup_test.rb
CHANGED
@@ -1,17 +1,9 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
|
4
|
-
module ActiveRecord # :nodoc:
|
5
|
-
module PostGISAdapter # :nodoc:
|
6
|
-
module Tests # :nodoc:
|
7
|
-
class SpatialQueriesTest < ActiveSupport::TestCase # :nodoc:
|
3
|
+
class SpatialQueriesTest < ActiveSupport::TestCase # :nodoc:
|
8
4
|
|
9
|
-
|
10
|
-
|
11
|
-
end
|
12
|
-
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
5
|
+
def test_ignore_tables
|
6
|
+
assert_equal %w(geometry_columns spatial_ref_sys layer topology), ::ActiveRecord::SchemaDumper.ignore_tables
|
16
7
|
end
|
8
|
+
|
17
9
|
end
|
@@ -1,105 +1,97 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
|
4
|
-
module ActiveRecord # :nodoc:
|
5
|
-
module PostGISAdapter # :nodoc:
|
6
|
-
module Tests # :nodoc:
|
7
|
-
class SpatialQueriesTest < ActiveSupport::TestCase # :nodoc:
|
3
|
+
class SpatialQueriesTest < ActiveSupport::TestCase # :nodoc:
|
8
4
|
|
9
|
-
|
10
|
-
|
5
|
+
DATABASE_CONFIG_PATH = ::File.dirname(__FILE__) + '/database.yml'
|
6
|
+
OVERRIDE_DATABASE_CONFIG_PATH = ::File.dirname(__FILE__) + '/database_local.yml'
|
11
7
|
|
12
|
-
|
8
|
+
include RGeo::ActiveRecord::AdapterTestHelper
|
13
9
|
|
14
|
-
|
10
|
+
define_test_methods do
|
15
11
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
def test_query_point
|
36
|
-
klass = populate_ar_class(:mercator_point)
|
37
|
-
obj = klass.new
|
38
|
-
obj.latlon = @factory.point(1.0, 2.0)
|
39
|
-
obj.save!
|
40
|
-
id = obj.id
|
41
|
-
obj2 = klass.where(:latlon => @factory.multi_point([@factory.point(1.0, 2.0)])).first
|
42
|
-
refute_nil(obj2)
|
43
|
-
assert_equal(id, obj2.id)
|
44
|
-
obj3 = klass.where(:latlon => @factory.point(2.0, 2.0)).first
|
45
|
-
assert_nil(obj3)
|
46
|
-
end
|
12
|
+
def populate_ar_class(content)
|
13
|
+
klass = create_ar_class
|
14
|
+
case content
|
15
|
+
when :mercator_point
|
16
|
+
klass.connection.create_table(:spatial_test) do |t|
|
17
|
+
t.column 'latlon', :point, :srid => 3785
|
18
|
+
end
|
19
|
+
when :latlon_point_geographic
|
20
|
+
klass.connection.create_table(:spatial_test) do |t|
|
21
|
+
t.column 'latlon', :point, :srid => 4326, :geographic => true
|
22
|
+
end
|
23
|
+
when :path_linestring
|
24
|
+
klass.connection.create_table(:spatial_test) do |t|
|
25
|
+
t.column 'path', :line_string, :srid => 3785
|
26
|
+
end
|
27
|
+
end
|
28
|
+
klass
|
29
|
+
end
|
47
30
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
31
|
+
def test_query_point
|
32
|
+
klass = populate_ar_class(:mercator_point)
|
33
|
+
obj = klass.new
|
34
|
+
obj.latlon = @factory.point(1.0, 2.0)
|
35
|
+
obj.save!
|
36
|
+
id = obj.id
|
37
|
+
obj2 = klass.where(:latlon => @factory.multi_point([@factory.point(1.0, 2.0)])).first
|
38
|
+
refute_nil(obj2)
|
39
|
+
assert_equal(id, obj2.id)
|
40
|
+
obj3 = klass.where(:latlon => @factory.point(2.0, 2.0)).first
|
41
|
+
assert_nil(obj3)
|
42
|
+
end
|
60
43
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
44
|
+
def test_query_point_wkt
|
45
|
+
klass = populate_ar_class(:mercator_point)
|
46
|
+
obj = klass.new
|
47
|
+
obj.latlon = @factory.point(1.0, 2.0)
|
48
|
+
obj.save!
|
49
|
+
id = obj.id
|
50
|
+
obj2 = klass.where(:latlon => 'SRID=3785;POINT(1 2)').first
|
51
|
+
refute_nil(obj2)
|
52
|
+
assert_equal(id, obj2.id)
|
53
|
+
obj3 = klass.where(:latlon => 'SRID=3785;POINT(2 2)').first
|
54
|
+
assert_nil(obj3)
|
55
|
+
end
|
73
56
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
57
|
+
def test_query_st_distance
|
58
|
+
klass = populate_ar_class(:mercator_point)
|
59
|
+
obj = klass.new
|
60
|
+
obj.latlon = @factory.point(1.0, 2.0)
|
61
|
+
obj.save!
|
62
|
+
id = obj.id
|
63
|
+
obj2 = klass.where(klass.arel_table[:latlon].st_distance('SRID=3785;POINT(2 3)').lt(2)).first
|
64
|
+
refute_nil(obj2)
|
65
|
+
assert_equal(id, obj2.id)
|
66
|
+
obj3 = klass.where(klass.arel_table[:latlon].st_distance('SRID=3785;POINT(2 3)').gt(2)).first
|
67
|
+
assert_nil(obj3)
|
68
|
+
end
|
86
69
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
70
|
+
def test_query_st_distance_from_constant
|
71
|
+
klass = populate_ar_class(:mercator_point)
|
72
|
+
obj = klass.new
|
73
|
+
obj.latlon = @factory.point(1.0, 2.0)
|
74
|
+
obj.save!
|
75
|
+
id = obj.id
|
76
|
+
obj2 = klass.where(::Arel.spatial('SRID=3785;POINT(2 3)').st_distance(klass.arel_table[:latlon]).lt(2)).first
|
77
|
+
refute_nil(obj2)
|
78
|
+
assert_equal(id, obj2.id)
|
79
|
+
obj3 = klass.where(::Arel.spatial('SRID=3785;POINT(2 3)').st_distance(klass.arel_table[:latlon]).gt(2)).first
|
80
|
+
assert_nil(obj3)
|
81
|
+
end
|
99
82
|
|
100
|
-
|
101
|
-
|
102
|
-
|
83
|
+
def test_query_st_length
|
84
|
+
klass = populate_ar_class(:path_linestring)
|
85
|
+
obj = klass.new
|
86
|
+
obj.path = @factory.line(@factory.point(1.0, 2.0), @factory.point(3.0, 2.0))
|
87
|
+
obj.save!
|
88
|
+
id = obj.id
|
89
|
+
obj2 = klass.where(klass.arel_table[:path].st_length.eq(2)).first
|
90
|
+
refute_nil(obj2)
|
91
|
+
assert_equal(id, obj2.id)
|
92
|
+
obj3 = klass.where(klass.arel_table[:path].st_length.gt(3)).first
|
93
|
+
assert_nil(obj3)
|
103
94
|
end
|
95
|
+
|
104
96
|
end
|
105
97
|
end
|
data/test/tasks_test.rb
CHANGED
@@ -1,160 +1,152 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
require 'active_record/schema_dumper'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
t.spatial "latlon2", limit: { srid: 4326, type: "point", geographic: true }
|
94
|
-
end
|
95
|
-
::File.open(tmp_sql_filename, "w:utf-8") do |file|
|
96
|
-
::ActiveRecord::SchemaDumper.dump(connection, file)
|
97
|
-
end
|
98
|
-
data = ::File.read(tmp_sql_filename)
|
99
|
-
assert(data.index('t.spatial "latlon1", limit: {:srid=>4326, :type=>"point", :geographic=>true}'))
|
100
|
-
assert(data.index('t.spatial "latlon2", limit: {:srid=>4326, :type=>"point", :geographic=>true}'))
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_index_schema_dump
|
104
|
-
setup_database_tasks
|
105
|
-
connection.create_table(:spatial_test) do |t|
|
106
|
-
t.point "latlon", geographic: true
|
107
|
-
end
|
108
|
-
connection.add_index :spatial_test, :latlon, spatial: true
|
109
|
-
::File.open(tmp_sql_filename, "w:utf-8") do |file|
|
110
|
-
::ActiveRecord::SchemaDumper.dump(connection, file)
|
111
|
-
end
|
112
|
-
data = ::File.read(tmp_sql_filename)
|
113
|
-
assert data.index('t.spatial "latlon", limit: {:srid=>4326, :type=>"point", :geographic=>true}')
|
114
|
-
assert data.index('add_index "spatial_test", ["latlon"], :name => "index_spatial_test_on_latlon", :spatial => true')
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_add_index_with_nil_options
|
118
|
-
setup_database_tasks
|
119
|
-
connection.create_table(:test) do |t|
|
120
|
-
t.string "name"
|
121
|
-
end
|
122
|
-
connection.add_index :test, :name, nil
|
123
|
-
::ActiveRecord::Tasks::DatabaseTasks.structure_dump(TasksTest.new_database_config, tmp_sql_filename)
|
124
|
-
data = ::File.read(tmp_sql_filename)
|
125
|
-
assert data.index('CREATE INDEX index_test_on_name ON test USING btree (name);')
|
126
|
-
end
|
127
|
-
|
128
|
-
def test_add_index_via_references
|
129
|
-
setup_database_tasks
|
130
|
-
connection.create_table(:cats)
|
131
|
-
connection.create_table(:dogs) do |t|
|
132
|
-
t.references :cats, index: true
|
133
|
-
end
|
134
|
-
::ActiveRecord::Tasks::DatabaseTasks.structure_dump(TasksTest.new_database_config, tmp_sql_filename)
|
135
|
-
data = ::File.read(tmp_sql_filename)
|
136
|
-
assert data.index('CREATE INDEX index_dogs_on_cats_id ON dogs USING btree (cats_id);')
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
private
|
141
|
-
|
142
|
-
def connection
|
143
|
-
::ActiveRecord::Base.connection
|
144
|
-
end
|
145
|
-
|
146
|
-
def tmp_sql_filename
|
147
|
-
::File.expand_path('../tmp/tmp.sql', ::File.dirname(__FILE__))
|
148
|
-
end
|
149
|
-
|
150
|
-
def setup_database_tasks
|
151
|
-
::FileUtils.rm_f(tmp_sql_filename)
|
152
|
-
::FileUtils.mkdir_p(::File.dirname(tmp_sql_filename))
|
153
|
-
::ActiveRecord::Tasks::DatabaseTasks.create(TasksTest.new_database_config)
|
154
|
-
end
|
155
|
-
|
156
|
-
end
|
4
|
+
class TasksTest < ActiveSupport::TestCase # :nodoc:
|
5
|
+
DATABASE_CONFIG_PATH = ::File.dirname(__FILE__) + '/database.yml'
|
6
|
+
OVERRIDE_DATABASE_CONFIG_PATH = ::File.dirname(__FILE__) + '/database_local.yml'
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def before_open_database(args)
|
10
|
+
@new_database_config = args[:config].merge('database' => 'postgis_adapter_test2')
|
11
|
+
@new_database_config.stringify_keys!
|
12
|
+
end
|
13
|
+
attr_reader :new_database_config
|
14
|
+
end
|
15
|
+
|
16
|
+
include RGeo::ActiveRecord::AdapterTestHelper
|
17
|
+
|
18
|
+
def cleanup_tables
|
19
|
+
::ActiveRecord::Base.remove_connection
|
20
|
+
::ActiveRecord::Base.clear_active_connections!
|
21
|
+
TasksTest::DEFAULT_AR_CLASS.connection.execute("DROP DATABASE IF EXISTS \"postgis_adapter_test2\"")
|
22
|
+
end
|
23
|
+
|
24
|
+
define_test_methods do
|
25
|
+
def test_create_database_from_extension_in_public_schema
|
26
|
+
::ActiveRecord::Tasks::DatabaseTasks.create(TasksTest.new_database_config)
|
27
|
+
refute_empty connection.select_values("SELECT * from public.spatial_ref_sys")
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_empty_sql_dump
|
31
|
+
setup_database_tasks
|
32
|
+
::ActiveRecord::Tasks::DatabaseTasks.structure_dump(TasksTest.new_database_config, tmp_sql_filename)
|
33
|
+
sql = ::File.read(tmp_sql_filename)
|
34
|
+
assert(sql !~ /CREATE TABLE/)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_basic_geography_sql_dump
|
38
|
+
setup_database_tasks
|
39
|
+
connection.create_table(:spatial_test) do |t|
|
40
|
+
t.point "latlon", geographic: true
|
41
|
+
end
|
42
|
+
::ActiveRecord::Tasks::DatabaseTasks.structure_dump(TasksTest.new_database_config, tmp_sql_filename)
|
43
|
+
data = ::File.read(tmp_sql_filename)
|
44
|
+
assert(data.index('latlon geography(Point,4326)'))
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_index_sql_dump
|
48
|
+
setup_database_tasks
|
49
|
+
connection.create_table(:spatial_test) do |t|
|
50
|
+
t.point "latlon", geographic: true
|
51
|
+
t.string "name"
|
52
|
+
end
|
53
|
+
connection.add_index :spatial_test, :latlon, spatial: true
|
54
|
+
connection.add_index :spatial_test, :name, using: :btree
|
55
|
+
::ActiveRecord::Tasks::DatabaseTasks.structure_dump(TasksTest.new_database_config, tmp_sql_filename)
|
56
|
+
data = ::File.read(tmp_sql_filename)
|
57
|
+
assert(data.index('latlon geography(Point,4326)'))
|
58
|
+
assert data.index('CREATE INDEX index_spatial_test_on_latlon ON spatial_test USING gist (latlon);')
|
59
|
+
assert data.index('CREATE INDEX index_spatial_test_on_name ON spatial_test USING btree (name);')
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_empty_schema_dump
|
63
|
+
setup_database_tasks
|
64
|
+
::File.open(tmp_sql_filename, "w:utf-8") do |file|
|
65
|
+
::ActiveRecord::SchemaDumper.dump(::ActiveRecord::Base.connection, file)
|
66
|
+
end
|
67
|
+
data = ::File.read(tmp_sql_filename)
|
68
|
+
assert(data.index('ActiveRecord::Schema'))
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_basic_geometry_schema_dump
|
72
|
+
setup_database_tasks
|
73
|
+
connection.create_table(:spatial_test) do |t|
|
74
|
+
t.geometry 'object1'
|
75
|
+
t.spatial "object2", limit: { srid: connection.default_srid, type: "geometry" }
|
76
|
+
end
|
77
|
+
::File.open(tmp_sql_filename, "w:utf-8") do |file|
|
78
|
+
::ActiveRecord::SchemaDumper.dump(connection, file)
|
79
|
+
end
|
80
|
+
data = ::File.read(tmp_sql_filename)
|
81
|
+
assert(data.index("t.spatial \"object1\", limit: {:srid=>#{connection.default_srid}, :type=>\"geometry\"}"))
|
82
|
+
assert(data.index("t.spatial \"object2\", limit: {:srid=>#{connection.default_srid}, :type=>\"geometry\"}"))
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_basic_geography_schema_dump
|
86
|
+
setup_database_tasks
|
87
|
+
connection.create_table(:spatial_test) do |t|
|
88
|
+
t.point "latlon1", geographic: true
|
89
|
+
t.spatial "latlon2", limit: { srid: 4326, type: "point", geographic: true }
|
90
|
+
end
|
91
|
+
::File.open(tmp_sql_filename, "w:utf-8") do |file|
|
92
|
+
::ActiveRecord::SchemaDumper.dump(connection, file)
|
157
93
|
end
|
94
|
+
data = ::File.read(tmp_sql_filename)
|
95
|
+
assert(data.index('t.spatial "latlon1", limit: {:srid=>4326, :type=>"point", :geographic=>true}'))
|
96
|
+
assert(data.index('t.spatial "latlon2", limit: {:srid=>4326, :type=>"point", :geographic=>true}'))
|
158
97
|
end
|
98
|
+
|
99
|
+
def test_index_schema_dump
|
100
|
+
setup_database_tasks
|
101
|
+
connection.create_table(:spatial_test) do |t|
|
102
|
+
t.point "latlon", geographic: true
|
103
|
+
end
|
104
|
+
connection.add_index :spatial_test, :latlon, spatial: true
|
105
|
+
::File.open(tmp_sql_filename, "w:utf-8") do |file|
|
106
|
+
::ActiveRecord::SchemaDumper.dump(connection, file)
|
107
|
+
end
|
108
|
+
data = ::File.read(tmp_sql_filename)
|
109
|
+
assert data.index('t.spatial "latlon", limit: {:srid=>4326, :type=>"point", :geographic=>true}')
|
110
|
+
assert data.index('add_index "spatial_test", ["latlon"], :name => "index_spatial_test_on_latlon", :spatial => true')
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_add_index_with_nil_options
|
114
|
+
setup_database_tasks
|
115
|
+
connection.create_table(:test) do |t|
|
116
|
+
t.string "name"
|
117
|
+
end
|
118
|
+
connection.add_index :test, :name, nil
|
119
|
+
::ActiveRecord::Tasks::DatabaseTasks.structure_dump(TasksTest.new_database_config, tmp_sql_filename)
|
120
|
+
data = ::File.read(tmp_sql_filename)
|
121
|
+
assert data.index('CREATE INDEX index_test_on_name ON test USING btree (name);')
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_add_index_via_references
|
125
|
+
setup_database_tasks
|
126
|
+
connection.create_table(:cats)
|
127
|
+
connection.create_table(:dogs) do |t|
|
128
|
+
t.references :cats, index: true
|
129
|
+
end
|
130
|
+
::ActiveRecord::Tasks::DatabaseTasks.structure_dump(TasksTest.new_database_config, tmp_sql_filename)
|
131
|
+
data = ::File.read(tmp_sql_filename)
|
132
|
+
assert data.index('CREATE INDEX index_dogs_on_cats_id ON dogs USING btree (cats_id);')
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
private
|
137
|
+
|
138
|
+
def connection
|
139
|
+
::ActiveRecord::Base.connection
|
159
140
|
end
|
141
|
+
|
142
|
+
def tmp_sql_filename
|
143
|
+
::File.expand_path('../tmp/tmp.sql', ::File.dirname(__FILE__))
|
144
|
+
end
|
145
|
+
|
146
|
+
def setup_database_tasks
|
147
|
+
::FileUtils.rm_f(tmp_sql_filename)
|
148
|
+
::FileUtils.mkdir_p(::File.dirname(tmp_sql_filename))
|
149
|
+
::ActiveRecord::Tasks::DatabaseTasks.create(TasksTest.new_database_config)
|
150
|
+
end
|
151
|
+
|
160
152
|
end
|