activerecord-postgresql-extensions 0.4.0 → 0.5.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.
data/Gemfile CHANGED
@@ -10,7 +10,7 @@ end
10
10
 
11
11
  gem "rdoc", "~> 3.12"
12
12
  gem "rake", "~> 10.0"
13
- gem "minitest"
13
+ gem "minitest", "~> 4.7"
14
14
  gem "minitest-reporters"
15
15
  gem "guard-minitest"
16
16
  gem "simplecov"
@@ -11,6 +11,7 @@ Gem::Specification.new do |s|
11
11
  s.description = "A whole bunch of extensions the ActiveRecord PostgreSQL adapter."
12
12
  s.summary = s.description
13
13
  s.email = "code@zoocasa.com"
14
+ s.license = "MIT"
14
15
  s.extra_rdoc_files = [
15
16
  "README.rdoc"
16
17
  ]
@@ -28,9 +28,14 @@ module ActiveRecord
28
28
 
29
29
  module ConnectionAdapters
30
30
  class PostgreSQLAdapter
31
+ # :call-seq:
32
+ # create_function(name, arguments, returns, language, options = {}, &block)
33
+ # create_function(name, arguments, returns, language, body, options = {})
34
+ # create_function(name, returns, language, options = {}, &block)
35
+ #
31
36
  # Creates a PostgreSQL function/stored procedure.
32
37
  #
33
- # +args+ is a simple String that you can use to represent the
38
+ # +arguments+ is a simple String that you can use to represent the
34
39
  # function arguments.
35
40
  #
36
41
  # +returns+ is the return type for the function.
@@ -44,7 +49,8 @@ module ActiveRecord
44
49
  # C, this will be an Array containing two items: the object file
45
50
  # the function is found in and the link symbol for the function.
46
51
  # In all other cases, this argument will be a String containing
47
- # the actual function code.
52
+ # the actual function code. You can also provide either a block that
53
+ # returns the function body as a String or a :body option.
48
54
  #
49
55
  # ==== Options
50
56
  #
@@ -83,6 +89,8 @@ module ActiveRecord
83
89
  # with the parameters as keys and the values to set as values.
84
90
  # When using a Hash, the value <tt>:from_current</tt> can be
85
91
  # used to specify the actual <tt>FROM CURRENT</tt> clause.
92
+ # * <tt>:body</tt> - allows you to set a function body when the arguments
93
+ # to <tt>create_function</tt> are ambiguous.
86
94
  #
87
95
  # You should definitely check out the PostgreSQL documentation
88
96
  # on creating stored procedures, because it can get pretty
@@ -112,10 +120,25 @@ module ActiveRecord
112
120
  # # LANGUAGE "sql"
113
121
  # # IMMUTABLE
114
122
  # # SET "search_path" FROM CURRENT;
115
- def create_function(name, arguments, returns, language, *args)
123
+ def create_function(name, *args)
116
124
  options = args.extract_options!
117
125
 
118
- body = if args.first.present?
126
+ raise ArgumentError.new("Expected 3-6 arguments") unless args.length.between?(2, 4)
127
+ raise ArgumentError.new("Ambiguous arguments: can't specify a function body as an argument without any function arguments. Hint: Use a :body option.") if args.length <= 3 && !block_given? && !options.key?(:body)
128
+
129
+ arguments, returns, language = if args.length >= 3
130
+ args.shift(3)
131
+ else
132
+ [ nil ] + args.shift(2)
133
+ end
134
+
135
+ body = if options.key?(:body)
136
+ if block_given? || args.first.present?
137
+ raise ArgumentError.new("Can't have both a :body option as well as a block or body argument in create_function")
138
+ end
139
+
140
+ options[:body].to_s
141
+ elsif args.first.present?
119
142
  if block_given?
120
143
  raise ArgumentError.new("Can't have both a function body argument as well as a block in create_function")
121
144
  end
@@ -135,27 +158,50 @@ module ActiveRecord
135
158
  # * <tt>:if_exists</tt> - adds an <tt>IF EXISTS</tt> clause.
136
159
  # * <tt>:cascade</tt> - cascades the operation on to any objects
137
160
  # referring to the function.
138
- def drop_function(name, args, options = {})
161
+ def drop_function(name, *args)
162
+ raise ArgumentError.new("Expected 2-3 arguments") unless args.length.between?(1, 2)
163
+
164
+ options = args.extract_options!
165
+ arguments = args.first
166
+
139
167
  sql = 'DROP FUNCTION '
140
168
  sql << 'IF EXISTS ' if options[:if_exists]
141
- sql << "#{quote_function(name)}(#{args})"
169
+ sql << "#{quote_function(name)}(#{arguments})"
142
170
  sql << ' CASCADE' if options[:cascade]
