postgres_ext 0.0.10 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,3 +2,11 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in postgres_ext.gemspec
4
4
  gemspec
5
+ unless ENV['CI']
6
+ if RUBY_PLATFORM =~ /java/
7
+ gem 'ruby-debug'
8
+ elsif RUBY_VERSION == '1.9.3'
9
+ gem 'debugger', '~> 1.1.2'
10
+ end
11
+ end
12
+ gem 'fivemat'
@@ -11,6 +11,7 @@ module ActiveRecord
11
11
  class PostgreSQLColumn
12
12
  include PgArrayParser
13
13
  attr_accessor :array
14
+
14
15
  def initialize_with_extended_types(name, default, sql_type = nil, null = true)
15
16
  if sql_type =~ /\[\]$/
16
17
  @array = true
@@ -21,6 +22,7 @@ module ActiveRecord
21
22
  end
22
23
  end
23
24
  alias_method_chain :initialize, :extended_types
25
+
24
26
  def klass_with_extended_types
25
27
  case type
26
28
  when :inet, :cidr then IPAddr
@@ -29,6 +31,7 @@ module ActiveRecord
29
31
  end
30
32
  end
31
33
  alias_method_chain :klass, :extended_types
34
+
32
35
  def type_cast_with_extended_types(value)
33
36
  return nil if value.nil?
34
37
  return coder.load(value) if encoded?
@@ -47,6 +50,7 @@ module ActiveRecord
47
50
  end
48
51
  end
49
52
  alias_method_chain :type_cast, :extended_types
53
+
50
54
  def string_to_array(value)
51
55
  if Array === value
52
56
  value
@@ -119,22 +123,14 @@ module ActiveRecord
119
123
  end
120
124
 
121
125
  class PostgreSQLAdapter
126
+ class UnsupportedFeature < Exception; end
127
+
122
128
  EXTENDED_TYPES = {:inet => {:name => 'inet'}, :cidr => {:name => 'cidr'}, :macaddr => {:name => 'macaddr'}, :uuid => {:name => 'uuid'}}
129
+
123
130
  class ColumnDefinition < ActiveRecord::ConnectionAdapters::ColumnDefinition
124
131
  attr_accessor :array
125
132
  end
126
133
 
127
- # Translate from the current database encoding to the encoding we
128
- # will force string array components into on retrievial.
129
- def encoding_for_ruby
130
- @database_encoding ||= case ActiveRecord::Base.connection.encoding
131
- when 'UTF8'
132
- 'UTF-8'
133
- else
134
- ActiveRecord::Base.connection.encoding
135
- end
136
- end
137
-
138
134
  class TableDefinition
139
135
  EXTENDED_TYPES.keys.map(&:to_s).each do |column_type|
140
136
  class_eval <<-EOV, __FILE__, __LINE__ + 1
@@ -157,6 +153,7 @@ module ActiveRecord
157
153
  end
158
154
 
159
155
  private
156
+
160
157
  def new_column_definition(base, name, type)
161
158
  definition = ColumnDefinition.new base, name, type
162
159
  @columns << definition
@@ -192,6 +189,21 @@ module ActiveRecord
192
189
 
193
190
  NATIVE_DATABASE_TYPES.merge!(EXTENDED_TYPES)
194
191
 
192
+ # Translate from the current database encoding to the encoding we
193
+ # will force string array components into on retrievial.
194
+ def encoding_for_ruby
195
+ @database_encoding ||= case ActiveRecord::Base.connection.encoding
196
+ when 'UTF8'
197
+ 'UTF-8'
198
+ else
199
+ ActiveRecord::Base.connection.encoding
200
+ end
201
+ end
202
+
203
+ def supports_extensions?
204
+ postgresql_version > 90100
205
+ end
206
+
195
207
  def add_column_options!(sql, options)
196
208
  if options[:array] || options[:column].try(:array)
197
209
  sql << '[]'
@@ -210,7 +222,8 @@ module ActiveRecord
210
222
  end
211
223
 
212
224
  def add_extension(extension_name, options={})
213
- execute "CREATE extension if not exists \"#{extension_name}\""
225
+ raise UnsupportedFeature.new('Extensions are not support by this version of PostgreSQL') unless supports_extensions?
226
+ execute "CREATE extension IF NOT EXISTS \"#{extension_name}\""
214
227
  end
215
228
 
216
229
  def change_table(table_name, options = {})
@@ -10,7 +10,7 @@ module ActiveRecord
10
10
  def dump(stream)
11
11
  header(stream)
12
12
  # added
13
- extensions(stream)
13
+ extensions(stream) if @connection.supports_extensions?
14
14
  # /added
15
15
  tables(stream)
16
16
  trailer(stream)
@@ -20,7 +20,7 @@ module ActiveRecord
20
20
  private
21
21
 
22
22
  def extensions(stream)
23
- exts=@connection.extensions
23
+ exts = @connection.extensions
24
24
 
25
25
  unless exts.empty?
26
26
  stream.puts exts.map { |name| " add_extension \"#{name}\""}.join("\n") + "\n\n"
@@ -119,7 +119,7 @@ module ActiveRecord
119
119
  # changed from rails 2.3
120
120
  statement_parts << (':where => ' + index.where.inspect) if index.where
121
121
  statement_parts << (':index_type => ' + index.index_type.inspect) if index.index_type
122
- statement_parts << (':index_opclass => ' + index.index_opclass.inspect) if index.index_opclass
122
+ statement_parts << (':index_opclass => ' + index.index_opclass.inspect) if index.index_opclass.present?
123
123
  # /changed
