aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorDoug Cole <doug@estately.com>2012-04-07 18:23:26 -0700
committerDoug Cole <doug@estately.com>2012-04-07 18:39:04 -0700
commitcd6ddc865a68d126a845d03337e1c7f775588db2 (patch)
tree1df30b89892f59e110ea872a65b4c1a07d314542 /activerecord
parent3a0d08163a76427611df0c109a5159daaf5b51bf (diff)
downloadrails-cd6ddc865a68d126a845d03337e1c7f775588db2.tar.gz
rails-cd6ddc865a68d126a845d03337e1c7f775588db2.tar.bz2
rails-cd6ddc865a68d126a845d03337e1c7f775588db2.zip
refactor configuration of insert_returning
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb18
-rw-r--r--activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb34
2 files changed, 19 insertions, 33 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index c39e8c8577..78c00d9341 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -17,7 +17,7 @@ module ActiveRecord
# Forward any unused config params to PGconn.connect.
[:statement_limit, :encoding, :min_messages, :schema_search_path,
:schema_order, :adapter, :pool, :wait_timeout, :template,
- :reaping_frequency].each do |key|
+ :reaping_frequency, :insert_returning].each do |key|
conn_params.delete key
end
conn_params.delete_if { |k,v| v.nil? }
@@ -259,6 +259,8 @@ module ActiveRecord
# <encoding></tt> call on the connection.
# * <tt>:min_messages</tt> - An optional client min messages that is used in a
# <tt>SET client_min_messages TO <min_messages></tt> call on the connection.
+ # * <tt>:insert_returning</tt> - An optional boolean to control the use or <tt>RETURNING</tt> for <tt>INSERT<tt> statements
+ # defaults to true.
#
# Any further options are used as connection parameters to libpq. See
# http://www.postgresql.org/docs/9.1/static/libpq-connect.html for the
@@ -406,7 +408,7 @@ module ActiveRecord
initialize_type_map
@local_tz = execute('SHOW TIME ZONE', 'SCHEMA').first["TimeZone"]
- self.enable_insert_returning!
+ @use_insert_returning = @config.key?(:insert_returning) ? @config[:insert_returning] : true
end
# Clears the prepared statements cache.
@@ -1258,18 +1260,6 @@ module ActiveRecord
end
end
- # Enable insert statements to use INSERT ... RETURNING ... statements. On by default.
- def enable_insert_returning!
- @use_insert_returning = true
- end
-
- # Disable INSERT ... RETURNING ... statements, using currval() instead.
- # Useful for trigger based insertions where INSERT RETURNING does the wrong thing.
- def disable_insert_returning!
- @use_insert_returning = false
- end
-
-
def use_insert_returning?
@use_insert_returning
end
diff --git a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb
index 3073b9f011..92e31a3e44 100644
--- a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb
+++ b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb
@@ -50,38 +50,30 @@ module ActiveRecord
end
def test_insert_sql_with_returning_disabled
- @connection.disable_insert_returning!
- id = @connection.insert_sql("insert into postgresql_partitioned_table_parent (number) VALUES (1)")
- expect = @connection.query('select max(id) from postgresql_partitioned_table_parent').first.first
+ connection = connection_without_insert_returning
+ id = connection.insert_sql("insert into postgresql_partitioned_table_parent (number) VALUES (1)")
+ expect = connection.query('select max(id) from postgresql_partitioned_table_parent').first.first
assert_equal expect, id
- ensure
- @connection.enable_insert_returning!
end
def test_exec_insert_with_returning_disabled
- @connection.disable_insert_returning!
- result = @connection.exec_insert("insert into postgresql_partitioned_table_parent (number) VALUES (1)", nil, [], 'id', 'postgresql_partitioned_table_parent_id_seq')
- expect = @connection.query('select max(id) from postgresql_partitioned_table_parent').first.first
+ connection = connection_without_insert_returning
+ result = connection.exec_insert("insert into postgresql_partitioned_table_parent (number) VALUES (1)", nil, [], 'id', 'postgresql_partitioned_table_parent_id_seq')
+ expect = connection.query('select max(id) from postgresql_partitioned_table_parent').first.first
assert_equal expect, result.rows.first.first
- ensure
- @connection.enable_insert_returning!
end
def test_exec_insert_with_returning_disabled_and_no_sequence_name_given
- @connection.disable_insert_returning!
- result = @connection.exec_insert("insert into postgresql_partitioned_table_parent (number) VALUES (1)", nil, [], 'id')
- expect = @connection.query('select max(id) from postgresql_partitioned_table_parent').first.first
+ connection = connection_without_insert_returning
+ result = connection.exec_insert("insert into postgresql_partitioned_table_parent (number) VALUES (1)", nil, [], 'id')
+ expect = connection.query('select max(id) from postgresql_partitioned_table_parent').first.first
assert_equal expect, result.rows.first.first
- ensure
- @connection.enable_insert_returning!
end
def test_sql_for_insert_with_returning_disabled
- @connection.disable_insert_returning!
- result = @connection.sql_for_insert('sql', nil, nil, nil, 'binds')
+ connection = connection_without_insert_returning
+ result = connection.sql_for_insert('sql', nil, nil, nil, 'binds')
assert_equal ['sql', 'binds'], result
- ensure
- @connection.enable_insert_returning!
end
def test_serial_sequence
@@ -239,6 +231,10 @@ module ActiveRecord
ctx.exec_insert(sql, 'SQL', binds)
end
+
+ def connection_without_insert_returning
+ ActiveRecord::Base.postgresql_connection(ActiveRecord::Base.configurations['arunit'].merge(:insert_returning => false))
+ end
end
end
end