143
171
  execute "#{sql};"
144
172
  end
145
173
 
146
174
  # Renames a function.
147
- def rename_function(name, args, rename_to, options = {})
148
- execute PostgreSQLFunctionAlterer.new(self, name, args, :rename_to => rename_to).to_s
175
+ def rename_function(name, *args)
176
+ raise ArgumentError.new("Expected 2-3 arguments") unless args.length.between?(1, 2)
177
+
178
+ options = args.extract_options!
179
+ rename_to = args.pop
180
+ arguments = args.pop
181
+
182
+ execute PostgreSQLFunctionAlterer.new(self, name, arguments, :rename_to => rename_to).to_s
149
183
  end
150
184
 
151
185
  # Changes the function's owner.
152
- def alter_function_owner(name, args, owner_to, options = {})
153
- execute PostgreSQLFunctionAlterer.new(self, name, args, :owner_to => owner_to).to_s
186
+ def alter_function_owner(name, *args)
187
+ raise ArgumentError.new("Expected 2-3 arguments") unless args.length.between?(1, 2)
188
+
189
+ options = args.extract_options!
190
+ owner_to = args.pop
191
+ arguments = args.pop
192
+
193
+ execute PostgreSQLFunctionAlterer.new(self, name, arguments, :owner_to => owner_to).to_s
154
194
  end
155
195
 
156
196
  # Changes the function's schema.
157
- def alter_function_schema(name, args, set_schema, options = {})
158
- execute PostgreSQLFunctionAlterer.new(self, name, args, :set_schema => set_schema).to_s
197
+ def alter_function_schema(name, *args)
198
+ raise ArgumentError.new("Expected 2-3 arguments") unless args.length.between?(1, 2)
199
+
200
+ options = args.extract_options!
201
+ set_schema = args.pop
202
+ arguments = args.pop
203
+
204
+ execute PostgreSQLFunctionAlterer.new(self, name, arguments, :set_schema => set_schema).to_s
159
205
  end
160
206
 
161
207
  # Alters a function. There's a ton of stuff you can do here, and
@@ -187,8 +233,12 @@ module ActiveRecord
187
233
  # #
188
234
  # # ALTER FUNCTION "my_function"(integer) OWNER TO "jdoe";
189
235
  # # ALTER FUNCTION "my_function"(integer) RENAME TO "another_function";
190
- def alter_function(name, args, options = {})
191
- alterer = PostgreSQLFunctionAlterer.new(self, name, args, options)
236
+ def alter_function(name, *args)
237
+ raise ArgumentError.new("Expected 2-3 arguments") unless args.length.between?(0, 2)
238
+
239
+ options = args.extract_options!
240
+ arguments = args.pop
241
+ alterer = PostgreSQLFunctionAlterer.new(self, name, arguments, options)
192
242
 
193
243
  if block_given?
194
244
  yield alterer
@@ -1,7 +1,7 @@
1
1
 
2
2
  module ActiveRecord
3
3
  module PostgreSQLExtensions
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.0"
5
5
  end
6
6
  end
7
7
 
@@ -46,6 +46,49 @@ class FunctionsTests < PostgreSQLExtensionsTestCase
46
46
  assert_equal(expected, statements)
47
47
  end
48
48
 
49
+ def test_create_function_with_empty_arguments
50
+ ARBC.create_function(:test, :integer, :sql) do
51
+ "select 10;"
52
+ end
53
+
54
+ ARBC.create_function(:test, :integer, :sql, {
55
+ :force => true,
56
+ :delimiter => '$__$',
57
+ :behavior => :immutable,
58
+ :on_null_input => :strict,
59
+ :cost => 1,
60
+ :rows => 10,
61
+ :set => {
62
+ 'TIME ZONE' => 'America/Halifax'
63
+ }
64
+ }) do
65
+ "return 10;"
66
+ end
67
+
68
+ expected = []
69
+
70
+ expected << strip_heredoc(<<-SQL)
71
+ CREATE FUNCTION "test"() RETURNS integer AS $$
72
+ select 10;
73
+ $$
74
+ LANGUAGE "sql";
75
+ SQL
76
+
77
+ expected << strip_heredoc(<<-SQL)
78
+ CREATE OR REPLACE FUNCTION "test"() RETURNS integer AS $__$
79
+ return 10;
80
+ $__$
81
+ LANGUAGE "sql"
82
+ IMMUTABLE
83
+ STRICT
84
+ COST 1
85
+ ROWS 10
86
+ SET TIME ZONE "America/Halifax";
87
+ SQL
88
+
89
+ assert_equal(expected, statements)
90
+ end
91
+
49
92
  def test_create_function_with_body_argument
50
93
  ARBC.create_function(:test, :integer, :integer, :sql, "select 10;")