124
124
 
125
125
  ' ' + statement_parts.join(', ')
@@ -1,3 +1,3 @@
1
1
  module PostgresExt
2
- VERSION = '0.0.10'
2
+ VERSION = '0.1.0'
3
3
  end
data/postgres_ext.gemspec CHANGED
@@ -26,12 +26,4 @@ Gem::Specification.new do |gem|
26
26
  else
27
27
  gem.add_development_dependency 'pg', '~> 0.13.2'
28
28
  end
29
- unless ENV['CI']
30
- if RUBY_PLATFORM =~ /java/
31
- gem.add_development_dependency 'ruby-debug'
32
- elsif RUBY_VERSION == '1.9.3'
33
- gem.add_development_dependency 'debugger', '~> 1.1.2'
34
- end
35
- end
36
- gem.add_development_dependency 'fivemat'
37
29
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe 'index migrations' do
3
+ describe 'Index migrations' do
4
4
  let!(:connection) { ActiveRecord::Base.connection }
5
5
 
6
6
  before do
@@ -35,7 +35,7 @@ describe 'index migrations' do
35
35
  index_2.where.should match /col2 > 50/
36
36
  end
37
37
 
38
- it 'creates indexes with operator classes' do
38
+ it 'creates indexes with operator classes', :if => ActiveRecord::Base.connection.supports_extensions? do
39
39
  lambda do
40
40
  connection.add_index(:index_types, :col3, :index_type => :gin, :index_opclass => :gin_trgm_ops)
41
41
  end.should_not raise_exception
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe 'extension schema dump' do
3
+ describe 'extension schema dump', :if => ActiveRecord::Base.connection.supports_extensions? do
4
4
  let!(:connection) { ActiveRecord::Base.connection }
5
5
  it 'correctly creates and exports database extensions' do
6
6
  stream = StringIO.new
@@ -22,6 +22,7 @@ describe 'Index schema dumper' do
22
22
 
23
23
  output.should match /:index_type => :gin/
24
24
  output.should_not match /:index_type => :btree/
25
+ output.should_not match /:index_opclass =>/
25
26
  end
26
27
 
27
28
  it 'handles index where clauses' do
@@ -34,7 +35,7 @@ describe 'Index schema dumper' do
34
35
  output.should match /:where => "\(col2 > 50\)"/
35
36
  end
36
37
 
37
- it 'dumps index operator classes' do
38
+ it 'dumps index operator classes', :if => ActiveRecord::Base.connection.supports_extensions? do
38
39
  connection.add_index(:index_types, :col3, :index_type => :gin, :index_opclass => :gin_trgm_ops)
39
40
 
40
41
  stream = StringIO.new
data/spec/spec_helper.rb CHANGED
@@ -12,9 +12,10 @@ ENGINE_RAILS_ROOT=File.join(File.dirname(__FILE__), '../')
12
12
  # Requires supporting ruby files with custom matchers and macros, etc,
13
13
  # in spec/support/ and its subdirectories.
14
14
  Dir[File.join(ENGINE_RAILS_ROOT, 'spec/support/**/*.rb')].each { |f| require f }
15
+ require 'postgres_ext'
15
16
 
16
17
  RSpec.configure do |config|
17
- config.before(:suite) { ActiveRecord::Base.connection.add_extension('pg_trgm') }
18
+ config.before(:suite) { ActiveRecord::Base.connection.add_extension('pg_trgm') if ActiveRecord::Base.connection.supports_extensions?}
18
19
  config.use_transactional_fixtures = false
19
20
  config.treat_symbols_as_metadata_keys_with_true_values = true
20
21
  config.mock_with :mocha
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postgres_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-16 00:00:00.000000000 Z
12
+ date: 2012-12-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -107,38 +107,6 @@ dependencies:
107
107
  - - ~>
108
108
  - !ruby/object:Gem::Version
109
109
  version: 0.13.2
110
- - !ruby/object:Gem::Dependency
111
- name: debugger
112
- requirement: !ruby/object:Gem::Requirement
113
- none: false
114
- requirements:
115
- - - ~>
116
- - !ruby/object:Gem::Version
117
- version: 1.1.2
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
- requirements:
123
- - - ~>
124
- - !ruby/object:Gem::Version
125
- version: 1.1.2
126
- - !ruby/object:Gem::Dependency
127
- name: fivemat
128
- requirement: !ruby/object:Gem::Requirement
129
- none: false
130
- requirements:
131
- - - ! '>='
132
- - !ruby/object:Gem::Version
133
- version: '0'
134
- type: :development
135
- prerelease: false
136
- version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
- requirements:
139
- - - ! '>='
140
- - !ruby/object:Gem::Version
141
- version: '0'
142
110
  description: Adds missing native PostgreSQL data types to ActiveRecord
143
111
  email:
144
112
  - git@danmcclain.net
@@ -256,7 +224,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
256
224
  version: '0'
257
225
  segments:
258
226
  - 0
259
- hash: 1227873910843194517
227
+ hash: -3677858070850987585
260
228
  required_rubygems_version: !ruby/object:Gem::Requirement
261
229
  none: false
262
230
  requirements:
@@ -265,7 +233,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
265
233
  version: '0'
266
234
  segments:
267
235
  - 0
268
- hash: 1227873910843194517
236
+ hash: -3677858070850987585
269
237
  requirements: []
270
238
  rubyforge_project:
271
239
  rubygems_version: 1.8.23