postgres_ext 0.4.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -6
- data/docs/querying.md +6 -6
- data/lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb +6 -25
- data/lib/postgres_ext/version.rb +1 -1
- data/postgres_ext.gemspec +2 -1
- data/spec/migrations/array_spec.rb +70 -12
- metadata +7 -7
- data/LICENSE +0 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 04e9453088f60586e98a2f2213afe6e64c075aa7
|
4
|
+
data.tar.gz: 25d1bf9062740090514152e513cc684b4e42a582
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 22a8dd126361df619dbd44c45ecaf645ef90f8eb5b23c08b5b4252a2faad6a1d92ec490c7c705645f273c9a8c08ba4154714d466967baed2a0203d1f06868d59
|
7
|
+
data.tar.gz: 45f1d420e3dcb6a3cf295fe60cabcdf35a40dbc8239d50832f4b9c2d5e54adcfa6db0aced323b397a0f7a017251fbcd7d8ddc6d22890cdb1a13c8befb158334f
|
data/README.md
CHANGED
@@ -8,15 +8,11 @@ Adds support for missing PostgreSQL data types to ActiveRecord.
|
|
8
8
|
## Looking for help? ##
|
9
9
|
|
10
10
|
If it is a bug [please open an issue on
|
11
|
-
Github](https://github.com/dockyard/
|
11
|
+
Github](https://github.com/dockyard/postgres_ext/issues). If you need help using
|
12
12
|
the gem please ask the question on
|
13
13
|
[Stack Overflow](http://stackoverflow.com). Be sure to tag the
|
14
14
|
question with `DockYard` so we can find it.
|
15
15
|
|
16
|
-
## Note ##
|
17
|
-
PostgresExt is dropping support for Ruby 1.8.7 with the next minor
|
18
|
-
release.
|
19
|
-
|
20
16
|
## Installation
|
21
17
|
|
22
18
|
Add this line to your application's Gemfile:
|
@@ -84,7 +80,6 @@ supported in postgres\_ext at this time.
|
|
84
80
|
|
85
81
|
|
86
82
|
|
87
|
-
|
88
83
|
## Authors
|
89
84
|
|
90
85
|
Dan McClain [twitter](http://twitter.com/_danmcclain) [github](http://github.com/danmcclain)
|
data/docs/querying.md
CHANGED
@@ -30,7 +30,7 @@ would be:
|
|
30
30
|
User.where.overlap(:nick_names => ['Bob', 'Fred'])
|
31
31
|
```
|
32
32
|
|
33
|
-
Postgres\_ext defines `
|
33
|
+
Postgres\_ext defines `overlap`, an [Arel](https://github.com/rails/arel)
|
34
34
|
predicate for the `&&` operator. This is utilized by the `where.overlap`
|
35
35
|
call above.
|
36
36
|
|
@@ -38,7 +38,7 @@ call above.
|
|
38
38
|
user_arel = User.arel_table
|
39
39
|
|
40
40
|
# Execute the query
|
41
|
-
User.where(user_arel[:tags].
|
41
|
+
User.where(user_arel[:tags].overlap(['one','two']))
|
42
42
|
# => SELECT \"users\".* FROM \"users\" WHERE \"users\".\"tags\" && '{one,two}'
|
43
43
|
```
|
44
44
|
|
@@ -62,15 +62,15 @@ adding a `contains` method. To make a contains query, you can do:
|
|
62
62
|
User.where.contains(:nick_names => ['Bob', 'Fred'])
|
63
63
|
```
|
64
64
|
|
65
|
-
Postgres\_ext
|
66
|
-
predicate
|
67
|
-
`where.
|
65
|
+
Postgres\_ext overrides `contains`, an [Arel](https://github.com/rails/arel)
|
66
|
+
predicate, to use the `@>` operator for arrays. This is utilized by the
|
67
|
+
`where.contains` call above.
|
68
68
|
|
69
69
|
```ruby
|
70
70
|
user_arel = User.arel_table
|
71
71
|
|
72
72
|
# Execute the query
|
73
|
-
User.where(user_arel[:tags].
|
73
|
+
User.where(user_arel[:tags].contains(['one','two']))
|
74
74
|
# => SELECT "users".* FROM "users" WHERE "users"."tags" @> '{"one","two"}'
|
75
75
|
```
|
76
76
|
|
@@ -59,11 +59,7 @@ module ActiveRecord
|
|
59
59
|
value
|
60
60
|
else
|
61
61
|
string_array = parse_pg_array value
|
62
|
-
|
63
|
-
force_character_encoding(string_array)
|
64
|
-
else
|
65
|
-
type_cast_array(string_array)
|
66
|
-
end
|
62
|
+
type_cast_array(string_array)
|
67
63
|
end
|
68
64
|
end
|
69
65
|
|
@@ -178,12 +174,6 @@ module ActiveRecord
|
|
178
174
|
|
179
175
|
private
|
180
176
|
|
181
|
-
def force_character_encoding(string_array)
|
182
|
-
string_array.map do |item|
|
183
|
-
item.respond_to?(:force_encoding) ? item.force_encoding(ActiveRecord::Base.connection.encoding_for_ruby) : item
|
184
|
-
end
|
185
|
-
end
|
186
|
-
|
187
177
|
def simplified_type_with_extended_types(field_type)
|
188
178
|
case field_type
|
189
179
|
when 'uuid'
|
@@ -287,19 +277,6 @@ module ActiveRecord
|
|
287
277
|
|
288
278
|
NATIVE_DATABASE_TYPES.merge!(EXTENDED_TYPES)
|
289
279
|
|
290
|
-
# Translate from the current database encoding to the encoding we
|
291
|
-
# will force string array components into on retrievial.
|
292
|
-
def encoding_for_ruby
|
293
|
-
@database_encoding ||= case ActiveRecord::Base.connection.encoding
|
294
|
-
when 'UTF8'
|
295
|
-
'UTF-8'
|
296
|
-
when 'SQL_ASCII'
|
297
|
-
'ASCII'
|
298
|
-
else
|
299
|
-
ActiveRecord::Base.connection.encoding
|
300
|
-
end
|
301
|
-
end
|
302
|
-
|
303
280
|
def supports_extensions?
|
304
281
|
postgresql_version > 90100
|
305
282
|
end
|
@@ -485,7 +462,11 @@ module ActiveRecord
|
|
485
462
|
clear_cache!
|
486
463
|
quoted_table_name = quote_table_name(table_name)
|
487
464
|
|
488
|
-
|
465
|
+
if type.to_s =~ /string|text/
|
466
|
+
execute "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(column_name)} TYPE #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}[] USING string_to_array(#{quote_column_name(column_name)}, ',')"
|
467
|
+
else
|
468
|
+
execute "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(column_name)} TYPE #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}[]"
|
469
|
+
end
|
489
470
|
|
490
471
|
change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
|
491
472
|
change_column_null(table_name, column_name, options[:null], options[:default]) if options.key?(:null)
|
data/lib/postgres_ext/version.rb
CHANGED
data/postgres_ext.gemspec
CHANGED
@@ -7,6 +7,7 @@ Gem::Specification.new do |gem|
|
|
7
7
|
gem.description = %q{Adds missing native PostgreSQL data types to ActiveRecord}
|
8
8
|
gem.summary = %q{Extends ActiveRecord to handle native PostgreSQL data types}
|
9
9
|
gem.homepage = ""
|
10
|
+
gem.licenses = ['MIT']
|
10
11
|
|
11
12
|
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
13
|
gem.files = `git ls-files`.split("\n")
|
@@ -16,7 +17,7 @@ Gem::Specification.new do |gem|
|
|
16
17
|
gem.version = PostgresExt::VERSION
|
17
18
|
|
18
19
|
gem.add_dependency 'activerecord', '~> 3.2.0'
|
19
|
-
gem.add_dependency 'pg_array_parser', '~> 0.0.
|
20
|
+
gem.add_dependency 'pg_array_parser', '~> 0.0.9'
|
20
21
|
|
21
22
|
gem.add_development_dependency 'rails', '~> 3.2.0'
|
22
23
|
gem.add_development_dependency 'rspec-rails', '~> 2.12.0'
|
@@ -83,22 +83,80 @@ describe 'Array migrations' do
|
|
83
83
|
end
|
84
84
|
|
85
85
|
context 'Change Column' do
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
86
|
+
describe 'Change with column existing as array' do
|
87
|
+
after { connection.drop_table :data_types }
|
88
|
+
it 'updates the column definitions' do
|
89
|
+
lambda do
|
90
|
+
connection.create_table :data_types do |t|
|
91
|
+
t.integer :array_1, :array => true, :default => []
|
92
|
+
end
|
93
|
+
|
94
|
+
connection.change_column :data_types, :array_1, :integer, :array => true, :default => [], :null => false
|
95
|
+
end.should_not raise_exception
|
96
|
+
|
97
|
+
columns = connection.columns(:data_types)
|
98
|
+
|
99
|
+
array_1 = columns.detect { |c| c.name == 'array_1'}
|
100
|
+
array_1.sql_type.should eq 'integer[]'
|
101
|
+
array_1.default.should eq []
|
102
|
+
array_1.null.should be_false
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe 'Change string column to array' do
|
107
|
+
after { connection.drop_table :data_types }
|
108
|
+
it 'updates the column definitions' do
|
109
|
+
lambda do
|
110
|
+
connection.create_table :data_types do |t|
|
111
|
+
t.string :string_1
|
112
|
+
end
|
113
|
+
|
114
|
+
connection.exec_query "INSERT INTO data_types (string_1) VALUES ('some,values,here')"
|
115
|
+
connection.change_column :data_types, :string_1, :string, :array => true, :default => [], :null => false
|
116
|
+
end.should_not raise_exception
|
117
|
+
|
118
|
+
columns = connection.columns(:data_types)
|
119
|
+
|
120
|
+
string_1 = columns.detect { |c| c.name == 'string_1'}
|
121
|
+
string_1.sql_type.should eq 'character varying(255)[]'
|
122
|
+
string_1.default.should eq []
|
123
|
+
string_1.null.should be_false
|
124
|
+
|
125
|
+
new_string_value = connection.exec_query "SELECT array_length(string_1, 1) FROM data_types LIMIT 1"
|
126
|
+
if RUBY_PLATFORM =~ /java/
|
127
|
+
new_string_value.first['array_length'].should eq 3
|
128
|
+
else
|
129
|
+
new_string_value.rows.first.should == ['3']
|
91
130
|
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
describe 'Change text column to array' do
|
135
|
+
after { connection.drop_table :data_types }
|
136
|
+
it 'updates the column definitions' do
|
137
|
+
lambda do
|
138
|
+
connection.create_table :data_types do |t|
|
139
|
+
t.text :text_1
|
140
|
+
end
|
141
|
+
|
142
|
+
connection.exec_query "INSERT INTO data_types (text_1) VALUES ('some,values,here')"
|
143
|
+
connection.change_column :data_types, :text_1, :text, :array => true, :default => [], :null => false
|
144
|
+
end.should_not raise_exception
|
92
145
|
|
93
|
-
connection.
|
94
|
-
end.should_not raise_exception
|
146
|
+
columns = connection.columns(:data_types)
|
95
147
|
|
96
|
-
|
148
|
+
text_1 = columns.detect { |c| c.name == 'text_1'}
|
149
|
+
text_1.sql_type.should eq 'text[]'
|
150
|
+
text_1.default.should eq []
|
151
|
+
text_1.null.should be_false
|
97
152
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
153
|
+
new_text_value = connection.exec_query "SELECT array_length(text_1, 1) FROM data_types LIMIT 1"
|
154
|
+
if RUBY_PLATFORM =~ /java/
|
155
|
+
new_text_value.first['array_length'].should eq 3
|
156
|
+
else
|
157
|
+
new_text_value.rows.first.should eq ['3']
|
158
|
+
end
|
159
|
+
end
|
102
160
|
end
|
103
161
|
end
|
104
162
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: postgres_ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dan McClain
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-08-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.0.
|
33
|
+
version: 0.0.9
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 0.0.
|
40
|
+
version: 0.0.9
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rails
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -107,7 +107,6 @@ files:
|
|
107
107
|
- CHANGELOG.md
|
108
108
|
- CONTRIBUTING.md
|
109
109
|
- Gemfile
|
110
|
-
- LICENSE
|
111
110
|
- README.md
|
112
111
|
- Rakefile
|
113
112
|
- docs/indexes.md
|
@@ -238,7 +237,8 @@ files:
|
|
238
237
|
- spec/schema_dumper/uuid_spec.rb
|
239
238
|
- spec/spec_helper.rb
|
240
239
|
homepage: ''
|
241
|
-
licenses:
|
240
|
+
licenses:
|
241
|
+
- MIT
|
242
242
|
metadata: {}
|
243
243
|
post_install_message:
|
244
244
|
rdoc_options: []
|
@@ -256,7 +256,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
256
256
|
version: '0'
|
257
257
|
requirements: []
|
258
258
|
rubyforge_project:
|
259
|
-
rubygems_version: 2.0.
|
259
|
+
rubygems_version: 2.0.3
|
260
260
|
signing_key:
|
261
261
|
specification_version: 4
|
262
262
|
summary: Extends ActiveRecord to handle native PostgreSQL data types
|
data/LICENSE
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
Copyright (c) 2012 Dan Seaver
|
2
|
-
|
3
|
-
MIT License
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
-
a copy of this software and associated documentation files (the
|
7
|
-
"Software"), to deal in the Software without restriction, including
|
8
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
-
permit persons to whom the Software is furnished to do so, subject to
|
11
|
-
the following conditions:
|
12
|
-
|
13
|
-
The above copyright notice and this permission notice shall be
|
14
|
-
included in all copies or substantial portions of the Software.
|
15
|
-
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|