51
94
 
@@ -85,6 +128,50 @@ class FunctionsTests < PostgreSQLExtensionsTestCase
85
128
  assert_equal(expected, statements)
86
129
  end
87
130
 
131
+ def test_create_function_with_body_argument_and_empty_arguments
132
+ assert_raises(ArgumentError) do
133
+ ARBC.create_function(:test, :integer, :sql, "select 10;")
134
+ end
135
+
136
+ assert_raises(ArgumentError) do
137
+ ARBC.create_function(:test, :integer, :sql, "select 10;", {
138
+ :force => true
139
+ })
140
+ end
141
+
142
+ assert_raises(ArgumentError) do
143
+ ARBC.create_function(:test, :integer, :sql, {
144
+ :body => "select 20"
145
+ }) do
146
+ "select 30"
147
+ end
148
+ end
149
+
150
+ ARBC.create_function(:test, :integer, :sql, :body => "select 20;")
151
+ ARBC.create_function(:test, :integer, :sql, {
152
+ :body => "select 20;",
153
+ :force => true
154
+ })
155
+
156
+ expected = []
157
+
158
+ expected << strip_heredoc(<<-SQL)
159
+ CREATE FUNCTION "test"() RETURNS integer AS $$
160
+ select 20;
161
+ $$
162
+ LANGUAGE "sql";
163
+ SQL
164
+
165
+ expected << strip_heredoc(<<-SQL)
166
+ CREATE OR REPLACE FUNCTION "test"() RETURNS integer AS $$
167
+ select 20;
168
+ $$
169
+ LANGUAGE "sql";
170
+ SQL
171
+
172
+ assert_equal(expected, statements)
173
+ end
174
+
88
175
  def test_drop_function
89
176
  ARBC.drop_function(:test, :integer)
90
177
  ARBC.drop_function(:test, :integer, :if_exists => true, :cascade => true)
@@ -97,31 +184,39 @@ class FunctionsTests < PostgreSQLExtensionsTestCase
97
184
 
98
185
  def test_rename_function
99
186
  ARBC.rename_function(:test, 'integer, text', :foo)
187
+ ARBC.rename_function(:test, :foo)
100
188
 
101
189
  assert_equal([
102
- %{ALTER FUNCTION "test"(integer, text) RENAME TO "foo";}
190
+ %{ALTER FUNCTION "test"(integer, text) RENAME TO "foo";},
191
+ %{ALTER FUNCTION "test"() RENAME TO "foo";}
103
192
  ], statements)
104
193
  end
105
194
 
106
195
  def test_alter_function_owner
107
196
  ARBC.alter_function_owner(:test, 'integer, text', :admin)
197
+ ARBC.alter_function_owner(:test, :admin)
108
198
 
109
199
  assert_equal([
110
- %{ALTER FUNCTION "test"(integer, text) OWNER TO "admin";}
200
+ %{ALTER FUNCTION "test"(integer, text) OWNER TO "admin";},
201
+ %{ALTER FUNCTION "test"() OWNER TO "admin";}
111
202
  ], statements)
112
203
  end
113
204
 
114
205
  def test_alter_function_schema
115
206
  ARBC.alter_function_schema(:test, 'integer, text', :geospatial)
207
+ ARBC.alter_function_schema(:test, :geospatial)
116
208
 
117
209
  assert_equal([
118
- %{ALTER FUNCTION "test"(integer, text) SET SCHEMA "geospatial";}
210
+ %{ALTER FUNCTION "test"(integer, text) SET SCHEMA "geospatial";},
211
+ %{ALTER FUNCTION "test"() SET SCHEMA "geospatial";}
119
212
  ], statements)
120
213
  end
121
214
 
122
215
  def test_alter_function
123
216
  ARBC.alter_function('my_function', 'integer', :rename_to => 'another_function')
217
+ ARBC.alter_function('my_function', :rename_to => 'another_function')
124
218
  ARBC.alter_function('another_function', 'integer', :owner_to => 'jdoe')
219
+ ARBC.alter_function('another_function', :owner_to => 'jdoe')
125
220
  ARBC.alter_function('my_function', 'integer') do |f|
126
221
  f.rename_to 'another_function'
127
222
  f.owner_to 'jdoe'
@@ -137,9 +232,15 @@ class FunctionsTests < PostgreSQLExtensionsTestCase
137
232
  f.reset %w{ debug_assertions trace_notify }
138
233
  end
139
234
 
