activerecord-postgis-adapter 0.3.4 → 0.3.5
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/History.rdoc +4 -0
- data/Version +1 -1
- data/lib/active_record/connection_adapters/postgis_adapter/databases.rake +147 -0
- metadata +3 -2
data/History.rdoc
CHANGED
data/Version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.5
|
@@ -0,0 +1,147 @@
|
|
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
|
+
has_su_ = config_.include?('su_username') # Is there a distinct superuser?
|
51
|
+
username_ = config_['username'] # regular user name
|
52
|
+
su_username_ = config_['su_username'] || username_ # superuser name
|
53
|
+
su_password_ = config_['su_password'] || config_['password'] # superuser password
|
54
|
+
|
55
|
+
# Create the database. Optionally do so as the given superuser.
|
56
|
+
# But make sure the database is owned by the regular user.
|
57
|
+
::ActiveRecord::Base.establish_connection(config_.merge('database' => 'postgres', 'schema_search_path' => 'public', 'username' => su_username_, 'password' => su_password_))
|
58
|
+
extra_configs_ = {'encoding' => @encoding}
|
59
|
+
extra_configs_['owner'] = username_ if has_su_
|
60
|
+
::ActiveRecord::Base.connection.create_database(config_['database'], config_.merge(extra_configs_))
|
61
|
+
|
62
|
+
# Initial setup of the database: Add schemas from the search path.
|
63
|
+
# If a superuser is given, we log in as the superuser, but we make sure
|
64
|
+
# the schemas are owned by the regular user.
|
65
|
+
::ActiveRecord::Base.establish_connection(config_.merge('schema_search_path' => 'public', 'username' => su_username_, 'password' => su_password_))
|
66
|
+
conn_ = ::ActiveRecord::Base.connection
|
67
|
+
search_path_ = config_["schema_search_path"].to_s.strip
|
68
|
+
search_path_ = search_path_.split(",").map{ |sp_| sp_.strip }
|
69
|
+
auth_ = has_su_ ? " AUTHORIZATION #{username_}" : ''
|
70
|
+
search_path_.each do |schema_|
|
71
|
+
conn_.execute("CREATE SCHEMA #{schema_}#{auth_}") unless schema_.downcase == 'public'
|
72
|
+
end
|
73
|
+
|
74
|
+
# Define the postgis stuff, if the script_dir is provided.
|
75
|
+
# Note: a superuser is required to run the postgis definitions.
|
76
|
+
# If a separate superuser is provided, we need to grant privileges on
|
77
|
+
# the postgis definitions over to the regular user afterwards.
|
78
|
+
# The schema for the postgis definitions is chosen as follows:
|
79
|
+
# If "postgis" is present in the search path, use it.
|
80
|
+
# Otherwise, use the last schema in the search path.
|
81
|
+
# If no search path is given, use "public".
|
82
|
+
if (script_dir_ = config_['script_dir'])
|
83
|
+
postgis_schema_ = search_path_.include?('postgis') ? 'postgis' : (search_path_.last || 'public')
|
84
|
+
conn_.execute("SET search_path TO #{postgis_schema_}")
|
85
|
+
conn_.execute(::File.read(::File.expand_path('postgis.sql', script_dir_)))
|
86
|
+
conn_.execute(::File.read(::File.expand_path('spatial_ref_sys.sql', script_dir_)))
|
87
|
+
if has_su_
|
88
|
+
conn_.execute("GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA #{postgis_schema_} TO #{username_}")
|
89
|
+
conn_.execute("GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA #{postgis_schema_} TO #{username_}")
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# Done
|
94
|
+
::ActiveRecord::Base.establish_connection(config_)
|
95
|
+
rescue ::Exception => e_
|
96
|
+
$stderr.puts(e_, *(e_.backtrace))
|
97
|
+
$stderr.puts("Couldn't create database for #{config_.inspect}")
|
98
|
+
end
|
99
|
+
else
|
100
|
+
create_database_without_postgis(config_)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
|
105
|
+
def drop_database(config_)
|
106
|
+
if config_['adapter'] == 'postgis'
|
107
|
+
::ActiveRecord::Base.establish_connection(config_.merge('database' => 'postgres', 'schema_search_path' => 'public', 'username' => config_['su_username'] || config_['username'], 'password' => config_['su_password'] || config_['password']))
|
108
|
+
::ActiveRecord::Base.connection.drop_database(config_['database'])
|
109
|
+
else
|
110
|
+
drop_database_without_postgis(config_)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
|
115
|
+
::RGeo::ActiveRecord::TaskHacker.modify('db:charset', nil, 'postgis') do |config_|
|
116
|
+
::ActiveRecord::Base.establish_connection(config_)
|
117
|
+
puts(::ActiveRecord::Base.connection.encoding)
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
::RGeo::ActiveRecord::TaskHacker.modify('db:structure:dump', nil, 'postgis') do |config_|
|
122
|
+
::ENV['PGHOST'] = config_["host"] if config_["host"]
|
123
|
+
::ENV['PGPORT'] = config_["port"].to_s if config_["port"]
|
124
|
+
::ENV['PGPASSWORD'] = config_["password"].to_s if config_["password"]
|
125
|
+
search_path_ = config_["schema_search_path"].to_s.strip
|
126
|
+
search_path_ = search_path_.split(",").map{ |sp_| sp_.strip }
|
127
|
+
search_path_.delete('postgis')
|
128
|
+
search_path_ = ['public'] if search_path_.length == 0
|
129
|
+
search_path_ = search_path_.map{ |sp_| "--schema=#{sp_}" }.join(" ")
|
130
|
+
`pg_dump -i -U "#{config_["username"]}" -s -x -O -f db/#{::Rails.env}_structure.sql #{search_path_} #{config_["database"]}`
|
131
|
+
raise "Error dumping database" if $?.exitstatus == 1
|
132
|
+
end
|
133
|
+
|
134
|
+
|
135
|
+
::RGeo::ActiveRecord::TaskHacker.modify('db:test:clone_structure', 'test', 'postgis') do |config_|
|
136
|
+
::ENV['PGHOST'] = config_["host"] if config_["host"]
|
137
|
+
::ENV['PGPORT'] = config_["port"].to_s if config_["port"]
|
138
|
+
::ENV['PGPASSWORD'] = config_["password"].to_s if config_["password"]
|
139
|
+
`psql -U "#{config_["username"]}" -f #{::Rails.root}/db/#{::Rails.env}_structure.sql #{config_["database"]}`
|
140
|
+
end
|
141
|
+
|
142
|
+
|
143
|
+
::RGeo::ActiveRecord::TaskHacker.modify('db:test:purge', 'test', 'postgis') do |config_|
|
144
|
+
::ActiveRecord::Base.clear_active_connections!
|
145
|
+
drop_database(config_)
|
146
|
+
create_database(config_)
|
147
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: activerecord-postgis-adapter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.3.
|
5
|
+
version: 0.3.5
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Daniel Azuma
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-04-
|
13
|
+
date: 2011-04-13 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rgeo-activerecord
|
@@ -52,6 +52,7 @@ files:
|
|
52
52
|
- lib/active_record/connection_adapters/postgis_adapter/version.rb
|
53
53
|
- lib/active_record/connection_adapters/postgis_adapter.rb
|
54
54
|
- lib/rgeo/active_record/postgis_adapter/railtie.rb
|
55
|
+
- lib/active_record/connection_adapters/postgis_adapter/databases.rake
|
55
56
|
- test/tc_basic.rb
|
56
57
|
- test/tc_ddl.rb
|
57
58
|
- test/tc_spatial_queries.rb
|