activerecord-postgis-adapter 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/History.rdoc CHANGED
@@ -1,3 +1,10 @@
1
+ === 0.2.3 / 2011-01-06
2
+
3
+ * Many of ActiveRecord's rake tasks weren't working because they need to know about every adapter explicitly. I hesitate to call this "fixed" since I see it as a problem in ActiveRecord, but we now at least have a workaround so the rake tasks will run properly. (Reported by Tad Thorley.)
4
+ * Dumping schema.rb now omits the PostGIS internal tables.
5
+ * Added a new configuration parameter pointing to the script directory, for rake db:create.
6
+ * If the "postgis" schema is included in the schema search path, it is used as a container for the PostGIS internal definitions when running rake db:create. Furthermore, that schema is omitted when dumping the structure.sql. This should eliminate all the PostGIS internal cruft from SQL structure dumps, and also eliminate the errors that would appear when rebuilding a test database because the PostGIS internals would get applied twice.
7
+
1
8
  === 0.2.2 / 2010-12-27
2
9
 
3
10
  * Support for basic spatial equality queries. e.g. constructs such as:
data/README.rdoc CHANGED
@@ -7,13 +7,7 @@ the {RGeo}[http://github.com/dazuma/rgeo] library to represent spatial
7
7
  data in Ruby. Like the standard postgresql adapter, this adapter requires
8
8
  the pg gem.
9
9
 
10
- === Usage
11
-
12
- To use this adapter, add this gem, "activerecord-postgis-adapter", to
13
- your Gemfile, and then request the adapter name "postgis" in your
14
- database connection configuration (which, for a Rails application, is in
15
- the config/database.yml file). The other database connection configuration
16
- parameters are the same as for the stock postgresql adapter.
10
+ === What This Adapter Provides
17
11
 
18
12
  First, this adapter extends the migration syntax to support creating
19
13
  spatial columns and indexes. To create a spatial column, use the
@@ -83,19 +77,20 @@ You can also use WKT:
83
77
 
84
78
  rec = SpatialTable.where(:latlon => 'POINT(-122 47)').first
85
79
 
86
- Other types of queries currently must be written in SQL. However, we are
87
- investigating writing a set of Arel extensions for constructing arbitrary
88
- spatial queries.
80
+ Other types of queries (e.g. radius searches) currently must be written
81
+ in SQL. We are investigating writing a set of Arel extensions for
82
+ constructing arbitrary spatial queries. However, these extensions will
83
+ probably require Rails 3.1.
89
84
 
90
- === Installation
85
+ === Installing The Adapter Gem
91
86
 
92
87
  This adapter has the following requirements:
93
88
 
94
89
  * Ruby 1.8.7 or later. Ruby 1.9.2 or later preferred.
95
90
  * PostGIS 1.5 or later.
96
91
  * \ActiveRecord 3.0.3 or later. Earlier versions will not work.
97
- * rgeo gem 0.2.3 or later.
98
- * rgeo-activerecord gem 0.2.1 or later.
92
+ * rgeo gem 0.2.4 or later.
93
+ * rgeo-activerecord gem 0.2.2 or later.
99
94
  * pg gem 0.10 or later.
100
95
 
101
96
  Install this adapter as a gem:
@@ -105,6 +100,58 @@ Install this adapter as a gem:
105
100
  See the README for the "rgeo" gem, a required dependency, for further
106
101
  installation information.
107
102
 
103
+ === Setting Up The Adapter
104
+
105
+ To use this adapter, add this gem, "activerecord-postgis-adapter", to
106
+ your Gemfile, and then request the adapter name "postgis" in your
107
+ database connection configuration (which, for a Rails application, is in
108
+ the config/database.yml file).
109
+
110
+ Furthermore, the PostGIS adapter includes a special railtie that provides
111
+ support for PostGIS databases in ActiveRecord's rake tasks. This railtie
112
+ is required in order to run, e.g., rake test. To install this railtie,
113
+ you should add this line to your config/application.rb:
114
+
115
+ require 'rgeo/active_record/postgis_adapter/railtie'
116
+
117
+ Note that this railtie must load after the ActiveRecord railtie. That is,
118
+ the above require command should appear after <tt>require 'rails/all'</tt>.
119
+
120
+ Besides the adapter name "postgis", most of the other database connection
121
+ configuration parameters are the same as for the stock postgresql adapter.
122
+ However, there are a few special parameters you may want to set.
123
+
124
+ The <i>script_dir</i> parameter provides the path to the directory
125
+ containing the SQL scripts for PostGIS installation. i.e. this could be
126
+ <tt>/usr/local/share/contrib/postgis-1.5</tt>. This directory should
127
+ contain the files <tt>postgis.sql</tt> and <tt>spatial_ref_sys.sql</tt>.
128
+ It is used by the db:create rake task to add the PostGIS types, functions,
129
+ and tables to a newly created database. If you do not provide this
130
+ parameter, you will need to add these objects to the database manually.
131
+ Generally, therefore, this parameter is required at least for the test
132
+ database, which is usually automatically created by the rake tasks.
133
+
134
+ Because PostGIS adds many objects to the database, you may encounter a few
135
+ issues when running Rails rake tasks that manage databases. Specifically:
136
+
137
+ * Dumping the structure as sql (rake db:structure:dump) may include all
138
+ the PostGIS definitions as well, cluttering your SQL dump.
139
+ * Rake tasks that automatically create the test database (e.g. rake test)
140
+ may emit a number of errors because PostGIS definitions are being added
141
+ twice to the database: once on creation, and again because they are
142
+ showing up in the SQL dump that ActiveRecord uses to copy the schema.
143
+
144
+ Because of these issues, it is recommended that you include the schema
145
+ name "postgis" in the <i>schema_search_path</i> parameter. The PostGIS
146
+ adapter treats this schema name as special, in that:
147
+
148
+ * The db:create rake task will automatically create the "postgis" schema,
149
+ and will create all the PostGIS objects in that schema.
150
+ * Dumping the structure as sql will omit objects in the "postgis" schema.
151
+
152
+ As a result, the PostGIS definitions will no longer appear in the SQL
153
+ dump file, effectively eliminating the above issues.
154
+
108
155
  === Known bugs and limitations
109
156
 
110
157
  A few minor holes still exist in this adapter. Notably, using \ActiveRecord
data/Version CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.2.3
@@ -95,7 +95,7 @@ module ActiveRecord
95
95
 
96
96
  # The postgres drivers don't allow the creation of an unconnected PGconn object,
97
97
  # so just pass a nil connection object for the time being.
98
- ConnectionAdapters::PostGisAdapter.new(nil, logger, [host_, port_, nil, nil, database_, username_, password_], config_)
98
+ ConnectionAdapters::PostgisAdapter.new(nil, logger, [host_, port_, nil, nil, database_, username_, password_], config_)
99
99
  end
100
100
 
101
101
 
@@ -105,7 +105,7 @@ module ActiveRecord
105
105
  module ConnectionAdapters # :nodoc:
106
106
 
107
107
 
108
- class PostGisAdapter < PostgreSQLAdapter # :nodoc:
108
+ class PostgisAdapter < PostgreSQLAdapter # :nodoc:
109
109
 
110
110
 
111
111
  ADAPTER_NAME = 'PostGIS'.freeze
@@ -416,7 +416,7 @@ module ActiveRecord
416
416
 
417
417
 
418
418
  def type_cast_code(var_name_)
419
- type == :geometry ? "::ActiveRecord::ConnectionAdapters::PostGisAdapter::SpatialColumn.convert_to_geometry(#{var_name_}, self.class, #{name.inspect}, #{@geographic ? 'true' : 'false'}, #{@srid.inspect}, #{@has_z ? 'true' : 'false'}, #{@has_m ? 'true' : 'false'})" : super
419
+ type == :geometry ? "::ActiveRecord::ConnectionAdapters::PostgisAdapter::SpatialColumn.convert_to_geometry(#{var_name_}, self.class, #{name.inspect}, #{@geographic ? 'true' : 'false'}, #{@srid.inspect}, #{@has_z ? 'true' : 'false'}, #{@has_m ? 'true' : 'false'})" : super
420
420
  end
421
421
 
422
422
 
@@ -463,3 +463,8 @@ module ActiveRecord
463
463
 
464
464
 
465
465
  end
466
+
467
+
468
+ ignore_tables_ = ::ActiveRecord::SchemaDumper.ignore_tables
469
+ ignore_tables_ << 'geometry_columns' unless ignore_tables_.include?('geometry_columns')
470
+ ignore_tables_ << 'spatial_ref_sys' unless ignore_tables_.include?('spatial_ref_sys')
@@ -0,0 +1,117 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Rakefile changes for PostGIS adapter
4
+ #
5
+ # -----------------------------------------------------------------------------
6
+ # Copyright 2010 Daniel Azuma
7
+ #
8
+ # All rights reserved.
9
+ #
10
+ # Redistribution and use in source and binary forms, with or without
11
+ # modification, are permitted provided that the following conditions are met:
12
+ #
13
+ # * Redistributions of source code must retain the above copyright notice,
14
+ # this list of conditions and the following disclaimer.
15
+ # * Redistributions in binary form must reproduce the above copyright notice,
16
+ # this list of conditions and the following disclaimer in the documentation
17
+ # and/or other materials provided with the distribution.
18
+ # * Neither the name of the copyright holder, nor the names of any other
19
+ # contributors to this software, may be used to endorse or promote products
20
+ # derived from this software without specific prior written permission.
21
+ #
22
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
+ # POSSIBILITY OF SUCH DAMAGE.
33
+ # -----------------------------------------------------------------------------
34
+ ;
35
+
36
+
37
+ require 'rgeo/active_record/task_hacker'
38
+
39
+
40
+ class Object
41
+ alias_method :create_database_without_postgis, :create_database
42
+ alias_method :drop_database_without_postgis, :drop_database
43
+ end
44
+
45
+
46
+ def create_database(config_)
47
+ if config_['adapter'] == 'postgis'
48
+ @encoding = config_['encoding'] || ::ENV['CHARSET'] || 'utf8'
49
+ begin
50
+ ::ActiveRecord::Base.establish_connection(config_.merge('database' => 'postgres', 'schema_search_path' => 'public'))
51
+ ::ActiveRecord::Base.connection.create_database(config_['database'], config_.merge('encoding' => @encoding))
52
+ ::ActiveRecord::Base.establish_connection(config_.merge('schema_search_path' => 'public'))
53
+ if (script_dir_ = config_['script_dir'])
54
+ conn_ = ::ActiveRecord::Base.connection
55
+ search_path_ = config_["schema_search_path"].to_s.strip
56
+ search_path_ = search_path_.split(",").map{ |sp_| sp_.strip }
57
+ if search_path_.include?('postgis')
58
+ conn_.execute('CREATE SCHEMA postgis')
59
+ conn_.execute('SET search_path TO postgis')
60
+ end
61
+ conn_.execute(::File.read(::File.expand_path('postgis.sql', script_dir_)))
62
+ conn_.execute(::File.read(::File.expand_path('spatial_ref_sys.sql', script_dir_)))
63
+ end
64
+ ::ActiveRecord::Base.establish_connection(config_)
65
+ rescue ::Exception => e_
66
+ $stderr.puts(e_, *(e_.backtrace))
67
+ $stderr.puts("Couldn't create database for #{config.inspect}")
68
+ end
69
+ else
70
+ create_database_without_postgis(config_)
71
+ end
72
+ end
73
+
74
+
75
+ def drop_database(config_)
76
+ if config_['adapter'] == 'postgis'
77
+ ::ActiveRecord::Base.establish_connection(config_.merge('database' => 'postgres', 'schema_search_path' => 'public'))
78
+ ::ActiveRecord::Base.connection.drop_database(config_['database'])
79
+ else
80
+ drop_database_without_postgis(config_)
81
+ end
82
+ end
83
+
84
+
85
+ ::RGeo::ActiveRecord::TaskHacker.modify('db:charset', nil, 'postgis') do |config_|
86
+ ::ActiveRecord::Base.establish_connection(config_)
87
+ puts(::ActiveRecord::Base.connection.encoding)
88
+ end
89
+
90
+
91
+ ::RGeo::ActiveRecord::TaskHacker.modify('db:structure:dump', nil, 'postgis') do |config_|
92
+ ::ENV['PGHOST'] = config_["host"] if config_["host"]
93
+ ::ENV['PGPORT'] = config_["port"].to_s if config_["port"]
94
+ ::ENV['PGPASSWORD'] = config_["password"].to_s if config_["password"]
95
+ search_path_ = config_["schema_search_path"].to_s.strip
96
+ search_path_ = search_path_.split(",").map{ |sp_| sp_.strip }
97
+ search_path_.delete('postgis')
98
+ search_path_ = ['public'] if search_path_.length == 0
99
+ search_path_ = search_path_.map{ |sp_| "--schema=#{sp_}" }.join(" ")
100
+ `pg_dump -i -U "#{config_["username"]}" -s -x -O -f db/#{::Rails.env}_structure.sql #{search_path_} #{config_["database"]}`
101
+ raise "Error dumping database" if $?.exitstatus == 1
102
+ end
103
+
104
+
105
+ ::RGeo::ActiveRecord::TaskHacker.modify('db:test:clone_structure', 'test', 'postgis') do |config_|
106
+ ::ENV['PGHOST'] = config_["host"] if config_["host"]
107
+ ::ENV['PGPORT'] = config_["port"].to_s if config_["port"]
108
+ ::ENV['PGPASSWORD'] = config_["password"].to_s if config_["password"]
109
+ `psql -U "#{config_["username"]}" -f #{::Rails.root}/db/#{::Rails.env}_structure.sql #{config_["database"]}`
110
+ end
111
+
112
+
113
+ ::RGeo::ActiveRecord::TaskHacker.modify('db:test:purge', 'test', 'postgis') do |config_|
114
+ ::ActiveRecord::Base.clear_active_connections!
115
+ drop_database(config_)
116
+ create_database(config_)
117
+ end
@@ -0,0 +1,58 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Railtie for PostGIS adapter
4
+ #
5
+ # -----------------------------------------------------------------------------
6
+ # Copyright 2010 Daniel Azuma
7
+ #
8
+ # All rights reserved.
9
+ #
10
+ # Redistribution and use in source and binary forms, with or without
11
+ # modification, are permitted provided that the following conditions are met:
12
+ #
13
+ # * Redistributions of source code must retain the above copyright notice,
14
+ # this list of conditions and the following disclaimer.
15
+ # * Redistributions in binary form must reproduce the above copyright notice,
16
+ # this list of conditions and the following disclaimer in the documentation
17
+ # and/or other materials provided with the distribution.
18
+ # * Neither the name of the copyright holder, nor the names of any other
19
+ # contributors to this software, may be used to endorse or promote products
20
+ # derived from this software without specific prior written permission.
21
+ #
22
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
+ # POSSIBILITY OF SUCH DAMAGE.
33
+ # -----------------------------------------------------------------------------
34
+ ;
35
+
36
+
37
+ require 'rails/railtie'
38
+
39
+
40
+ module RGeo
41
+
42
+ module ActiveRecord
43
+
44
+ module PostgisAdapter
45
+
46
+ class Railtie < ::Rails::Railtie
47
+
48
+ rake_tasks do
49
+ load "rgeo/active_record/postgis_adapter/databases.rake"
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+
56
+ end
57
+
58
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 2
9
- version: 0.2.2
8
+ - 3
9
+ version: 0.2.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Daniel Azuma
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-27 00:00:00 -08:00
17
+ date: 2011-01-06 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -28,8 +28,8 @@ dependencies:
28
28
  segments:
29
29
  - 0
30
30
  - 2
31
- - 1
32
- version: 0.2.1
31
+ - 2
32
+ version: 0.2.2
33
33
  type: :runtime
34
34
  version_requirements: *id001
35
35
  - !ruby/object:Gem::Dependency
@@ -58,6 +58,8 @@ extra_rdoc_files:
58
58
  - README.rdoc
59
59
  files:
60
60
  - lib/active_record/connection_adapters/postgis_adapter.rb
61
+ - lib/rgeo/active_record/postgis_adapter/railtie.rb
62
+ - lib/rgeo/active_record/postgis_adapter/databases.rake
61
63
  - History.rdoc
62
64
  - README.rdoc
63
65
  - test/tc_basic.rb