235
+ ARBC.alter_function('my_function') do |f|
236
+ f.rename_to 'another_function'
237
+ end
238
+
140
239
  expected = [
141
240
  %{ALTER FUNCTION "my_function"(integer) RENAME TO "another_function";},
142
- %{ALTER FUNCTION "another_function"(integer) OWNER TO "jdoe";}
241
+ %{ALTER FUNCTION "my_function"() RENAME TO "another_function";},
242
+ %{ALTER FUNCTION "another_function"(integer) OWNER TO "jdoe";},
243
+ %{ALTER FUNCTION "another_function"() OWNER TO "jdoe";}
143
244
  ]
144
245
 
145
246
  expected << strip_heredoc(<<-SQL)
@@ -155,6 +256,8 @@ class FunctionsTests < PostgreSQLExtensionsTestCase
155
256
  ALTER FUNCTION "another_function"(integer) RESET "debug_assertions" RESET "trace_notify";
156
257
  SQL
157
258
 
259
+ expected << %{ALTER FUNCTION "my_function"() RENAME TO "another_function";}
260
+
158
261
  assert_equal(expected, statements)
159
262
  end
160
263
  end
metadata CHANGED
@@ -1,29 +1,32 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-postgresql-extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ prerelease:
5
+ version: 0.5.0
5
6
  platform: ruby
6
7
  authors:
7
8
  - J Smith
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-10-28 00:00:00.000000000 Z
12
+ date: 2013-11-25 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
- type: :runtime
15
- prerelease: false
16
15
  name: activerecord
17
- version_requirements: !ruby/object:Gem::Requirement
16
+ type: :runtime
17
+ requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
21
  version: '2.3'
22
- requirement: !ruby/object:Gem::Requirement
22
+ none: false
23
+ version_requirements: !ruby/object:Gem::Requirement
23
24
  requirements:
24
25
  - - ! '>='
25
26
  - !ruby/object:Gem::Version
26
27
  version: '2.3'
28
+ none: false
29
+ prerelease: false
27
30
  description: A whole bunch of extensions the ActiveRecord PostgreSQL adapter.
28
31
  email: code@zoocasa.com
29
32
  executables: []
@@ -89,8 +92,8 @@ files:
89
92
  - test/vacuum_tests.rb
90
93
  - test/views_tests.rb
91
94
  homepage: http://github.com/zoocasa/activerecord-postgresql-extensions
92
- licenses: []
93
- metadata: {}
95
+ licenses:
96
+ - MIT
94
97
  post_install_message:
95
98
  rdoc_options: []
96
99
  require_paths:
@@ -99,17 +102,22 @@ required_ruby_version: !ruby/object:Gem::Requirement
99
102
  requirements:
100
103
  - - ! '>='
101
104
  - !ruby/object:Gem::Version
105
+ segments:
106
+ - 0
107
+ hash: 1506709517929414109
102
108
  version: '0'
109
+ none: false
103
110
  required_rubygems_version: !ruby/object:Gem::Requirement
104
111
  requirements:
105
112
  - - ! '>='
106
113
  - !ruby/object:Gem::Version
107
114
  version: '0'
115
+ none: false
108
116
  requirements: []
109
117
  rubyforge_project:
110
- rubygems_version: 2.1.5
118
+ rubygems_version: 1.8.23
111
119
  signing_key:
112
- specification_version: 4
120
+ specification_version: 3
113
121
  summary: A whole bunch of extensions the ActiveRecord PostgreSQL adapter.
114
122
  test_files:
115
123
  - test/adapter_extensions_tests.rb
checksums.yaml DELETED
@@ -1,15 +0,0 @@
1
- ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- OGY1YjMzZWU1ZmNjN2VmZDU5ODQyM2Q0ZDg3N2JjNjkwZTFkMTkwYQ==
5
- data.tar.gz: !binary |-
6
- ZGMxNWRjNjEwNzVhZjU1MzMxYTQ4ZTk0NWU2MjljYzViMDM5YzdjNQ==
7
- SHA512:
8
- metadata.gz: !binary |-
9
- NWRkMmZiNWViMzE4ZjBhNDEzOWFlNGJkOTIxZjg1MjljM2IyOWVmM2QzODA2
10
- OGQzOWY2YjRhY2UzMzE5MzBlN2YxYzM3MjZkMTUzYzc4NDM3Zjc3NzliZWZm
11
- MTc5NDU2NGE2MjY2YjUxYzhiYTI5MTVlNmZiNDFjMmZkOWFlM2U=
12
- data.tar.gz: !binary |-
13
- YjQyYWU2ZDM3MjQ0NmFhNDVmOWI3N2U0MWZkZDM5MTRiNTRhZmFlM2M4N2Jj
14
- YjdkZTUyODk5ZmFlYmJhNzVhMjdjZDc5YjdmY2QyODMxZmY2ZmIwZTBiNmE5
15
- YmZkMjE4NmZhNjAzNWI4MjIwOThkNjYzMzg3ZjQyMTQ1ODQ3NTE=